Add FromStr and TryFrom impls for m.ignored_user_list types.
This commit is contained in:
parent
b003699048
commit
bce1e91469
@ -1,11 +1,11 @@
|
|||||||
//! Types for the *m.ignored_user_list* event.
|
//! Types for the *m.ignored_user_list* event.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, convert::TryFrom, str::FromStr};
|
||||||
|
|
||||||
use ruma_identifiers::UserId;
|
use ruma_identifiers::UserId;
|
||||||
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
||||||
|
|
||||||
use crate::{Empty, Event, EventType, InvalidEvent};
|
use crate::{Empty, Event, EventType, InnerInvalidEvent, InvalidEvent};
|
||||||
|
|
||||||
/// A list of users to ignore.
|
/// A list of users to ignore.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
@ -21,10 +21,25 @@ pub struct IgnoredUserListEventContent {
|
|||||||
pub ignored_users: Vec<UserId>,
|
pub ignored_users: Vec<UserId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IgnoredUserListEvent {
|
impl FromStr for IgnoredUserListEvent {
|
||||||
|
type Err = InvalidEvent;
|
||||||
|
|
||||||
/// Attempt to create `Self` from parsing a string of JSON data.
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
pub fn from_str(json: &str) -> Result<Self, InvalidEvent> {
|
fn from_str(json: &str) -> Result<Self, InvalidEvent> {
|
||||||
let raw = serde_json::from_str::<raw::IgnoredUserListEvent>(json)?;
|
let raw = match serde_json::from_str::<raw::IgnoredUserListEvent>(json) {
|
||||||
|
Ok(raw) => raw,
|
||||||
|
Err(error) => match serde_json::from_str::<serde_json::Value>(json) {
|
||||||
|
Ok(value) => {
|
||||||
|
return Err(InvalidEvent(InnerInvalidEvent::Validation {
|
||||||
|
json: value,
|
||||||
|
message: error.to_string(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error }));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
content: IgnoredUserListEventContent {
|
content: IgnoredUserListEventContent {
|
||||||
@ -34,6 +49,15 @@ impl IgnoredUserListEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a str> for IgnoredUserListEvent {
|
||||||
|
type Error = InvalidEvent;
|
||||||
|
|
||||||
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
|
fn try_from(json: &'a str) -> Result<Self, Self::Error> {
|
||||||
|
FromStr::from_str(json)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Serialize for IgnoredUserListEvent {
|
impl Serialize for IgnoredUserListEvent {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@ -48,18 +72,44 @@ impl Serialize for IgnoredUserListEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Event for IgnoredUserListEvent {
|
impl_event!(
|
||||||
/// The type of this event's `content` field.
|
IgnoredUserListEvent,
|
||||||
type Content = IgnoredUserListEventContent;
|
IgnoredUserListEventContent,
|
||||||
|
EventType::IgnoredUserList
|
||||||
|
);
|
||||||
|
|
||||||
/// The event's content.
|
impl FromStr for IgnoredUserListEventContent {
|
||||||
fn content(&self) -> &Self::Content {
|
type Err = InvalidEvent;
|
||||||
&self.content
|
|
||||||
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
|
fn from_str(json: &str) -> Result<Self, Self::Err> {
|
||||||
|
let raw = match serde_json::from_str::<raw::IgnoredUserListEventContent>(json) {
|
||||||
|
Ok(raw) => raw,
|
||||||
|
Err(error) => match serde_json::from_str::<serde_json::Value>(json) {
|
||||||
|
Ok(value) => {
|
||||||
|
return Err(InvalidEvent(InnerInvalidEvent::Validation {
|
||||||
|
json: value,
|
||||||
|
message: error.to_string(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error }));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
ignored_users: raw.ignored_users.keys().cloned().collect(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The type of the event.
|
impl<'a> TryFrom<&'a str> for IgnoredUserListEventContent {
|
||||||
fn event_type(&self) -> EventType {
|
type Error = InvalidEvent;
|
||||||
EventType::IgnoredUserList
|
|
||||||
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
|
fn try_from(json: &'a str) -> Result<Self, Self::Error> {
|
||||||
|
FromStr::from_str(json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +174,7 @@ mod tests {
|
|||||||
fn deserialization() {
|
fn deserialization() {
|
||||||
let json = r#"{"content":{"ignored_users":{"@carl:example.com":{}}},"type":"m.ignored_user_list"}"#;
|
let json = r#"{"content":{"ignored_users":{"@carl:example.com":{}}},"type":"m.ignored_user_list"}"#;
|
||||||
|
|
||||||
let actual = IgnoredUserListEvent::from_str(json).unwrap();
|
let actual: IgnoredUserListEvent = json.parse().unwrap();
|
||||||
|
|
||||||
let expected = IgnoredUserListEvent {
|
let expected = IgnoredUserListEvent {
|
||||||
content: IgnoredUserListEventContent {
|
content: IgnoredUserListEventContent {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user