Make remaining filter types lifetime-generic

This commit is contained in:
Jonas Platte 2020-08-17 22:48:53 +02:00
parent 30a0a39d2b
commit d6beef3d07
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 52 additions and 48 deletions

View File

@ -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<RoomEventFilter>,
pub filter: Option<RoomEventFilter<'a>>,
}
#[non_exhaustive]

View File

@ -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<String>,
#[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<String>,
#[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<Vec<RoomId>>,
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<UserId>,
#[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<Vec<UserId>>,
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<Vec<String>>,
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<RoomEventFilter>,
pub account_data: Option<RoomEventFilter<'a>>,
/// The message and state update events to include for rooms.
#[serde(skip_serializing_if = "Option::is_none")]
pub timeline: Option<RoomEventFilter>,
pub timeline: Option<RoomEventFilter<'a>>,
/// 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<RoomEventFilter>,
pub ephemeral: Option<RoomEventFilter<'a>>,
/// The state events to include for rooms.
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<RoomEventFilter>,
pub state: Option<RoomEventFilter<'a>>,
/// 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<RoomId>,
#[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<Vec<RoomId>>,
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<String>,
#[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<Vec<UserId>>,
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<Vec<String>>,
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<UserId>,
#[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<Filter>,
pub presence: Option<Filter<'a>>,
/// The user account data that isn't associated with rooms to include.
#[serde(skip_serializing_if = "Option::is_none")]
pub account_data: Option<Filter>,
pub account_data: Option<Filter<'a>>,
/// Filters to be applied to room data.
#[serde(skip_serializing_if = "Option::is_none")]
pub room: Option<RoomFilter>,
pub room: Option<RoomFilter<'a>>,
}
impl<'a> FilterDefinition<'a> {

View File

@ -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<RoomEventFilter>,
pub filter: Option<RoomEventFilter<'a>>,
}
#[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 {

View File

@ -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<RoomEventFilter>,
pub filter: Option<RoomEventFilter<'a>>,
/// The order in which to search for results.
#[serde(skip_serializing_if = "Option::is_none")]