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