sm.rs

  1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7#![allow(missing_docs)]
  8
  9use stanza_error::DefinedCondition;
 10
 11generate_element!(
 12    A, "a", SM,
 13    attributes: [
 14        h: u32 = "h" => required,
 15    ]
 16);
 17
 18impl A {
 19    pub fn new(h: u32) -> A {
 20        A { h }
 21    }
 22}
 23
 24generate_attribute!(ResumeAttr, "resume", bool);
 25
 26generate_element!(
 27    Enable, "enable", SM,
 28    attributes: [
 29        // TODO: should be the infinite integer set ≥ 1.
 30        max: Option<u32> = "max" => optional,
 31        resume: ResumeAttr = "resume" => default,
 32    ]
 33);
 34
 35impl Enable {
 36    pub fn new() -> Self {
 37        Enable {
 38            max: None,
 39            resume: ResumeAttr::False,
 40        }
 41    }
 42
 43    pub fn with_max(mut self, max: u32) -> Self {
 44        self.max = Some(max);
 45        self
 46    }
 47
 48    pub fn with_resume(mut self) -> Self {
 49        self.resume = ResumeAttr::True;
 50        self
 51    }
 52}
 53
 54generate_element!(
 55    Enabled, "enabled", SM,
 56    attributes: [
 57        id: Option<String> = "id" => optional,
 58        location: Option<String> = "location" => optional,
 59        // TODO: should be the infinite integer set ≥ 1.
 60        max: Option<u32> = "max" => optional,
 61        resume: ResumeAttr = "resume" => default,
 62    ]
 63);
 64
 65generate_element!(
 66    Failed, "failed", SM,
 67    attributes: [
 68        h: Option<u32> = "h" => optional,
 69    ],
 70    children: [
 71        // XXX: implement the * handling.
 72        error: Option<DefinedCondition> = ("*", XMPP_STANZAS) => DefinedCondition
 73    ]
 74);
 75
 76generate_empty_element!(
 77    R, "r", SM
 78);
 79
 80generate_element!(
 81    Resume, "resume", SM,
 82    attributes: [
 83        h: u32 = "h" => required,
 84        previd: String = "previd" => required,
 85    ]
 86);
 87
 88generate_element!(
 89    Resumed, "resumed", SM,
 90    attributes: [
 91        h: u32 = "h" => required,
 92        previd: String = "previd" => required,
 93    ]
 94);
 95
 96// TODO: add support for optional and required.
 97generate_empty_element!(
 98    /// Represents availability of Stream Management in `<stream:features/>`.
 99    StreamManagement, "sm", SM
100);
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use try_from::TryFrom;
106    use minidom::Element;
107
108    #[test]
109    fn a() {
110        let elem: Element = "<a xmlns='urn:xmpp:sm:3' h='5'".parse().unwrap();
111        let a = A::try_from(elem).unwrap();
112        assert_eq!(a.h, 5);
113    }
114
115    #[test]
116    fn stream_feature() {
117        let elem: Element = "<sm xmlns='urn:xmpp:sm:3'/>".parse().unwrap();
118        StreamManagement::try_from(elem).unwrap();
119    }
120}