From e7db5307820aad89aa81f7eed7cbc2f2783ee0e8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 4 Aug 2019 17:22:59 +0200 Subject: [PATCH] Add Default impls, ignore_all ctors to filter types --- src/r0/filter.rs | 52 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/r0/filter.rs b/src/r0/filter.rs index 50ffbae0..2de9de95 100644 --- a/src/r0/filter.rs +++ b/src/r0/filter.rs @@ -18,7 +18,7 @@ pub enum EventFormat { } /// Filters to be applied to room events -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct RoomEventFilter { /// A list of event types to exclude. /// @@ -66,8 +66,18 @@ pub struct RoomEventFilter { pub contains_url: Option, } +impl RoomEventFilter { + /// A filter that can be used to ignore all room events + pub fn ignore_all() -> Self { + Self { + types: Some(vec![]), + ..Default::default() + } + } +} + /// Filters to be applied to room data -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct RoomFilter { /// Include rooms that the user has left in the sync. /// @@ -102,8 +112,18 @@ pub struct RoomFilter { pub rooms: Option>, } +impl RoomFilter { + /// A filter that can be used to ignore all room events (of any type) + pub fn ignore_all() -> Self { + Self { + rooms: Some(vec![]), + ..Default::default() + } + } +} + /// Filter for not-room data -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct Filter { /// A list of event types to exclude. /// @@ -134,8 +154,18 @@ pub struct Filter { pub not_senders: Vec, } +impl Filter { + /// A filter that can be used to ignore all events + pub fn ignore_all() -> Self { + Self { + types: Some(vec![]), + ..Default::default() + } + } +} + /// A filter definition -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct FilterDefinition { /// List of event fields to include. /// @@ -148,7 +178,7 @@ pub struct FilterDefinition { /// The format to use for events. /// /// 'client' will return the events in a format suitable for clients. 'federation' will return - /// the raw event as receieved over federation. The default is 'client'. + /// the raw event as received over federation. The default is 'client'. #[serde(skip_serializing_if = "Option::is_none")] pub event_format: Option, /// The user account data that isn't associated with rooms to include. @@ -161,3 +191,15 @@ pub struct FilterDefinition { #[serde(skip_serializing_if = "Option::is_none")] pub presence: Option, } + +impl FilterDefinition { + /// A filter that can be used to ignore all events + pub fn ignore_all() -> Self { + Self { + account_data: Some(Filter::ignore_all()), + room: Some(RoomFilter::ignore_all()), + presence: Some(Filter::ignore_all()), + ..Default::default() + } + } +}