xmpp_parsers/
stream_limits.rs

1// Copyright (c) 2024 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 xso::{AsXml, FromXml};
8
9use crate::ns;
10use core::num::NonZeroU32;
11
12/// Advertises limits on this stream.
13#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
14#[xml(namespace = ns::STREAM_LIMITS, name = "limits")]
15pub struct Limits {
16    /// Maximum size of any first-level stream elements (including stanzas), in bytes the
17    /// announcing entity is willing to accept.
18    #[xml(extract(default, name = "max-bytes", fields(text(type_ = NonZeroU32))))]
19    pub max_bytes: Option<NonZeroU32>,
20
21    /// Number of seconds without any traffic from the initiating entity after which the server may
22    /// consider the stream idle, and either perform liveness checks or terminate the stream.
23    #[xml(extract(default, name = "idle-seconds", fields(text(type_ = NonZeroU32))))]
24    pub idle_seconds: Option<NonZeroU32>,
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use minidom::Element;
31
32    #[test]
33    fn test_size() {
34        assert_size!(Limits, 8);
35    }
36
37    #[test]
38    fn test_simple() {
39        let elem: Element =
40            "<limits xmlns='urn:xmpp:stream-limits:0'><max-bytes>262144</max-bytes></limits>"
41                .parse()
42                .unwrap();
43        let limits = Limits::try_from(elem).unwrap();
44        assert_eq!(limits.max_bytes.unwrap(), NonZeroU32::new(262144).unwrap());
45        assert!(limits.idle_seconds.is_none());
46    }
47}