aboutsummaryrefslogtreecommitdiff
path: root/src/message/signalproxy/objects/aliasmanager.rs
blob: 093163bc1253e436c9b4afcb48330eca4c57c955 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::primitive::{StringList, Variant, VariantMap};

use crate::message::signalproxy::Network;

#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct AliasManager {
    pub aliases: Vec<Alias>,
}

#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct Alias {
    name: String,
    expansion: String,
}

impl Alias {
    fn from_network_internal(input: &VariantMap) -> Vec<Self> {
        let names = match_variant!(input.get("names").unwrap(), Variant::StringList);
        let expansions = match_variant!(input.get("expansions").unwrap(), Variant::StringList);

        return names
            .iter()
            .zip(expansions)
            .map(|(name, expansion)| Alias {
                name: name.clone(),
                expansion,
            })
            .collect();
    }
}

impl Network for Alias {
    type Item = VariantMap;

    fn to_network(&self) -> Self::Item {
        let mut map = VariantMap::new();
        map.insert(s!("names"), Variant::StringList(vec![self.name.clone()]));
        map.insert(
            s!("expansions"),
            Variant::StringList(vec![self.expansion.clone()]),
        );

        return map;
    }

    fn from_network(input: &mut Self::Item) -> Self {
        Alias::from_network_internal(&input)[0].clone()
    }
}

impl Network for AliasManager {
    type Item = VariantMap;

    fn to_network(&self) -> Self::Item {
        let (names, expansions) = self.aliases.iter().fold(
            (StringList::new(), StringList::new()),
            |(mut names, mut expansions), alias| {
                names.push(alias.name.clone());
                expansions.push(alias.expansion.clone());
                return (names, expansions);
            },
        );

        let mut map = VariantMap::new();
        map.insert(s!("names"), Variant::StringList(names));
        map.insert(s!("expansions"), Variant::StringList(expansions));

        return map;
    }

    fn from_network(input: &mut Self::Item) -> Self {
        let names = match_variant!(input.get("names").unwrap(), Variant::StringList);
        let expansions = match_variant!(input.get("expansions").unwrap(), Variant::StringList);

        AliasManager {
            aliases: names
                .iter()
                .zip(expansions)
                .map(|(name, expansion)| Alias {
                    name: name.clone(),
                    expansion,
                })
                .collect(),
        }
    }
}

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

    fn get_src() -> AliasManager {
        AliasManager {
            aliases: vec![
                Alias {
                    name: s!("j"),
                    expansion: s!("/join $0"),
                },
                Alias {
                    name: s!("ns"),
                    expansion: s!("/msg nickserv $0"),
                },
            ],
        }
    }

    fn get_dest() -> VariantMap {
        map! {
            s!("names") => Variant::StringList(
                vec![
                    s!("j"),
                    s!("ns"),
                ],
            ),
            s!("expansions") => Variant::StringList(
                vec![
                    s!("/join $0"),
                    s!("/msg nickserv $0"),
                ],
            ),
        }
    }

    #[test]
    fn aliasmanager_to_network() {
        assert_eq!(get_src().to_network(), get_dest())
    }

    #[test]
    fn aliasmanager_from_network() {
        assert_eq!(AliasManager::from_network(&mut get_dest()), get_src())
    }
}

// impl AliasManager {
//     /// Client to Server
//     ///
//     /// Replaces all properties of the object with the content of the
//     /// "properties" parameter. This parameter is in network representation.
//     ///
//     fn request_update(self: &mut Self, properties: VariantMap) {
//         self.update(properties);
//     }

//     /// Server to Client
//     fn add_alias(self: &mut Self, name: String, expansion: String) {
//         self.aliases.push(Alias { name, expansion });
//     }

//     /// Server to Client
//     ///
//     /// Replaces all properties of the object with the content of the
//     /// "properties" parameter. This parameter is in network representation.
//     ///
//     fn update(self: &mut Self, properties: VariantMap) {
//         let mut alias: Vec<Alias> = Vec::new();

//         // for (i, name) in match_variant!(properties[&"Aliases".to_string()], Variant::String) {
//         //     alias.push(Alias {
//         //         name,
//         //         expansion: match_variant!(properties["Aliases"], Variant::String)["expansions"][i],
//         //     })
//         // }

//         self.aliases = alias
//     }
// }