Simplify deserialization
This commit is contained in:
parent
11841f91b8
commit
d120f77635
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
@ -75,9 +75,6 @@ struct Original {
|
|||||||
pub port: u16,
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `EventId`.
|
|
||||||
struct EventIdVisitor;
|
|
||||||
|
|
||||||
impl EventId {
|
impl EventId {
|
||||||
/// Attempts to generate an `EventId` for the given origin server with a localpart consisting
|
/// 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
|
/// 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
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedEventId;
|
||||||
type Value = EventId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix event ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
@ -40,9 +40,6 @@ pub struct RoomAliasId {
|
|||||||
port: u16,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `RoomAliasId`.
|
|
||||||
struct RoomAliasIdVisitor;
|
|
||||||
|
|
||||||
impl RoomAliasId {
|
impl RoomAliasId {
|
||||||
/// Returns a `Host` for the room alias ID, containing the server name (minus the port) of
|
/// Returns a `Host` for the room alias ID, containing the server name (minus the port) of
|
||||||
/// the originating homeserver.
|
/// the originating homeserver.
|
||||||
@ -83,7 +80,10 @@ impl<'de> Deserialize<'de> for RoomAliasId {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedRoomAliasId;
|
||||||
type Value = RoomAliasId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix room alias ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
@ -40,9 +40,6 @@ pub struct RoomId {
|
|||||||
port: u16,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `RoomId`.
|
|
||||||
struct RoomIdVisitor;
|
|
||||||
|
|
||||||
impl RoomId {
|
impl RoomId {
|
||||||
/// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of
|
/// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of
|
||||||
/// 18 random ASCII characters.
|
/// 18 random ASCII characters.
|
||||||
@ -98,7 +95,10 @@ impl<'de> Deserialize<'de> for RoomId {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedRoomId;
|
||||||
type Value = RoomId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix room ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,9 +43,6 @@ pub enum RoomIdOrAliasId {
|
|||||||
RoomId(RoomId),
|
RoomId(RoomId),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `RoomIdOrAliasId`.
|
|
||||||
struct RoomIdOrAliasIdVisitor;
|
|
||||||
|
|
||||||
impl Display for RoomIdOrAliasId {
|
impl Display for RoomIdOrAliasId {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||||
match *self {
|
match *self {
|
||||||
@ -86,7 +83,11 @@ impl<'de> Deserialize<'de> for RoomIdOrAliasId {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedRoomIdOrAliasId;
|
||||||
type Value = RoomIdOrAliasId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix room ID or room alias ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,9 +55,6 @@ enum InnerRoomVersionId {
|
|||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `RoomVersionId`.
|
|
||||||
struct RoomVersionIdVisitor;
|
|
||||||
|
|
||||||
impl RoomVersionId {
|
impl RoomVersionId {
|
||||||
/// Creates a version 1 room ID.
|
/// Creates a version 1 room ID.
|
||||||
pub fn version_1() -> Self {
|
pub fn version_1() -> Self {
|
||||||
@ -157,7 +154,10 @@ impl<'de> Deserialize<'de> for RoomVersionId {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedRoomVersionId;
|
||||||
type Value = RoomVersionId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix room version ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
@ -8,7 +8,7 @@ use std::{
|
|||||||
#[cfg(feature = "diesel")]
|
#[cfg(feature = "diesel")]
|
||||||
use diesel::sql_types::Text;
|
use diesel::sql_types::Text;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{Error as SerdeError, Unexpected, Visitor},
|
de::{Error as SerdeError, Expected, Unexpected},
|
||||||
Deserialize, Deserializer, Serialize, Serializer,
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
};
|
};
|
||||||
use url::Host;
|
use url::Host;
|
||||||
@ -40,9 +40,6 @@ pub struct UserId {
|
|||||||
port: u16,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A serde visitor for `UserId`.
|
|
||||||
struct UserIdVisitor;
|
|
||||||
|
|
||||||
impl UserId {
|
impl UserId {
|
||||||
/// Attempts to generate a `UserId` for the given origin server with a localpart consisting of
|
/// Attempts to generate a `UserId` for the given origin server with a localpart consisting of
|
||||||
/// 12 random ASCII characters.
|
/// 12 random ASCII characters.
|
||||||
@ -102,7 +99,10 @@ impl<'de> Deserialize<'de> for UserId {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
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 {
|
struct ExpectedUserId;
|
||||||
type Value = UserId;
|
|
||||||
|
|
||||||
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")
|
write!(formatter, "a Matrix user ID as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user