chatstates.rs

  1// Copyright (c) 2017 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::message::MessagePayload;
 10use crate::ns;
 11
 12/// Enum representing chatstate elements part of the
 13/// `http://jabber.org/protocol/chatstates` namespace.
 14#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
 15#[xml(namespace = ns::CHATSTATES, exhaustive)]
 16pub enum ChatState {
 17    /// `<active xmlns='http://jabber.org/protocol/chatstates'/>`
 18    #[xml(name = "active")]
 19    Active,
 20
 21    /// `<composing xmlns='http://jabber.org/protocol/chatstates'/>`
 22    #[xml(name = "composing")]
 23    Composing,
 24
 25    /// `<gone xmlns='http://jabber.org/protocol/chatstates'/>`
 26    #[xml(name = "gone")]
 27    Gone,
 28
 29    /// `<inactive xmlns='http://jabber.org/protocol/chatstates'/>`
 30    #[xml(name = "inactive")]
 31    Inactive,
 32
 33    /// `<paused xmlns='http://jabber.org/protocol/chatstates'/>`
 34    #[xml(name = "paused")]
 35    Paused,
 36}
 37
 38impl MessagePayload for ChatState {}
 39
 40#[cfg(test)]
 41mod tests {
 42    use super::*;
 43    use crate::ns;
 44    use minidom::Element;
 45    use xso::error::{Error, FromElementError};
 46
 47    #[test]
 48    fn test_size() {
 49        assert_size!(ChatState, 1);
 50    }
 51
 52    #[test]
 53    fn test_simple() {
 54        let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>"
 55            .parse()
 56            .unwrap();
 57        ChatState::try_from(elem).unwrap();
 58    }
 59
 60    #[test]
 61    fn test_invalid() {
 62        let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>"
 63            .parse()
 64            .unwrap();
 65        let error = ChatState::try_from(elem).unwrap_err();
 66        let message = match error {
 67            FromElementError::Invalid(Error::Other(string)) => string,
 68            _ => panic!(),
 69        };
 70        assert_eq!(message, "This is not a ChatState element.");
 71    }
 72
 73    #[cfg(not(feature = "disable-validation"))]
 74    #[test]
 75    fn test_invalid_child() {
 76        let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>"
 77            .parse()
 78            .unwrap();
 79        let error = ChatState::try_from(elem).unwrap_err();
 80        let message = match error {
 81            FromElementError::Invalid(Error::Other(string)) => string,
 82            _ => panic!(),
 83        };
 84        assert_eq!(message, "Unknown child in ChatState::Gone element.");
 85    }
 86
 87    #[cfg(not(feature = "disable-validation"))]
 88    #[test]
 89    fn test_invalid_attribute() {
 90        let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>"
 91            .parse()
 92            .unwrap();
 93        let error = ChatState::try_from(elem).unwrap_err();
 94        let message = match error {
 95            FromElementError::Invalid(Error::Other(string)) => string,
 96            _ => panic!(),
 97        };
 98        assert_eq!(message, "Unknown attribute in ChatState::Inactive element.");
 99    }
100
101    #[test]
102    fn test_serialise() {
103        let chatstate = ChatState::Active;
104        let elem: Element = chatstate.into();
105        assert!(elem.is("active", ns::CHATSTATES));
106    }
107}