diff --git a/src/event_id.rs b/src/event_id.rs index 022449f9..fc582526 100644 --- a/src/event_id.rs +++ b/src/event_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; use url::Host; @@ -75,9 +75,6 @@ struct Original { pub port: u16, } -/// A serde visitor for `EventId`. -struct EventIdVisitor; - impl EventId { /// Attempts to generate an `EventId` for the given origin server with a localpart consisting /// of 18 random ASCII characters. This should only be used for events in the original format @@ -158,7 +155,10 @@ impl<'de> Deserialize<'de> for EventId { where D: Deserializer<'de>, { - deserializer.deserialize_any(EventIdVisitor) + String::deserialize(deserializer).and_then(|v| { + EventId::try_from(&v as &str) + .map_err(|_| SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedEventId)) + }) } } @@ -189,22 +189,12 @@ impl<'a> TryFrom<&'a str> for EventId { } } -impl<'de> Visitor<'de> for EventIdVisitor { - type Value = EventId; +struct ExpectedEventId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedEventId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix event ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match EventId::try_from(v) { - Ok(event_id) => Ok(event_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)] diff --git a/src/room_alias_id.rs b/src/room_alias_id.rs index 52cd06f4..5267e1e1 100644 --- a/src/room_alias_id.rs +++ b/src/room_alias_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; use url::Host; @@ -40,9 +40,6 @@ pub struct RoomAliasId { port: u16, } -/// A serde visitor for `RoomAliasId`. -struct RoomAliasIdVisitor; - impl RoomAliasId { /// Returns a `Host` for the room alias ID, containing the server name (minus the port) of /// the originating homeserver. @@ -83,7 +80,10 @@ impl<'de> Deserialize<'de> for RoomAliasId { where D: Deserializer<'de>, { - deserializer.deserialize_any(RoomAliasIdVisitor) + String::deserialize(deserializer).and_then(|v| { + RoomAliasId::try_from(&v as &str) + .map_err(|_| SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedRoomAliasId)) + }) } } @@ -105,22 +105,12 @@ impl<'a> TryFrom<&'a str> for RoomAliasId { } } -impl<'de> Visitor<'de> for RoomAliasIdVisitor { - type Value = RoomAliasId; +struct ExpectedRoomAliasId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedRoomAliasId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix room alias ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match RoomAliasId::try_from(v) { - Ok(room_alias_id) => Ok(room_alias_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)] diff --git a/src/room_id.rs b/src/room_id.rs index c28dbe05..09de6ca2 100644 --- a/src/room_id.rs +++ b/src/room_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; use url::Host; @@ -40,9 +40,6 @@ pub struct RoomId { port: u16, } -/// A serde visitor for `RoomId`. -struct RoomIdVisitor; - impl RoomId { /// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of /// 18 random ASCII characters. @@ -98,7 +95,10 @@ impl<'de> Deserialize<'de> for RoomId { where D: Deserializer<'de>, { - deserializer.deserialize_any(RoomIdVisitor) + String::deserialize(deserializer).and_then(|v| { + RoomId::try_from(&v as &str) + .map_err(|_| SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedRoomId)) + }) } } @@ -120,22 +120,12 @@ impl<'a> TryFrom<&'a str> for RoomId { } } -impl<'de> Visitor<'de> for RoomIdVisitor { - type Value = RoomId; +struct ExpectedRoomId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedRoomId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix room ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match RoomId::try_from(v) { - Ok(room_id) => Ok(room_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)] diff --git a/src/room_id_or_room_alias_id.rs b/src/room_id_or_room_alias_id.rs index 3a2d7563..f453131b 100644 --- a/src/room_id_or_room_alias_id.rs +++ b/src/room_id_or_room_alias_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; @@ -43,9 +43,6 @@ pub enum RoomIdOrAliasId { RoomId(RoomId), } -/// A serde visitor for `RoomIdOrAliasId`. -struct RoomIdOrAliasIdVisitor; - impl Display for RoomIdOrAliasId { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match *self { @@ -86,7 +83,11 @@ impl<'de> Deserialize<'de> for RoomIdOrAliasId { where D: Deserializer<'de>, { - deserializer.deserialize_any(RoomIdOrAliasIdVisitor) + String::deserialize(deserializer).and_then(|v| { + RoomIdOrAliasId::try_from(&v as &str).map_err(|_| { + SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedRoomIdOrAliasId) + }) + }) } } @@ -119,22 +120,12 @@ impl<'a> TryFrom<&'a str> for RoomIdOrAliasId { } } -impl<'de> Visitor<'de> for RoomIdOrAliasIdVisitor { - type Value = RoomIdOrAliasId; +struct ExpectedRoomIdOrAliasId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedRoomIdOrAliasId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix room ID or room alias ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match RoomIdOrAliasId::try_from(v) { - Ok(room_id_or_alias_id) => Ok(room_id_or_alias_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)] diff --git a/src/room_version_id.rs b/src/room_version_id.rs index 3928c91f..44b59c2a 100644 --- a/src/room_version_id.rs +++ b/src/room_version_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; @@ -55,9 +55,6 @@ enum InnerRoomVersionId { Custom(String), } -/// A serde visitor for `RoomVersionId`. -struct RoomVersionIdVisitor; - impl RoomVersionId { /// Creates a version 1 room ID. pub fn version_1() -> Self { @@ -157,7 +154,10 @@ impl<'de> Deserialize<'de> for RoomVersionId { where D: Deserializer<'de>, { - deserializer.deserialize_any(RoomVersionIdVisitor) + String::deserialize(deserializer).and_then(|v| { + RoomVersionId::try_from(&v as &str) + .map_err(|_| SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedRoomVersionId)) + }) } } @@ -187,22 +187,12 @@ impl<'a> TryFrom<&'a str> for RoomVersionId { } } -impl<'de> Visitor<'de> for RoomVersionIdVisitor { - type Value = RoomVersionId; +struct ExpectedRoomVersionId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedRoomVersionId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix room version ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match RoomVersionId::try_from(v) { - Ok(room_id) => Ok(room_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)] diff --git a/src/user_id.rs b/src/user_id.rs index 255ae416..005ecf21 100644 --- a/src/user_id.rs +++ b/src/user_id.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(feature = "diesel")] use diesel::sql_types::Text; use serde::{ - de::{Error as SerdeError, Unexpected, Visitor}, + de::{Error as SerdeError, Expected, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; use url::Host; @@ -40,9 +40,6 @@ pub struct UserId { port: u16, } -/// A serde visitor for `UserId`. -struct UserIdVisitor; - impl UserId { /// Attempts to generate a `UserId` for the given origin server with a localpart consisting of /// 12 random ASCII characters. @@ -102,7 +99,10 @@ impl<'de> Deserialize<'de> for UserId { where D: Deserializer<'de>, { - deserializer.deserialize_any(UserIdVisitor) + String::deserialize(deserializer).and_then(|v| { + UserId::try_from(&v as &str) + .map_err(|_| SerdeError::invalid_value(Unexpected::Str(&v), &ExpectedUserId)) + }) } } @@ -133,22 +133,12 @@ impl<'a> TryFrom<&'a str> for UserId { } } -impl<'de> Visitor<'de> for UserIdVisitor { - type Value = UserId; +struct ExpectedUserId; - fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult { +impl Expected for ExpectedUserId { + fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult { write!(formatter, "a Matrix user ID as a string") } - - fn visit_str(self, v: &str) -> Result - where - E: SerdeError, - { - match UserId::try_from(v) { - Ok(user_id) => Ok(user_id), - Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)), - } - } } #[cfg(test)]