Abstract Pdu filter matching into trait Event.
Abstract Pdu unsigned accessors into trait Event. Abstract Pdu relation related into trait Event. Abstract PDU content into trait Event. Move event_id utils from pdu to event. Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
mod content;
|
||||
mod filter;
|
||||
mod format;
|
||||
mod id;
|
||||
mod redact;
|
||||
mod relation;
|
||||
mod type_ext;
|
||||
mod unsigned;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use ruma::{
|
||||
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, RoomVersionId, UserId,
|
||||
events::TimelineEventType,
|
||||
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId,
|
||||
RoomVersionId, UserId, events::TimelineEventType,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value as JsonValue, value::RawValue as RawJsonValue};
|
||||
|
||||
pub use self::type_ext::TypeExt;
|
||||
use super::state_key::StateKey;
|
||||
use crate::Result;
|
||||
pub use self::{filter::Matches, id::*, relation::RelationTypeEqual, type_ext::TypeExt};
|
||||
use super::{pdu::Pdu, state_key::StateKey};
|
||||
use crate::{Result, utils};
|
||||
|
||||
/// Abstraction of a PDU so users can have their own PDU types.
|
||||
pub trait Event {
|
||||
pub trait Event: Clone + Debug {
|
||||
/// Serialize into a Ruma JSON format, consuming.
|
||||
#[inline]
|
||||
fn into_format<T>(self) -> T
|
||||
@@ -36,6 +42,41 @@ pub trait Event {
|
||||
format::Ref(self).into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn contains_unsigned_property<T>(&self, property: &str, is_type: T) -> bool
|
||||
where
|
||||
T: FnOnce(&JsonValue) -> bool,
|
||||
Self: Sized,
|
||||
{
|
||||
unsigned::contains_unsigned_property::<T, _>(self, property, is_type)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_unsigned_property<T>(&self, property: &str) -> Result<T>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
Self: Sized,
|
||||
{
|
||||
unsigned::get_unsigned_property::<T, _>(self, property)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_unsigned_as_value(&self) -> JsonValue
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
unsigned::get_unsigned_as_value(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_unsigned<T>(&self) -> Result<T>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
Self: Sized,
|
||||
{
|
||||
unsigned::get_unsigned::<T, _>(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_content_as_value(&self) -> JsonValue
|
||||
where
|
||||
@@ -69,6 +110,39 @@ pub trait Event {
|
||||
redact::is_redacted(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_canonical_object(self) -> CanonicalJsonObject
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
utils::to_canonical_object(self.into_pdu()).expect("failed to create Value::Object")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn to_canonical_object(&self) -> CanonicalJsonObject {
|
||||
utils::to_canonical_object(self.as_pdu()).expect("failed to create Value::Object")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_value(self) -> JsonValue
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
serde_json::to_value(self.into_pdu()).expect("failed to create JSON Value")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn to_value(&self) -> JsonValue {
|
||||
serde_json::to_value(self.as_pdu()).expect("failed to create JSON Value")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_mut_pdu(&mut self) -> &mut Pdu { unimplemented!("not a mutable Pdu") }
|
||||
|
||||
fn as_pdu(&self) -> &Pdu;
|
||||
|
||||
fn into_pdu(self) -> Pdu;
|
||||
|
||||
fn is_owned(&self) -> bool;
|
||||
|
||||
//
|
||||
@@ -76,7 +150,7 @@ pub trait Event {
|
||||
//
|
||||
|
||||
/// All the authenticating events for this event.
|
||||
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Send + '_;
|
||||
fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
|
||||
|
||||
/// The event's content.
|
||||
fn content(&self) -> &RawJsonValue;
|
||||
@@ -88,7 +162,7 @@ pub trait Event {
|
||||
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
|
||||
|
||||
/// The events before this event.
|
||||
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Send + '_;
|
||||
fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
|
||||
|
||||
/// If this event is a redaction event this is the event it redacts.
|
||||
fn redacts(&self) -> Option<&EventId>;
|
||||
|
||||
Reference in New Issue
Block a user