diff --git a/crates/ruma-events/src/lib.rs b/crates/ruma-events/src/lib.rs index fdd9c9c2..6e9ba9e4 100644 --- a/crates/ruma-events/src/lib.rs +++ b/crates/ruma-events/src/lib.rs @@ -117,7 +117,6 @@ use std::fmt::Debug; -use js_int::Int; use ruma_identifiers::{EventEncryptionAlgorithm, RoomVersionId}; use ruma_serde::Raw; use serde::{ @@ -130,6 +129,7 @@ use self::room::redaction::SyncRedactionEvent; mod enums; mod event_kinds; +mod unsigned; // Hack to allow both ruma-events itself and external crates (or tests) to use procedural macros // that expect `ruma_events` to exist in the prelude. @@ -188,80 +188,11 @@ pub mod typing; #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] pub use self::relation::Relations; -pub use self::{enums::*, event_kinds::*}; - -/// Extra information about an event that is not incorporated into the event's -/// hash. -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Unsigned { - /// The time in milliseconds that has elapsed since the event was sent. This - /// field is generated by the local homeserver, and may be incorrect if the - /// local time on at least one of the two servers is out of sync, which can - /// cause the age to either be negative or greater than it actually is. - #[serde(skip_serializing_if = "Option::is_none")] - pub age: Option, - - /// The client-supplied transaction ID, if the client being given the event - /// is the same one which sent it. - #[serde(skip_serializing_if = "Option::is_none")] - pub transaction_id: Option, - - /// Server-compiled information from other events relating to this event. - #[cfg(feature = "unstable-pre-spec")] - #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] - #[serde(rename = "m.relations", skip_serializing_if = "Option::is_none")] - pub relations: Option, -} - -impl Unsigned { - /// Create a new `Unsigned` with fields set to `None`. - pub fn new() -> Self { - Self::default() - } - - /// Whether this unsigned data is empty (all fields are `None`). - /// - /// This method is used to determine whether to skip serializing the - /// `unsigned` field in room events. Do not use it to determine whether - /// an incoming `unsigned` field was present - it could still have been - /// present but contained none of the known fields. - pub fn is_empty(&self) -> bool { - self.age.is_none() && self.transaction_id.is_none() - } -} - -/// Extra information about a redacted event that is not incorporated into the event's -/// hash. -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct RedactedUnsigned { - /// The event that redacted this event, if any. - #[serde(skip_serializing_if = "Option::is_none")] - pub redacted_because: Option>, -} - -impl RedactedUnsigned { - /// Create a new `RedactedUnsigned` with field set to `None`. - pub fn new() -> Self { - Self::default() - } - - /// Create a new `RedactedUnsigned` with the given redacted because. - pub fn new_because(redacted_because: Box) -> Self { - Self { redacted_because: Some(redacted_because) } - } - - /// Whether this unsigned data is empty (`redacted_because` is `None`). - /// - /// This method is used to determine whether to skip serializing the - /// `unsigned` field in redacted room events. Do not use it to determine whether - /// an incoming `unsigned` field was present - it could still have been - /// present but contained none of the known fields. - pub fn is_empty(&self) -> bool { - self.redacted_because.is_none() - } -} +pub use self::{ + enums::*, + event_kinds::*, + unsigned::{RedactedUnsigned, Unsigned}, +}; /// The base trait that all event content types implement. /// diff --git a/crates/ruma-events/src/unsigned.rs b/crates/ruma-events/src/unsigned.rs new file mode 100644 index 00000000..b8d38b52 --- /dev/null +++ b/crates/ruma-events/src/unsigned.rs @@ -0,0 +1,76 @@ +use js_int::Int; +use serde::{Deserialize, Serialize}; + +#[cfg(feature = "unstable-pre-spec")] +use crate::relation::Relations; +use crate::room::redaction::SyncRedactionEvent; + +/// Extra information about an event that is not incorporated into the event's hash. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct Unsigned { + /// The time in milliseconds that has elapsed since the event was sent. + /// + /// This field is generated by the local homeserver, and may be incorrect if the local time on + /// at least one of the two servers is out of sync, which can cause the age to either be + /// negative or greater than it actually is. + #[serde(skip_serializing_if = "Option::is_none")] + pub age: Option, + + /// The client-supplied transaction ID, if the client being given the event is the same one + /// which sent it. + #[serde(skip_serializing_if = "Option::is_none")] + pub transaction_id: Option, + + /// Server-compiled information from other events relating to this event. + #[cfg(feature = "unstable-pre-spec")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] + #[serde(rename = "m.relations", skip_serializing_if = "Option::is_none")] + pub relations: Option, +} + +impl Unsigned { + /// Create a new `Unsigned` with fields set to `None`. + pub fn new() -> Self { + Self::default() + } + + /// Whether this unsigned data is empty (all fields are `None`). + /// + /// This method is used to determine whether to skip serializing the `unsigned` field in room + /// events. Do not use it to determine whether an incoming `unsigned` field was present - it + /// could still have been present but contained none of the known fields. + pub fn is_empty(&self) -> bool { + self.age.is_none() && self.transaction_id.is_none() + } +} + +/// Extra information about a redacted event that is not incorporated into the event's hash. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct RedactedUnsigned { + /// The event that redacted this event, if any. + #[serde(skip_serializing_if = "Option::is_none")] + pub redacted_because: Option>, +} + +impl RedactedUnsigned { + /// Create a new `RedactedUnsigned` with field set to `None`. + pub fn new() -> Self { + Self::default() + } + + /// Create a new `RedactedUnsigned` with the given redacted because. + pub fn new_because(redacted_because: Box) -> Self { + Self { redacted_because: Some(redacted_because) } + } + + /// Whether this unsigned data is empty (`redacted_because` is `None`). + /// + /// This method is used to determine whether to skip serializing the `unsigned` field in + /// redacted room events. Do not use it to determine whether an incoming `unsigned` field + /// was present - it could still have been present but contained none of the known fields. + pub fn is_empty(&self) -> bool { + self.redacted_because.is_none() + } +}