1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use serde::{
    de::{Deserialize, Deserializer},
    ser::{Serialize, Serializer},
};

/// Options for filtering based on the presence of a URL.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum UrlFilter {
    /// Includes only events with a url key in their content.
    EventsWithUrl,

    /// Excludes events with a url key in their content.
    EventsWithoutUrl,
}

impl Serialize for UrlFilter {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            Self::EventsWithUrl => serializer.serialize_bool(true),
            Self::EventsWithoutUrl => serializer.serialize_bool(false),
        }
    }
}

impl<'de> Deserialize<'de> for UrlFilter {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(match bool::deserialize(deserializer)? {
            true => Self::EventsWithUrl,
            false => Self::EventsWithoutUrl,
        })
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

    use super::UrlFilter;

    #[test]
    fn serialize_filter_events_with_url() {
        let events_with_url = UrlFilter::EventsWithUrl;
        assert_eq!(to_json_value(events_with_url).unwrap(), json!(true));
    }

    #[test]
    fn serialize_filter_events_without_url() {
        let events_without_url = UrlFilter::EventsWithoutUrl;
        assert_eq!(to_json_value(events_without_url).unwrap(), json!(false));
    }

    #[test]
    fn deserialize_filter_events_with_url() {
        let json = json!(true);
        assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithUrl);
    }

    #[test]
    fn deserialize_filter_events_without_url() {
        let json = json!(false);
        assert_eq!(from_json_value::<UrlFilter>(json).unwrap(), UrlFilter::EventsWithoutUrl);
    }
}