diff --git a/ruma-client-api/src/r0/context/get_context.rs b/ruma-client-api/src/r0/context/get_context.rs index 38f5833e..94df7efe 100644 --- a/ruma-client-api/src/r0/context/get_context.rs +++ b/ruma-client-api/src/r0/context/get_context.rs @@ -6,7 +6,7 @@ use ruma_common::Raw; use ruma_events::{AnyRoomEvent, AnyStateEvent}; use ruma_identifiers::{EventId, RoomId}; -use crate::r0::filter::RoomEventFilter; +use crate::r0::filter::{IncomingRoomEventFilter, RoomEventFilter}; ruma_api! { metadata: { @@ -42,7 +42,7 @@ ruma_api! { default, skip_serializing_if = "Option::is_none" )] - pub filter: Option, + pub filter: Option>, } #[non_exhaustive] diff --git a/ruma-client-api/src/r0/filter.rs b/ruma-client-api/src/r0/filter.rs index 1090a706..05aa03ac 100644 --- a/ruma-client-api/src/r0/filter.rs +++ b/ruma-client-api/src/r0/filter.rs @@ -26,22 +26,23 @@ pub enum EventFormat { } /// Filters to be applied to room events -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct RoomEventFilter { +#[derive(Clone, Copy, Debug, Default, Outgoing, Serialize)] +#[incoming_derive(Clone, Serialize)] +pub struct RoomEventFilter<'a> { /// A list of event types to exclude. /// /// If this list is absent then no event types are excluded. A matching type will be excluded /// even if it is listed in the 'types' filter. A '*' can be used as a wildcard to match any /// sequence of characters. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_types: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_types: &'a [String], /// A list of room IDs to exclude. /// /// If this list is absent then no rooms are excluded. A matching room will be excluded even if /// it is listed in the 'rooms' filter. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_rooms: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_rooms: &'a [String], /// The maximum number of events to return. #[serde(skip_serializing_if = "Option::is_none")] @@ -51,27 +52,27 @@ pub struct RoomEventFilter { /// /// If this list is absent then all rooms are included. #[serde(default, skip_serializing_if = "Option::is_none")] - pub rooms: Option>, + pub rooms: Option<&'a [RoomId]>, /// A list of sender IDs to exclude. /// /// If this list is absent then no senders are excluded. A matching sender will be excluded even /// if it is listed in the 'senders' filter. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_senders: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_senders: &'a [UserId], /// A list of senders IDs to include. /// /// If this list is absent then all senders are included. #[serde(default, skip_serializing_if = "Option::is_none")] - pub senders: Option>, + pub senders: Option<&'a [UserId]>, /// A list of event types to include. /// /// If this list is absent then all event types are included. A '*' can be used as a wildcard to /// match any sequence of characters. #[serde(default, skip_serializing_if = "Option::is_none")] - pub types: Option>, + pub types: Option<&'a [String]>, /// If `true` include only events with a URL key in their content. /// If `false`, exclude such events. @@ -85,16 +86,17 @@ pub struct RoomEventFilter { pub lazy_load_options: LazyLoadOptions, } -impl RoomEventFilter { +impl<'a> RoomEventFilter<'a> { /// A filter that can be used to ignore all room events pub fn ignore_all() -> Self { - Self { types: Some(vec![]), ..Default::default() } + Self { types: Some(&[]), ..Default::default() } } } /// Filters to be applied to room data -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct RoomFilter { +#[derive(Clone, Copy, Debug, Default, Outgoing, Serialize)] +#[incoming_derive(Clone, Serialize)] +pub struct RoomFilter<'a> { /// Include rooms that the user has left in the sync. /// /// Defaults to false if not included. @@ -103,54 +105,55 @@ pub struct RoomFilter { /// The per user account data to include for rooms. #[serde(skip_serializing_if = "Option::is_none")] - pub account_data: Option, + pub account_data: Option>, /// The message and state update events to include for rooms. #[serde(skip_serializing_if = "Option::is_none")] - pub timeline: Option, + pub timeline: Option>, /// The events that aren't recorded in the room history, e.g. typing and receipts, to include /// for rooms. #[serde(skip_serializing_if = "Option::is_none")] - pub ephemeral: Option, + pub ephemeral: Option>, /// The state events to include for rooms. #[serde(skip_serializing_if = "Option::is_none")] - pub state: Option, + pub state: Option>, /// A list of room IDs to exclude. /// /// If this list is absent then no rooms are excluded. A matching room will be excluded even if /// it is listed in the 'rooms' filter. This filter is applied before the filters in /// `ephemeral`, `state`, `timeline` or `account_data`. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_rooms: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_rooms: &'a [RoomId], /// A list of room IDs to include. /// /// If this list is absent then all rooms are included. This filter is applied before the /// filters in `ephemeral`, `state`, `timeline` or `account_data`. #[serde(default, skip_serializing_if = "Option::is_none")] - pub rooms: Option>, + pub rooms: Option<&'a [RoomId]>, } -impl RoomFilter { +impl<'a> RoomFilter<'a> { /// 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() } + Self { rooms: Some(&[]), ..Default::default() } } } /// Filter for not-room data -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct Filter { +#[derive(Clone, Copy, Debug, Default, Outgoing, Serialize)] +#[incoming_derive(Clone, Serialize)] +pub struct Filter<'a> { /// A list of event types to exclude. /// /// If this list is absent then no event types are excluded. A matching type will be excluded /// even if it is listed in the 'types' filter. A '*' can be used as a wildcard to match any /// sequence of characters. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_types: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_types: &'a [String], /// The maximum number of events to return. #[serde(skip_serializing_if = "Option::is_none")] @@ -160,32 +163,32 @@ pub struct Filter { /// /// If this list is absent then all senders are included. #[serde(default, skip_serializing_if = "Option::is_none")] - pub senders: Option>, + pub senders: Option<&'a [UserId]>, /// A list of event types to include. /// /// If this list is absent then all event types are included. A '*' can be used as a wildcard to /// match any sequence of characters. #[serde(default, skip_serializing_if = "Option::is_none")] - pub types: Option>, + pub types: Option<&'a [String]>, /// A list of sender IDs to exclude. /// /// If this list is absent then no senders are excluded. A matching sender will be excluded even /// if it is listed in the 'senders' filter. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub not_senders: Vec, + #[serde(default, skip_serializing_if = "<[_]>::is_empty")] + pub not_senders: &'a [UserId], } -impl Filter { +impl<'a> Filter<'a> { /// A filter that can be used to ignore all events pub fn ignore_all() -> Self { - Self { types: Some(vec![]), ..Default::default() } + Self { types: Some(&[]), ..Default::default() } } } /// A filter definition -#[derive(Clone, Debug, Default, Outgoing, Serialize)] +#[derive(Clone, Copy, Debug, Default, Outgoing, Serialize)] #[incoming_derive(Clone, Serialize)] pub struct FilterDefinition<'a> { /// List of event fields to include. @@ -206,15 +209,15 @@ pub struct FilterDefinition<'a> { /// The presence updates to include. #[serde(skip_serializing_if = "Option::is_none")] - pub presence: Option, + pub presence: Option>, /// The user account data that isn't associated with rooms to include. #[serde(skip_serializing_if = "Option::is_none")] - pub account_data: Option, + pub account_data: Option>, /// Filters to be applied to room data. #[serde(skip_serializing_if = "Option::is_none")] - pub room: Option, + pub room: Option>, } impl<'a> FilterDefinition<'a> { diff --git a/ruma-client-api/src/r0/message/get_message_events.rs b/ruma-client-api/src/r0/message/get_message_events.rs index 3427fcbb..8d724a79 100644 --- a/ruma-client-api/src/r0/message/get_message_events.rs +++ b/ruma-client-api/src/r0/message/get_message_events.rs @@ -7,7 +7,7 @@ use ruma_events::{AnyRoomEvent, AnyStateEvent}; use ruma_identifiers::RoomId; use serde::{Deserialize, Serialize}; -use crate::r0::filter::RoomEventFilter; +use crate::r0::filter::{IncomingRoomEventFilter, RoomEventFilter}; ruma_api! { metadata: { @@ -60,7 +60,7 @@ ruma_api! { default, skip_serializing_if = "Option::is_none" )] - pub filter: Option, + pub filter: Option>, } #[non_exhaustive] @@ -135,11 +135,12 @@ mod tests { #[test] fn test_serialize_some_room_event_filter() { let room_id = room_id!("!roomid:example.org"); + let rooms = &[room_id.clone()]; let filter = RoomEventFilter { lazy_load_options: LazyLoadOptions::Enabled { include_redundant_members: true }, - rooms: Some(vec![room_id.clone()]), - not_rooms: vec!["room".into(), "room2".into(), "room3".into()], - not_types: vec!["type".into()], + rooms: Some(rooms), + not_rooms: &["room".into(), "room2".into(), "room3".into()], + not_types: &["type".into()], ..Default::default() }; let req = Request { diff --git a/ruma-client-api/src/r0/search/search_events.rs b/ruma-client-api/src/r0/search/search_events.rs index 65117056..da646c33 100644 --- a/ruma-client-api/src/r0/search/search_events.rs +++ b/ruma-client-api/src/r0/search/search_events.rs @@ -9,7 +9,7 @@ use ruma_events::{AnyEvent, AnyStateEvent}; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; -use crate::r0::filter::RoomEventFilter; +use crate::r0::filter::{IncomingRoomEventFilter, RoomEventFilter}; ruma_api! { metadata: { @@ -73,7 +73,7 @@ impl<'a> Categories<'a> { } /// Criteria for searching a category of events. -#[derive(Clone, Debug, Outgoing, Serialize)] +#[derive(Clone, Copy, Debug, Outgoing, Serialize)] pub struct Criteria<'a> { /// The string to search events for. pub search_term: &'a str, @@ -84,7 +84,7 @@ pub struct Criteria<'a> { /// A `Filter` to apply to the search. #[serde(skip_serializing_if = "Option::is_none")] - pub filter: Option, + pub filter: Option>, /// The order in which to search for results. #[serde(skip_serializing_if = "Option::is_none")]