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