Fix failing tests

This commit is contained in:
Jonas Platte 2019-10-18 00:46:35 +02:00
parent 85990676cd
commit 7f771bc788
3 changed files with 58 additions and 15 deletions

View File

@ -1,12 +1,10 @@
use std::{ use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
fmt::{Debug, Display, Formatter, Result as FmtResult},
marker::PhantomData,
};
use serde::{ use serde::{
de::{Error as SerdeError, Visitor}, de::{DeserializeOwned, Error as SerdeError, Visitor},
Deserialize, Deserializer, Serialize, Serializer, Deserialize, Deserializer, Serialize, Serializer,
}; };
use serde_json::Value;
/// The type of an event. /// The type of an event.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -114,9 +112,9 @@ pub trait FromRaw: Sized {
fn from_raw(_: Self::Raw) -> Self; fn from_raw(_: Self::Raw) -> Self;
} }
pub trait TryFromRaw { pub trait TryFromRaw: Sized {
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails. /// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
type Raw; type Raw: DeserializeOwned;
type Err: Into<String>; type Err: Into<String>;
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>; fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>;
@ -131,7 +129,41 @@ impl<T: FromRaw> TryFromRaw for T {
} }
} }
enum Void {} impl<'de, T> Deserialize<'de> for EventResult<T>
where
T: TryFromRaw,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let json = serde_json::Value::deserialize(deserializer)?;
let raw_data: T::Raw = match serde_json::from_value(json.clone()) {
Ok(raw) => raw,
Err(error) => {
return Ok(EventResult::Err(InvalidEvent(
InnerInvalidEvent::Deserialization {
json,
error: error.to_string(),
},
)));
}
};
match T::try_from_raw(raw_data) {
Ok(value) => Ok(EventResult::Ok(value)),
Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent(
InnerInvalidEvent::Validation {
message: msg.into(),
raw_data,
},
))),
}
}
}
pub enum Void {}
impl From<Void> for String { impl From<Void> for String {
fn from(v: Void) -> Self { fn from(v: Void) -> Self {
@ -196,15 +228,22 @@ pub struct InvalidEvent<T>(InnerInvalidEvent<T>);
/// An event that is malformed or otherwise invalid. /// An event that is malformed or otherwise invalid.
#[derive(Debug)] #[derive(Debug)]
enum InnerInvalidEvent<T> { enum InnerInvalidEvent<T> {
/// An event that failed to deserialize from JSON.
Deserialization {
/// The raw `serde_json::Value` representation of the invalid event.
json: Value,
/// The deserialization error returned by serde.
error: String,
},
/// An event that deserialized but failed validation. /// An event that deserialized but failed validation.
Validation { Validation {
/// The raw `serde_json::Value` representation of the invalid event. /// The event data that failed validation.
json: serde_json::Value, raw_data: T,
/// An message describing why the event was invalid. /// An message describing why the event was invalid.
message: String, message: String,
dummy: PhantomData<T>,
}, },
} }

View File

@ -40,6 +40,7 @@ impl Serialize for IgnoredUserListEvent {
#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize)]
pub struct IgnoredUserListEventContent { pub struct IgnoredUserListEventContent {
/// A list of users to ignore. /// A list of users to ignore.
#[serde(with = "vec_as_map_of_empty")]
pub ignored_users: Vec<UserId>, pub ignored_users: Vec<UserId>,
} }
@ -97,7 +98,10 @@ mod tests {
let json = serde_json::to_string(&ignored_user_list_event).unwrap(); let json = serde_json::to_string(&ignored_user_list_event).unwrap();
assert_eq!(json, r#"{"content":{"ignored_users":{"@carl:example.com":{}}},"type":"m.ignored_user_list"}"#); assert_eq!(
json,
r#"{"content":{"ignored_users":{"@carl:example.com":{}}},"type":"m.ignored_user_list"}"#
);
} }
#[test] #[test]

View File

@ -882,9 +882,9 @@ where
} }
} }
/// Serde serialization and deserialization functions that map a `Vec` to a /// Serde serialization and deserialization functions that map a `Vec<T>` to a `HashMap<T, Empty>`.
/// ///
/// To be used as `#[serde(with = "vec_as_map_of_empty")] /// To be used as `#[serde(with = "vec_as_map_of_empty")]`
mod vec_as_map_of_empty { mod vec_as_map_of_empty {
use super::Empty; use super::Empty;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};