#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
#![recursion_limit = "1024"]
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use std::fmt;
use ruma_identifiers::{EventEncryptionAlgorithm, RoomVersionId};
use ruma_serde::Raw;
use serde::{de::IgnoredAny, Deserialize, Serialize, Serializer};
use serde_json::value::RawValue as RawJsonValue;
use self::room::redaction::SyncRoomRedactionEvent;
#[doc(hidden)]
pub mod _custom;
mod enums;
mod event_kinds;
mod unsigned;
extern crate self as ruma_events;
#[doc(hidden)]
pub mod exports {
pub use ruma_common;
pub use ruma_identifiers;
pub use ruma_serde;
pub use serde;
pub use serde_json;
}
pub mod macros {
pub use ruma_events_macros::{Event, EventContent};
}
pub mod call;
pub mod direct;
pub mod dummy;
pub mod forwarded_room_key;
pub mod fully_read;
pub mod ignored_user_list;
pub mod key;
#[cfg(feature = "unstable-pdu")]
pub mod pdu;
pub mod policy;
pub mod presence;
pub mod push_rules;
#[cfg(feature = "unstable-msc2677")]
pub mod reaction;
pub mod receipt;
#[cfg(feature = "unstable-msc2675")]
pub mod relation;
pub mod room;
pub mod room_key;
pub mod room_key_request;
pub mod secret;
pub mod space;
pub mod sticker;
pub mod tag;
pub mod typing;
#[cfg(feature = "unstable-msc2675")]
pub use self::relation::Relations;
#[doc(hidden)]
#[cfg(feature = "compat")]
pub use self::unsigned::{RedactedUnsignedWithPrevContent, UnsignedWithPrevContent};
pub use self::{
enums::*,
event_kinds::*,
unsigned::{RedactedUnsigned, Unsigned},
};
pub trait EventContent: Sized + Serialize {
fn event_type(&self) -> &str;
fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result<Self>;
}
pub trait Redact {
type Redacted;
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self::Redacted;
}
pub trait RedactContent {
type Redacted;
fn redact(self, version: &RoomVersionId) -> Self::Redacted;
}
pub trait RawExt<T: EventContent> {
fn deserialize_content(&self, event_type: &str) -> serde_json::Result<T>;
}
impl<T: EventContent> RawExt<T> for Raw<T> {
fn deserialize_content(&self, event_type: &str) -> serde_json::Result<T> {
T::from_parts(event_type, self.json())
}
}
pub trait EphemeralRoomEventContent: EventContent {}
pub trait GlobalAccountDataEventContent: EventContent {}
pub trait RoomAccountDataEventContent: EventContent {}
pub trait ToDeviceEventContent: EventContent {}
pub trait MessageEventContent: EventContent {}
pub trait StateEventContent: EventContent {}
pub trait RedactedEventContent: EventContent {
#[doc(hidden)]
fn empty(_event_type: &str) -> serde_json::Result<Self> {
Err(serde::de::Error::custom("this event is not redacted"))
}
#[doc(hidden)]
fn has_serialize_fields(&self) -> bool;
#[doc(hidden)]
fn has_deserialize_fields() -> HasDeserializeFields;
}
pub trait RedactedMessageEventContent: RedactedEventContent {}
pub trait RedactedStateEventContent: RedactedEventContent {}
pub trait StaticEventContent: EventContent {
const KIND: EventKind;
const TYPE: &'static str;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum EventKind {
GlobalAccountData,
RoomAccountData,
EphemeralRoomData,
Message {
redacted: bool,
},
State {
redacted: bool,
},
ToDevice,
Presence,
}
#[doc(hidden)]
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
pub enum HasDeserializeFields {
True,
False,
Optional,
}
#[doc(hidden)]
#[derive(Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct EventTypeDeHelper<'a> {
#[serde(borrow, rename = "type")]
pub ev_type: std::borrow::Cow<'a, str>,
}
#[doc(hidden)]
#[derive(Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct RedactionDeHelper {
pub unsigned: Option<UnsignedDeHelper>,
}
#[doc(hidden)]
#[derive(Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct UnsignedDeHelper {
pub redacted_because: Option<IgnoredAny>,
}
#[doc(hidden)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrivOwnedStr(Box<str>);
impl fmt::Debug for PrivOwnedStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[doc(hidden)]
#[allow(clippy::ptr_arg)]
pub fn serialize_custom_event_error<T, S: Serializer>(_: &T, _: S) -> Result<S::Ok, S::Error> {
Err(serde::ser::Error::custom(
"Failed to serialize event [content] enum: Unknown event type.\n\
To send custom events, turn them into `Raw<EnumType>` by going through
`serde_json::value::to_raw_value` and `Raw::from_json`.",
))
}