Implement FromStr for content types.
This commit is contained in:
parent
8c8f1790f1
commit
7d1701ccce
@ -147,6 +147,39 @@ impl_state_event!(
|
|||||||
EventType::RoomCanonicalAlias
|
EventType::RoomCanonicalAlias
|
||||||
);
|
);
|
||||||
|
|
||||||
|
impl FromStr for CanonicalAliasEventContent {
|
||||||
|
type Err = InvalidEvent;
|
||||||
|
|
||||||
|
/// 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::CanonicalAliasEventContent>(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 { alias: raw.alias })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a str> for CanonicalAliasEventContent {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod raw {
|
mod raw {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -163,6 +163,39 @@ impl NameEventContent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for NameEventContent {
|
||||||
|
type Err = InvalidEvent;
|
||||||
|
|
||||||
|
/// 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::NameEventContent>(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 { name: raw.name })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a str> for NameEventContent {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod raw {
|
mod raw {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -202,6 +202,50 @@ impl_state_event!(
|
|||||||
EventType::RoomPowerLevels
|
EventType::RoomPowerLevels
|
||||||
);
|
);
|
||||||
|
|
||||||
|
impl FromStr for PowerLevelsEventContent {
|
||||||
|
type Err = InvalidEvent;
|
||||||
|
|
||||||
|
/// 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::PowerLevelsEventContent>(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 {
|
||||||
|
ban: raw.ban,
|
||||||
|
events: raw.events,
|
||||||
|
events_default: raw.events_default,
|
||||||
|
invite: raw.invite,
|
||||||
|
kick: raw.kick,
|
||||||
|
redact: raw.redact,
|
||||||
|
state_default: raw.state_default,
|
||||||
|
users: raw.users,
|
||||||
|
users_default: raw.users_default,
|
||||||
|
notifications: raw.notifications,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TryFrom<&'a str> for PowerLevelsEventContent {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod raw {
|
mod raw {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -133,61 +133,46 @@ impl Serialize for ServerAclEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for ServerAclEvent {
|
impl_state_event!(
|
||||||
/// The type of this event's `content` field.
|
ServerAclEvent,
|
||||||
type Content = ServerAclEventContent;
|
ServerAclEventContent,
|
||||||
|
EventType::RoomServerAcl
|
||||||
|
);
|
||||||
|
|
||||||
/// The event's content.
|
impl FromStr for ServerAclEventContent {
|
||||||
fn content(&self) -> &Self::Content {
|
type Err = InvalidEvent;
|
||||||
&self.content
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The type of the event.
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
fn event_type(&self) -> EventType {
|
fn from_str(json: &str) -> Result<Self, Self::Err> {
|
||||||
EventType::RoomServerAcl
|
let raw = match serde_json::from_str::<raw::ServerAclEventContent>(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 {
|
||||||
|
allow_ip_literals: raw.allow_ip_literals,
|
||||||
|
allow: raw.allow,
|
||||||
|
deny: raw.deny,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RoomEvent for ServerAclEvent {
|
impl<'a> TryFrom<&'a str> for ServerAclEventContent {
|
||||||
/// The unique identifier for the event.
|
type Error = InvalidEvent;
|
||||||
fn event_id(&self) -> &EventId {
|
|
||||||
&self.event_id
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this event was
|
/// Attempt to create `Self` from parsing a string of JSON data.
|
||||||
/// sent.
|
fn try_from(json: &'a str) -> Result<Self, Self::Error> {
|
||||||
fn origin_server_ts(&self) -> UInt {
|
FromStr::from_str(json)
|
||||||
self.origin_server_ts
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The unique identifier for the room associated with this event.
|
|
||||||
///
|
|
||||||
/// This can be `None` if the event came from a context where there is
|
|
||||||
/// no ambiguity which room it belongs to, like a `/sync` response for example.
|
|
||||||
fn room_id(&self) -> Option<&RoomId> {
|
|
||||||
self.room_id.as_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The unique identifier for the user who sent this event.
|
|
||||||
fn sender(&self) -> &UserId {
|
|
||||||
&self.sender
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Additional key-value pairs not signed by the homeserver.
|
|
||||||
fn unsigned(&self) -> Option<&Value> {
|
|
||||||
self.unsigned.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StateEvent for ServerAclEvent {
|
|
||||||
/// The previous content for this state key, if any.
|
|
||||||
fn prev_content(&self) -> Option<&Self::Content> {
|
|
||||||
self.prev_content.as_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A key that determines which piece of room state the event represents.
|
|
||||||
fn state_key(&self) -> &str {
|
|
||||||
&self.state_key
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user