aboutsummaryrefslogtreecommitdiffstats
path: root/src/stanza/starttls.rs
blob: 874ae66e50aa797f9be2e8929f2132c7dfae821a (plain) (blame)
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
use std::collections::{HashMap, HashSet};

use peanuts::{
    element::{Content, FromElement, IntoElement, Name, NamespaceDeclaration},
    Element,
};

pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-tls";

#[derive(Debug)]
pub struct StartTls {
    pub required: bool,
}

impl IntoElement for StartTls {
    fn into_element(&self) -> peanuts::Element {
        let content;
        if self.required == true {
            let element = Content::Element(Element {
                name: Name {
                    namespace: Some(XMLNS.to_string()),
                    local_name: "required".to_string(),
                },
                namespace_declarations: HashSet::new(),
                attributes: HashMap::new(),
                content: Vec::new(),
            });
            content = vec![element];
        } else {
            content = Vec::new();
        }
        let mut namespace_declarations = HashSet::new();
        namespace_declarations.insert(NamespaceDeclaration {
            prefix: None,
            namespace: XMLNS.to_string(),
        });
        Element {
            name: Name {
                namespace: Some(XMLNS.to_string()),
                local_name: "starttls".to_string(),
            },
            namespace_declarations,
            attributes: HashMap::new(),
            content,
        }
    }
}

impl FromElement for StartTls {
    fn from_element(element: peanuts::Element) -> peanuts::Result<Self> {
        let Name {
            namespace,
            local_name,
        } = element.name;
        if namespace.as_deref() == Some(XMLNS) && &local_name == "starttls" {
            let mut required = false;
            if element.content.len() == 1 {
                match element.content.first().unwrap() {
                    Content::Element(element) => {
                        let Name {
                            namespace,
                            local_name,
                        } = &element.name;

                        if namespace.as_deref() == Some(XMLNS) && local_name == "required" {
                            required = true
                        } else {
                            return Err(peanuts::Error::UnexpectedElement(element.name.clone()));
                        }
                    }
                    c => return Err(peanuts::Error::UnexpectedContent((*c).clone())),
                }
            } else {
                return Err(peanuts::Error::UnexpectedNumberOfContents(
                    element.content.len(),
                ));
            }
            return Ok(StartTls { required });
        } else {
            return Err(peanuts::Error::IncorrectName(Name {
                namespace,
                local_name,
            }));
        }
    }
}

#[derive(Debug)]
pub struct Proceed;

impl IntoElement for Proceed {
    fn into_element(&self) -> Element {
        let mut namespace_declarations = HashSet::new();
        namespace_declarations.insert(NamespaceDeclaration {
            prefix: None,
            namespace: XMLNS.to_string(),
        });
        Element {
            name: Name {
                namespace: Some(XMLNS.to_string()),
                local_name: "proceed".to_string(),
            },
            namespace_declarations,
            attributes: HashMap::new(),
            content: Vec::new(),
        }
    }
}

impl FromElement for Proceed {
    fn from_element(element: Element) -> peanuts::Result<Self> {
        let Name {
            namespace,
            local_name,
        } = element.name;
        if namespace.as_deref() == Some(XMLNS) && &local_name == "proceed" {
            return Ok(Proceed);
        } else {
            return Err(peanuts::Error::IncorrectName(Name {
                namespace,
                local_name,
            }));
        }
    }
}

pub struct Failure;

impl IntoElement for Failure {
    fn into_element(&self) -> Element {
        let mut namespace_declarations = HashSet::new();
        namespace_declarations.insert(NamespaceDeclaration {
            prefix: None,
            namespace: XMLNS.to_string(),
        });
        Element {
            name: Name {
                namespace: Some(XMLNS.to_string()),
                local_name: "failure".to_string(),
            },
            namespace_declarations,
            attributes: HashMap::new(),
            content: Vec::new(),
        }
    }
}

impl FromElement for Failure {
    fn from_element(element: Element) -> peanuts::Result<Self> {
        let Name {
            namespace,
            local_name,
        } = element.name;
        if namespace.as_deref() == Some(XMLNS) && &local_name == "failure" {
            return Ok(Failure);
        } else {
            return Err(peanuts::Error::IncorrectName(Name {
                namespace,
                local_name,
            }));
        }
    }
}