ruma_client_api/filter/
lazy_load.rs

1use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct as _};
2
3/// Specifies options for [lazy-loading membership events][lazy-loading] on
4/// supported endpoints
5///
6/// [lazy-loading]: https://spec.matrix.org/latest/client-server-api/#lazy-loading-room-members
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize)]
8#[serde(from = "LazyLoadJsonRepr")]
9#[allow(clippy::exhaustive_enums)]
10pub enum LazyLoadOptions {
11    /// Disables lazy-loading of membership events.
12    #[default]
13    Disabled,
14
15    /// Enables lazy-loading of events.
16    Enabled {
17        /// If `true`, sends all membership events for all events, even if they have already been
18        /// sent to the client.
19        ///
20        /// Defaults to `false`.
21        include_redundant_members: bool,
22    },
23}
24
25impl LazyLoadOptions {
26    /// Returns `true` is `self` is `Disabled`.
27    pub fn is_disabled(&self) -> bool {
28        matches!(self, Self::Disabled)
29    }
30}
31
32impl Serialize for LazyLoadOptions {
33    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
34    where
35        S: Serializer,
36    {
37        let mut state;
38        match *self {
39            Self::Enabled { include_redundant_members: true } => {
40                state = serializer.serialize_struct("LazyLoad", 2)?;
41                state.serialize_field("lazy_load_members", &true)?;
42                state.serialize_field("include_redundant_members", &true)?;
43            }
44            Self::Enabled { .. } => {
45                state = serializer.serialize_struct("LazyLoad", 1)?;
46                state.serialize_field("lazy_load_members", &true)?;
47            }
48            Self::Disabled => {
49                state = serializer.serialize_struct("LazyLoad", 0)?;
50            }
51        }
52        state.end()
53    }
54}
55
56#[derive(Deserialize)]
57struct LazyLoadJsonRepr {
58    lazy_load_members: Option<bool>,
59    include_redundant_members: Option<bool>,
60}
61
62impl From<LazyLoadJsonRepr> for LazyLoadOptions {
63    fn from(opts: LazyLoadJsonRepr) -> Self {
64        if opts.lazy_load_members.unwrap_or(false) {
65            Self::Enabled {
66                include_redundant_members: opts.include_redundant_members.unwrap_or(false),
67            }
68        } else {
69            Self::Disabled
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use ruma_common::canonical_json::assert_to_canonical_json_eq;
77    use serde_json::{from_value as from_json_value, json};
78
79    use super::LazyLoadOptions;
80
81    #[test]
82    fn serialize_disabled() {
83        let lazy_load_options = LazyLoadOptions::Disabled;
84        assert_to_canonical_json_eq!(lazy_load_options, json!({}));
85    }
86
87    #[test]
88    fn serialize_no_redundant() {
89        let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
90        assert_to_canonical_json_eq!(lazy_load_options, json!({ "lazy_load_members": true }));
91    }
92
93    #[test]
94    fn serialize_with_redundant() {
95        let lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: true };
96        assert_to_canonical_json_eq!(
97            lazy_load_options,
98            json!({ "lazy_load_members": true, "include_redundant_members": true })
99        );
100    }
101
102    #[test]
103    fn deserialize_no_lazy_load() {
104        let json = json!({});
105        assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled);
106
107        let json = json!({ "include_redundant_members": true });
108        assert_eq!(from_json_value::<LazyLoadOptions>(json).unwrap(), LazyLoadOptions::Disabled);
109    }
110}