Extract enum serialization into a macro.
This commit is contained in:
parent
e8fba65f84
commit
1e1079a904
@ -2,13 +2,6 @@
|
|||||||
//!
|
//!
|
||||||
//! This module also contains types shared by events in its child namespaces.
|
//! This module also contains types shared by events in its child namespaces.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {ParseError, Visitor};
|
|
||||||
|
|
||||||
pub mod answer;
|
pub mod answer;
|
||||||
pub mod candidates;
|
pub mod candidates;
|
||||||
pub mod hangup;
|
pub mod hangup;
|
||||||
@ -32,65 +25,9 @@ pub enum SessionDescriptionType {
|
|||||||
Offer,
|
Offer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SessionDescriptionType {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
SessionDescriptionType {
|
||||||
let session_description_type_str = match *self {
|
Answer => "answer",
|
||||||
SessionDescriptionType::Answer => "answer",
|
Offer => "offer",
|
||||||
SessionDescriptionType::Offer => "offer",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", session_description_type_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for SessionDescriptionType {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"answer" => Ok(SessionDescriptionType::Answer),
|
|
||||||
"offer" => Ok(SessionDescriptionType::Offer),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for SessionDescriptionType {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for SessionDescriptionType {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::SessionDescriptionType;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn session_description_types_serialize_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&SessionDescriptionType::Answer).unwrap(),
|
|
||||||
r#""answer""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn session_description_types_deserialize_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<SessionDescriptionType>(r#""answer""#).unwrap(),
|
|
||||||
SessionDescriptionType::Answer
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_session_description_types_fail_deserialization() {
|
|
||||||
assert!(from_str::<SessionDescriptionType>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,9 @@ use serde::{Deserialize, Deserializer, Error as SerdeError, Serialize, Serialize
|
|||||||
use serde::de::Visitor as SerdeVisitor;
|
use serde::de::Visitor as SerdeVisitor;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
pub mod call;
|
pub mod call;
|
||||||
pub mod presence;
|
pub mod presence;
|
||||||
pub mod receipt;
|
pub mod receipt;
|
||||||
|
60
src/macros.rs
Normal file
60
src/macros.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
macro_rules! impl_enum {
|
||||||
|
($name:ident { $($variant:ident => $s:expr,)+ }) => {
|
||||||
|
impl ::std::fmt::Display for $name {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||||
|
let variant = match *self {
|
||||||
|
$($name::$variant => $s,)*
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(f, "{}", variant)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::str::FromStr for $name {
|
||||||
|
type Err = $crate::ParseError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
$($s => Ok($name::$variant),)*
|
||||||
|
_ => Err($crate::ParseError),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::serde::Serialize for $name {
|
||||||
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
|
where S: ::serde::Serializer {
|
||||||
|
serializer.serialize_str(&self.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::serde::Deserialize for $name {
|
||||||
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||||
|
where D: ::serde::Deserializer {
|
||||||
|
deserializer.deserialize_str($crate::Visitor::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod serialization_tests {
|
||||||
|
use serde_json::{from_str, to_string};
|
||||||
|
|
||||||
|
use super::$name;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialization_to_display_form() {
|
||||||
|
$(assert_eq!(to_string(&$name::$variant).unwrap(), stringify!($s));)*
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialization_from_display_form() {
|
||||||
|
$(assert_eq!(from_str::<$name>(stringify!($s)).unwrap(), $name::$variant);)*
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialization_fails_for_invalid_string_value() {
|
||||||
|
assert!(from_str::<$name>(r#""invalid variant name""#).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,8 @@
|
|||||||
//! Types for the *m.presence* event.
|
//! Types for the *m.presence* event.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use ruma_identifiers::{EventId, UserId};
|
use ruma_identifiers::{EventId, UserId};
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {Event, ParseError, Visitor};
|
use Event;
|
||||||
|
|
||||||
/// Informs the client of a user's presence state change.
|
/// Informs the client of a user's presence state change.
|
||||||
pub type PresenceEvent = Event<PresenceEventContent, PresenceEventExtraContent>;
|
pub type PresenceEvent = Event<PresenceEventContent, PresenceEventExtraContent>;
|
||||||
@ -53,67 +49,10 @@ pub struct PresenceEventExtraContent {
|
|||||||
pub event_id: EventId,
|
pub event_id: EventId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for PresenceState {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
PresenceState {
|
||||||
let presence_state_str = match *self {
|
Offline => "offline",
|
||||||
PresenceState::Offline => "offline",
|
Online => "online",
|
||||||
PresenceState::Online => "online",
|
Unavailable => "unavailable",
|
||||||
PresenceState::Unavailable => "unavailable",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", presence_state_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PresenceState {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"offline" => Ok(PresenceState::Offline),
|
|
||||||
"online" => Ok(PresenceState::Online),
|
|
||||||
"unavailable" => Ok(PresenceState::Unavailable),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for PresenceState {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for PresenceState {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::PresenceState;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn presence_states_serialize_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&PresenceState::Offline).unwrap(),
|
|
||||||
r#""offline""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn presence_states_deserialize_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<PresenceState>(r#""offline""#).unwrap(),
|
|
||||||
PresenceState::Offline
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_presence_states_fail_deserialization() {
|
|
||||||
assert!(from_str::<PresenceState>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
//! Types for the *m.room.guest_access* event.
|
//! Types for the *m.room.guest_access* event.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
use StateEvent;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {StateEvent, ParseError, Visitor};
|
|
||||||
|
|
||||||
/// Controls whether guest users are allowed to join rooms.
|
/// Controls whether guest users are allowed to join rooms.
|
||||||
///
|
///
|
||||||
@ -30,65 +25,9 @@ pub enum GuestAccess {
|
|||||||
Forbidden,
|
Forbidden,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for GuestAccess {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
GuestAccess {
|
||||||
let guest_access_str = match *self {
|
CanJoin => "can_join",
|
||||||
GuestAccess::CanJoin => "can_join",
|
Forbidden => "forbidden",
|
||||||
GuestAccess::Forbidden => "forbidden",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", guest_access_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for GuestAccess {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"can_join" => Ok(GuestAccess::CanJoin),
|
|
||||||
"forbidden" => Ok(GuestAccess::Forbidden),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for GuestAccess {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for GuestAccess {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::GuestAccess;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn guest_access_serializes_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&GuestAccess::CanJoin).unwrap(),
|
|
||||||
r#""can_join""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn guest_access_deserializes_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<GuestAccess>(r#""can_join""#).unwrap(),
|
|
||||||
GuestAccess::CanJoin
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_guest_access_fails_deserialization() {
|
|
||||||
assert!(from_str::<GuestAccess>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
//! Types for the *m.room.history_visibility* event.
|
//! Types for the *m.room.history_visibility* event.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
use StateEvent;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {StateEvent, ParseError, Visitor};
|
|
||||||
|
|
||||||
/// This event controls whether a member of a room can see the events that happened in a room from
|
/// This event controls whether a member of a room can see the events that happened in a room from
|
||||||
/// before they joined.
|
/// before they joined.
|
||||||
@ -40,69 +35,11 @@ pub enum HistoryVisibility {
|
|||||||
WorldReadable,
|
WorldReadable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for HistoryVisibility {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
HistoryVisibility {
|
||||||
let history_visibility_str = match *self {
|
Invited => "invited",
|
||||||
HistoryVisibility::Invited => "invited",
|
Joined => "joined",
|
||||||
HistoryVisibility::Joined => "joined",
|
Shared => "shared",
|
||||||
HistoryVisibility::Shared => "shared",
|
WorldReadable => "world_readable",
|
||||||
HistoryVisibility::WorldReadable => "world_readable",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", history_visibility_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for HistoryVisibility {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"invited" => Ok(HistoryVisibility::Invited),
|
|
||||||
"joined" => Ok(HistoryVisibility::Joined),
|
|
||||||
"shared" => Ok(HistoryVisibility::Shared),
|
|
||||||
"world_readable" => Ok(HistoryVisibility::WorldReadable),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for HistoryVisibility {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for HistoryVisibility {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::HistoryVisibility;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn history_visibility_serializes_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&HistoryVisibility::Invited).unwrap(),
|
|
||||||
r#""invited""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn history_visibility_deserializes_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<HistoryVisibility>(r#""invited""#).unwrap(),
|
|
||||||
HistoryVisibility::Invited
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_history_visibility_fails_deserialization() {
|
|
||||||
assert!(from_str::<HistoryVisibility>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
//! Types for the *m.room.join_rules* event.
|
//! Types for the *m.room.join_rules* event.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
use StateEvent;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {StateEvent, ParseError, Visitor};
|
|
||||||
|
|
||||||
/// Describes how users are allowed to join the room.
|
/// Describes how users are allowed to join the room.
|
||||||
pub type JoinRulesEvent = StateEvent<JoinRulesEventContent, ()>;
|
pub type JoinRulesEvent = StateEvent<JoinRulesEventContent, ()>;
|
||||||
@ -34,69 +29,11 @@ pub enum JoinRule {
|
|||||||
Public,
|
Public,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for JoinRule {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
JoinRule {
|
||||||
let join_rule_str = match *self {
|
Invite => "invite",
|
||||||
JoinRule::Invite => "invite",
|
Knock => "knock",
|
||||||
JoinRule::Knock => "knock",
|
Private => "private",
|
||||||
JoinRule::Private => "private",
|
Public => "public",
|
||||||
JoinRule::Public => "public",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", join_rule_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for JoinRule {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"invite" => Ok(JoinRule::Invite),
|
|
||||||
"knock" => Ok(JoinRule::Knock),
|
|
||||||
"private" => Ok(JoinRule::Private),
|
|
||||||
"public" => Ok(JoinRule::Public),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for JoinRule {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for JoinRule {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::JoinRule;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn join_rules_serialize_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&JoinRule::Invite).unwrap(),
|
|
||||||
r#""invite""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn join_rules_deserialize_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<JoinRule>(r#""invite""#).unwrap(),
|
|
||||||
JoinRule::Invite
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_join_rules_fail_deserialization() {
|
|
||||||
assert!(from_str::<JoinRule>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
//! Types for the *m.room.member* event.
|
//! Types for the *m.room.member* event.
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
use StateEvent;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use {StateEvent, ParseError, Visitor};
|
|
||||||
use stripped::StrippedState;
|
use stripped::StrippedState;
|
||||||
|
|
||||||
/// The current membership state of a user in the room.
|
/// The current membership state of a user in the room.
|
||||||
@ -65,71 +60,12 @@ pub struct MemberEventExtraContent {
|
|||||||
pub invite_room_state: Option<Vec<StrippedState>>,
|
pub invite_room_state: Option<Vec<StrippedState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for MembershipState {
|
impl_enum! {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
MembershipState {
|
||||||
let membership_state_str = match *self {
|
Ban => "ban",
|
||||||
MembershipState::Ban => "ban",
|
Invite => "invite",
|
||||||
MembershipState::Invite => "invite",
|
Join => "join",
|
||||||
MembershipState::Join => "join",
|
Knock => "knock",
|
||||||
MembershipState::Knock => "knock",
|
Leave => "leave",
|
||||||
MembershipState::Leave => "leave",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", membership_state_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for MembershipState {
|
|
||||||
type Err = ParseError;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"ban" => Ok(MembershipState::Ban),
|
|
||||||
"invite" => Ok(MembershipState::Invite),
|
|
||||||
"join" => Ok(MembershipState::Join),
|
|
||||||
"knock" => Ok(MembershipState::Knock),
|
|
||||||
"leave" => Ok(MembershipState::Leave),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for MembershipState {
|
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deserialize for MembershipState {
|
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer {
|
|
||||||
deserializer.deserialize_str(Visitor::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use serde_json::{from_str, to_string};
|
|
||||||
|
|
||||||
use super::MembershipState;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn membership_states_serialize_to_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
to_string(&MembershipState::Ban).unwrap(),
|
|
||||||
r#""ban""#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn membership_states_deserialize_from_display_form() {
|
|
||||||
assert_eq!(
|
|
||||||
from_str::<MembershipState>(r#""ban""#).unwrap(),
|
|
||||||
MembershipState::Ban
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn invalid_membership_states_fail_deserialization() {
|
|
||||||
assert!(from_str::<MembershipState>(r#""bad""#).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user