Allow room_types in directory::Filter to be null with the compat feature

This commit is contained in:
Jonas Platte 2022-12-16 09:56:18 +01:00
parent 5ff113a286
commit 1c9e0e7718
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
2 changed files with 12 additions and 2 deletions

View File

@ -109,7 +109,7 @@ impl From<PublicRoomsChunkInit> for PublicRoomsChunk {
}
}
/// A filter for public rooms lists
/// A filter for public rooms lists.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Filter {
@ -121,6 +121,7 @@ pub struct Filter {
///
/// Includes all room types if it is empty.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(feature = "compat", serde(deserialize_with = "crate::serde::none_as_default"))]
pub room_types: Vec<RoomTypeFilter>,
}

View File

@ -5,7 +5,7 @@
//!
//! [serde_urlencoded]: https://github.com/nox/serde_urlencoded
use serde::{de, Deserialize};
use serde::{de, Deserialize, Deserializer};
use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
pub mod base64;
@ -40,6 +40,15 @@ pub fn is_default<T: Default + PartialEq>(val: &T) -> bool {
*val == T::default()
}
/// Deserialize a `T` via `Option<T>`, falling back to `T::default()`.
pub fn none_as_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: Default + Deserialize<'de>,
{
Ok(Option::deserialize(deserializer)?.unwrap_or_default())
}
/// Simply returns `true`.
///
/// Useful for `#[serde(default = ...)]`.