aboutsummaryrefslogtreecommitdiff
path: root/src/message/signalproxy/objects/chanmodes.rs
blob: 1161e28550ca0d0958a3578bfd2954f3b56848ae (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::collections::HashMap;

use crate::{
    message::NetworkMap,
    primitive::{StringList, Variant, VariantMap},
};

#[derive(Debug, Clone, PartialEq)]
pub struct ChanModes {
    /// Modes that add or remove items from a list, like commonly +b for the banlist.
    ///
    /// Always require a parameter from server to client.
    /// Clients can request the whole list by leaving the parameter empty
    pub channel_modes_a: HashMap<char, StringList>,

    /// Modes that take a parameter as setting and require it when setting or removing the mode.
    pub channel_modes_b: HashMap<char, String>,

    /// Modes that take a parameter as setting, but only require it when setting the mode.
    pub channel_modes_c: HashMap<char, String>,

    /// Modes without a parameter.
    pub channel_modes_d: String,
}

impl NetworkMap for ChanModes {
    type Item = VariantMap;

    fn to_network_map(&self) -> Self::Item {
        let mut map = VariantMap::new();

        map.insert(
            s!("A"),
            Variant::VariantMap(
                self.channel_modes_a
                    .iter()
                    .map(|(k, v)| (k.to_string(), Variant::StringList(v.clone())))
                    .collect(),
            ),
        );
        map.insert(
            s!("B"),
            Variant::VariantMap(
                self.channel_modes_b
                    .iter()
                    .map(|(k, v)| (k.to_string(), Variant::String(v.clone())))
                    .collect(),
            ),
        );
        map.insert(
            s!("C"),
            Variant::VariantMap(
                self.channel_modes_c
                    .iter()
                    .map(|(k, v)| (k.to_string(), Variant::String(v.clone())))
                    .collect(),
            ),
        );
        map.insert(s!("D"), Variant::String(self.channel_modes_d.clone()));

        map
    }

    fn from_network_map(input: &mut Self::Item) -> Self {
        ChanModes {
            channel_modes_a: match_variant!(input.remove("A").unwrap(), Variant::VariantMap)
                .into_iter()
                .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::StringList)))
                .collect(),
            channel_modes_b: match_variant!(input.remove("B").unwrap(), Variant::VariantMap)
                .into_iter()
                .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::String)))
                .collect(),
            channel_modes_c: match_variant!(input.remove("C").unwrap(), Variant::VariantMap)
                .into_iter()
                .map(|(mut k, v)| (k.remove(0), match_variant!(v, Variant::String)))
                .collect(),
            channel_modes_d: match_variant!(input.remove("D").unwrap(), Variant::String),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn get_network() -> VariantMap {
        map! {
            s!("B") => Variant::VariantMap(map!
                {},
            ),
            s!("D") => Variant::String(
                s!("tCnT"),
            ),
            s!("C") => Variant::VariantMap(map!
                {
                    s!("j") => Variant::String(
                        s!("5:1"),
                    ),
                    s!("x") => Variant::String(
                        s!("10:5"),
                    ),
                    s!("f") => Variant::String(
                        s!("30:5"),
                    ),
                    s!("F") => Variant::String(
                        s!("5:60"),
                    ),
                },
            ),
            s!("A") => Variant::VariantMap(map! {
                s!("b") => Variant::StringList(vec![s!("*!*@test"), s!("*!*@test2")]),
            }),
        }
    }

    fn get_runtime() -> ChanModes {
        ChanModes {
            channel_modes_a: map! { 'b' => vec![s!("*!*@test"), s!("*!*@test2")] },
            channel_modes_b: map! {},
            channel_modes_c: map! { 'j' => s!("5:1"), 'x' => s!("10:5"), 'f' => s!("30:5"), 'F' => s!("5:60") },
            channel_modes_d: s!("tCnT"),
        }
    }

    #[test]
    fn chanmodes_to_network() {
        assert_eq!(get_runtime().to_network_map(), get_network())
    }

    #[test]
    fn chanmodes_from_network() {
        assert_eq!(ChanModes::from_network_map(&mut get_network()), get_runtime())
    }
}