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
// Copyright (C) 2019 Maxime “pep” Buquet <pep@bouah.net>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::data_forms::{DataForm, DataFormType, Field, FieldType};
use crate::ns;
use xso::error::Error;

/// Structure representing a `http://jabber.org/network/serverinfo` form type.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ServerInfo {
    /// Abuse addresses
    pub abuse: Vec<String>,

    /// Admin addresses
    pub admin: Vec<String>,

    /// Feedback addresses
    pub feedback: Vec<String>,

    /// Sales addresses
    pub sales: Vec<String>,

    /// Security addresses
    pub security: Vec<String>,

    /// Support addresses
    pub support: Vec<String>,
}

impl TryFrom<DataForm> for ServerInfo {
    type Error = Error;

    fn try_from(form: DataForm) -> Result<ServerInfo, Error> {
        if form.type_ != DataFormType::Result_ {
            return Err(Error::Other("Wrong type of form."));
        }
        if form.form_type != Some(String::from(ns::SERVER_INFO)) {
            return Err(Error::Other("Wrong FORM_TYPE for form."));
        }
        let mut server_info = ServerInfo::default();
        for field in form.fields {
            if field.type_ != FieldType::ListMulti {
                return Err(Error::Other("Field is not of the required type."));
            }
            if field.var.as_deref() == Some("abuse-addresses") {
                server_info.abuse = field.values;
            } else if field.var.as_deref() == Some("admin-addresses") {
                server_info.admin = field.values;
            } else if field.var.as_deref() == Some("feedback-addresses") {
                server_info.feedback = field.values;
            } else if field.var.as_deref() == Some("sales-addresses") {
                server_info.sales = field.values;
            } else if field.var.as_deref() == Some("security-addresses") {
                server_info.security = field.values;
            } else if field.var.as_deref() == Some("support-addresses") {
                server_info.support = field.values;
            } else {
                return Err(Error::Other("Unknown form field var."));
            }
        }

        Ok(server_info)
    }
}

impl From<ServerInfo> for DataForm {
    fn from(server_info: ServerInfo) -> DataForm {
        DataForm {
            type_: DataFormType::Result_,
            form_type: Some(String::from(ns::SERVER_INFO)),
            title: None,
            instructions: None,
            fields: vec![
                generate_address_field("abuse-addresses", server_info.abuse),
                generate_address_field("admin-addresses", server_info.admin),
                generate_address_field("feedback-addresses", server_info.feedback),
                generate_address_field("sales-addresses", server_info.sales),
                generate_address_field("security-addresses", server_info.security),
                generate_address_field("support-addresses", server_info.support),
            ],
        }
    }
}

/// Generate `Field` for addresses
pub fn generate_address_field<S: Into<String>>(var: S, values: Vec<String>) -> Field {
    Field {
        var: Some(var.into()),
        type_: FieldType::ListMulti,
        label: None,
        required: false,
        desc: None,
        options: vec![],
        values,
        media: vec![],
    }
}

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

    #[cfg(target_pointer_width = "32")]
    #[test]
    fn test_size() {
        assert_size!(ServerInfo, 72);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn test_size() {
        assert_size!(ServerInfo, 144);
    }

    #[test]
    fn test_simple() {
        let form = DataForm::new(
            DataFormType::Result_,
            ns::SERVER_INFO,
            vec![
                Field::new("abuse-addresses", FieldType::ListMulti),
                Field::new("admin-addresses", FieldType::ListMulti)
                    .with_value("xmpp:admin@foo.bar")
                    .with_value("https://foo.bar/chat/")
                    .with_value("mailto:admin@foo.bar"),
                Field::new("feedback-addresses", FieldType::ListMulti),
                Field::new("sales-addresses", FieldType::ListMulti),
                Field::new("security-addresses", FieldType::ListMulti)
                    .with_value("xmpp:security@foo.bar")
                    .with_value("mailto:security@foo.bar"),
                Field::new("support-addresses", FieldType::ListMulti)
                    .with_value("mailto:support@foo.bar"),
            ],
        );

        let server_info = ServerInfo {
            abuse: vec![],
            admin: vec![
                String::from("xmpp:admin@foo.bar"),
                String::from("https://foo.bar/chat/"),
                String::from("mailto:admin@foo.bar"),
            ],
            feedback: vec![],
            sales: vec![],
            security: vec![
                String::from("xmpp:security@foo.bar"),
                String::from("mailto:security@foo.bar"),
            ],
            support: vec![String::from("mailto:support@foo.bar")],
        };

        // assert_eq!(DataForm::from(server_info), form);
        assert_eq!(ServerInfo::try_from(form).unwrap(), server_info);
    }
}