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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#![deny(missing_docs)]

//! Provides a type for Jabber IDs.
//!
//! For usage, check the documentation on the `Jid` struct.

#[macro_use]
extern crate failure_derive;

use std::convert::Into;
use std::fmt;
use std::str::FromStr;

/// An error that signifies that a `Jid` cannot be parsed from a string.
#[derive(Debug, Clone, PartialEq, Eq, Fail)]
pub enum JidParseError {
    /// Happens when there is no domain, that is either the string is empty,
    /// starts with a /, or contains the @/ sequence.
    #[fail(display = "no domain found in this JID")]
    NoDomain,

    /// Happens when the node is empty, that is the string starts with a @.
    #[fail(display = "nodepart empty despite the presence of a @")]
    EmptyNode,

    /// Happens when the resource is empty, that is the string ends with a /.
    #[fail(display = "resource empty despite the presence of a /")]
    EmptyResource,
}

/// A struct representing a Jabber ID.
///
/// A Jabber ID is composed of 3 components, of which 2 are optional:
///
///  - A node/name, `node`, which is the optional part before the @.
///  - A domain, `domain`, which is the mandatory part after the @ but before the /.
///  - A resource, `resource`, which is the optional part after the /.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Jid {
    /// The node part of the Jabber ID, if it exists, else None.
    pub node: Option<String>,
    /// The domain of the Jabber ID.
    pub domain: String,
    /// The resource of the Jabber ID, if it exists, else None.
    pub resource: Option<String>,
}

impl From<Jid> for String {
    fn from(jid: Jid) -> String {
        let mut string = String::new();
        if let Some(ref node) = jid.node {
            string.push_str(node);
            string.push('@');
        }
        string.push_str(&jid.domain);
        if let Some(ref resource) = jid.resource {
            string.push('/');
            string.push_str(resource);
        }
        string
    }
}

impl fmt::Debug for Jid {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "JID({})", self)
    }
}

impl fmt::Display for Jid {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        fmt.write_str(String::from(self.clone()).as_ref())
    }
}

enum ParserState {
    Node,
    Domain,
    Resource,
}

impl FromStr for Jid {
    type Err = JidParseError;

    fn from_str(s: &str) -> Result<Jid, JidParseError> {
        // TODO: very naive, may need to do it differently
        let iter = s.chars();
        let mut buf = String::with_capacity(s.len());
        let mut state = ParserState::Node;
        let mut node = None;
        let mut domain = None;
        let mut resource = None;
        for c in iter {
            match state {
                ParserState::Node => {
                    match c {
                        '@' => {
                            if buf == "" {
                                return Err(JidParseError::EmptyNode);
                            }
                            state = ParserState::Domain;
                            node = Some(buf.clone()); // TODO: performance tweaks, do not need to copy it
                            buf.clear();
                        }
                        '/' => {
                            if buf == "" {
                                return Err(JidParseError::NoDomain);
                            }
                            state = ParserState::Resource;
                            domain = Some(buf.clone()); // TODO: performance tweaks
                            buf.clear();
                        }
                        c => {
                            buf.push(c);
                        }
                    }
                }
                ParserState::Domain => {
                    match c {
                        '/' => {
                            if buf == "" {
                                return Err(JidParseError::NoDomain);
                            }
                            state = ParserState::Resource;
                            domain = Some(buf.clone()); // TODO: performance tweaks
                            buf.clear();
                        }
                        c => {
                            buf.push(c);
                        }
                    }
                }
                ParserState::Resource => {
                    buf.push(c);
                }
            }
        }
        if !buf.is_empty() {
            match state {
                ParserState::Node => {
                    domain = Some(buf);
                }
                ParserState::Domain => {
                    domain = Some(buf);
                }
                ParserState::Resource => {
                    resource = Some(buf);
                }
            }
        } else if let ParserState::Resource = state {
            return Err(JidParseError::EmptyResource);
        }
        Ok(Jid {
            node: node,
            domain: domain.ok_or(JidParseError::NoDomain)?,
            resource: resource,
        })
    }
}

