r0::sync::sync_events: Fix (de)serialization of filter argument

This commit is contained in:
Jonas Platte 2019-02-02 22:11:59 +01:00
parent 14fbaf698f
commit 71f6a93d76

View File

@ -68,12 +68,45 @@ pub enum SetPresence {
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum Filter { pub enum Filter {
// The filter definition needs to be (de)serialized twice because it is a URL-encoded JSON
// string. Since #[ruma_api(query)] only does the latter and this is a very uncommon
// setup, we implement it through custom serde logic for this specific enum variant rather than
// adding another ruma_api attribute.
//
// On the deserialization side, because this is an enum with #[serde(untagged)], serde will
// try the variants in order (https://serde.rs/enum-representations.html). That means because
// FilterDefinition is the first variant, JSON decoding is attempted first which is almost
// functionally equivalent to looking at whether the first symbol is a '{' as the spec says.
// (there are probably some corner cases like leading whitespace)
#[serde(with = "filter_def_serde")]
/// A complete filter definition serialized to JSON. /// A complete filter definition serialized to JSON.
FilterDefinition(FilterDefinition), FilterDefinition(FilterDefinition),
/// The ID of a filter saved on the server. /// The ID of a filter saved on the server.
FilterId(String), FilterId(String),
} }
mod filter_def_serde {
use serde::{de::Error as _, ser::Error as _, Deserialize, Deserializer, Serializer};
use crate::r0::filter::FilterDefinition;
pub fn serialize<S>(filter_def: &FilterDefinition, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let string = serde_json::to_string(filter_def).map_err(S::Error::custom)?;
serializer.serialize_str(&string)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<FilterDefinition, D::Error>
where
D: Deserializer<'de>,
{
let filter_str = <&str>::deserialize(deserializer)?;
serde_json::from_str(filter_str).map_err(D::Error::custom)
}
}
/// Updates to rooms. /// Updates to rooms.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Rooms { pub struct Rooms {