ruma_client_api/filter/
url.rs
1use serde::{
2 de::{Deserialize, Deserializer},
3 ser::{Serialize, Serializer},
4};
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8#[allow(clippy::exhaustive_enums)]
9pub enum UrlFilter {
10 EventsWithUrl,
12
13 EventsWithoutUrl,
15}
16
17impl Serialize for UrlFilter {
18 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19 where
20 S: Serializer,
21 {
22 match *self {
23 Self::EventsWithUrl => serializer.serialize_bool(true),
24 Self::EventsWithoutUrl => serializer.serialize_bool(false),
25 }
26 }
27}
28
29impl<'de> Deserialize<'de> for UrlFilter {
30 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
31 where
32 D: Deserializer<'de>,
33 {
34 Ok(match bool::deserialize(deserializer)? {
35 true => Self::EventsWithUrl,
36 false => Self::EventsWithoutUrl,
37 })
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
44
45 use super::UrlFilter;
46
47 #[test]
48 fn serialize_filter_events_with_url() {
49 let events_with_url = UrlFilter::EventsWithUrl;
50 assert_eq!(to_json_value(events_with_url).unwrap(), json!(true));
51 }
52
53 #[test]
54 fn serialize_filter_events_without_url() {
55 let events_without_url = UrlFilter::EventsWithoutUrl;
56 assert_eq!(to_json_value(events_without_url).unwrap(), json!(false));
57 }
58
59 #[test]
60 fn deserialize_filter_events_with_url() {
61 let json = json!(true);
62 assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithUrl);
63 }
64
65 #[test]
66 fn deserialize_filter_events_without_url() {
67 let json = json!(false);
68 assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithoutUrl);
69 }
70}