impl Jid {
    /// Constructs a Jabber ID containing all three components.
    ///
    /// This is of the form `node`@`domain`/`resource`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::full("node", "domain", "resource");
    ///
    /// assert_eq!(jid.node, Some("node".to_owned()));
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, Some("resource".to_owned()));
    /// ```
    pub fn full<NS, DS, RS>(node: NS, domain: DS, resource: RS) -> Jid
    where
        NS: Into<String>,
        DS: Into<String>,
        RS: Into<String>,
    {
        Jid {
            node: Some(node.into()),
            domain: domain.into(),
            resource: Some(resource.into()),
        }
    }

    /// Constructs a Jabber ID containing only the `node` and `domain` components.
    ///
    /// This is of the form `node`@`domain`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::bare("node", "domain");
    ///
    /// assert_eq!(jid.node, Some("node".to_owned()));
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, None);
    /// ```
    pub fn bare<NS, DS>(node: NS, domain: DS) -> Jid
    where
        NS: Into<String>,
        DS: Into<String>,
    {
        Jid {
            node: Some(node.into()),
            domain: domain.into(),
            resource: None,
        }
    }

    /// Returns a new Jabber ID from the current one with only node and domain.
    ///
    /// This is of the form `node`@`domain`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::full("node", "domain", "resource").into_bare_jid();
    ///
    /// assert_eq!(jid.node, Some("node".to_owned()));
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, None);
    /// ```
    pub fn into_bare_jid(self) -> Jid {
        Jid {
            node: self.node,
            domain: self.domain,
            resource: None,
        }
    }

    /// Constructs a Jabber ID containing only a `domain`.
    ///
    /// This is of the form `domain`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::domain("domain");
    ///
    /// assert_eq!(jid.node, None);
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, None);
    /// ```
    pub fn domain<DS>(domain: DS) -> Jid
    where
        DS: Into<String>,
    {
        Jid {
            node: None,
            domain: domain.into(),
            resource: None,
        }
    }

    /// Returns a new Jabber ID from the current one with only domain.
    ///
    /// This is of the form `domain`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::full("node", "domain", "resource").into_domain_jid();
    ///
    /// assert_eq!(jid.node, None);
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, None);
    /// ```
    pub fn into_domain_jid(self) -> Jid {
        Jid {
            node: None,
            domain: self.domain,
            resource: None,
        }
    }

    /// Constructs a Jabber ID containing the `domain` and `resource` components.
    ///
    /// This is of the form `domain`/`resource`.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::domain_with_resource("domain", "resource");
    ///
    /// assert_eq!(jid.node, None);
    /// assert_eq!(jid.domain, "domain".to_owned());
    /// assert_eq!(jid.resource, Some("resource".to_owned()));
    /// ```
    pub fn domain_with_resource<DS, RS>(domain: DS, resource: RS) -> Jid
    where
        DS: Into<String>,
        RS: Into<String>,
    {
        Jid {
            node: None,
            domain: domain.into(),
            resource: Some(resource.into()),
        }
    }

    /// Constructs a new Jabber ID from an existing one, with the node swapped out with a new one.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::domain("domain");
    ///
    /// assert_eq!(jid.node, None);
    ///
    /// let new_jid = jid.with_node("node");
    ///
    /// assert_eq!(new_jid.node, Some("node".to_owned()));
    /// ```
    pub fn with_node<S>(&self, node: S) -> Jid
    where
        S: Into<String>,
    {
        Jid {
            node: Some(node.into()),
            domain: self.domain.clone(),
            resource: self.resource.clone(),
        }
    }

    /// Constructs a new Jabber ID from an existing one, with the domain swapped out with a new one.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::domain("domain");
    ///
    /// assert_eq!(jid.domain, "domain");
    ///
    /// let new_jid = jid.with_domain("new_domain");
    ///
    /// assert_eq!(new_jid.domain, "new_domain");
    /// ```
    pub fn with_domain<S>(&self, domain: S) -> Jid
    where
        S: Into<String>,
    {
        Jid {
            node: self.node.clone(),
            domain: domain.into(),
            resource: self.resource.clone(),
        }
    }

    /// Constructs a new Jabber ID from an existing one, with the resource swapped out with a new one.
    ///
    /// # Examples
    ///
    /// ```
    /// use jid::Jid;
    ///
    /// let jid = Jid::domain("domain");
    ///
    /// assert_eq!(jid.resource, None);
    ///
    /// let new_jid = jid.with_resource("resource");
    ///
    /// assert_eq!(new_jid.resource, Some("resource".to_owned()));
    /// ```
    pub fn with_resource<S>(&self, resource: S) -> Jid
    where
        S: Into<String>,
    {
        Jid {
            node: self.node.clone(),
            domain: self.domain.clone(),
            resource: Some(resource.into()),
        }
    }
}

#[cfg(feature = "minidom")]
use minidom::{ElementEmitter, IntoAttributeValue, IntoElements};

#[cfg(feature = "minidom")]
impl IntoAttributeValue for Jid {
    fn into_attribute_value(self) -> Option<String> {
        Some(String::from(self))
    }
}

#[cfg(feature = "minidom")]
impl IntoElements for Jid {
    fn into_elements(self, emitter: &mut ElementEmitter) {
        emitter.append_text_node(String::from(self))
    }
}

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

    use std::str::FromStr;

    #[test]
    fn can_parse_jids() {
        assert_eq!(Jid::from_str("a@b.c/d"), Ok(Jid::full("a", "b.c", "d")));
        assert_eq!(Jid::from_str("a@b.c"), Ok(Jid::bare("a", "b.c")));
        assert_eq!(Jid::from_str("b.c"), Ok(Jid::domain("b.c")));
        assert_eq!(
            Jid::from_str("a/b@c"),
            Ok(Jid::domain_with_resource("a", "b@c"))
        );
    }

    #[test]
    fn serialise() {
        assert_eq!(
            String::from(Jid::full("a", "b", "c")),
            String::from("a@b/c")
        );
    }

    #[test]
    fn invalid_jids() {
        assert_eq!(Jid::from_str(""), Err(JidParseError::NoDomain));
        assert_eq!(Jid::from_str("/c"), Err(JidParseError::NoDomain));
        assert_eq!(Jid::from_str("a@/c"), Err(JidParseError::NoDomain));
        assert_eq!(Jid::from_str("@b"), Err(JidParseError::EmptyNode));
        assert_eq!(Jid::from_str("b/"), Err(JidParseError::EmptyResource));
    }

    #[cfg(feature = "minidom")]
    #[test]
    fn minidom() {
        let elem: minidom::Element = "<message from='a@b/c'/>".parse().unwrap();
        let to: Jid = elem.attr("from").unwrap().parse().unwrap();
        assert_eq!(to, Jid::full("a", "b", "c"));
    }
}