diff --git a/parsers/ChangeLog b/parsers/ChangeLog index 51b382c15ed477b4220c5ab3e969fbc469f66cf6..2f6bc8960f49ba80292fd108ba52e6e7dbd965f0 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -28,6 +28,8 @@ XXXX-YY-ZZ RELEASER - The bookmarks2 Conference `extensions` child is now an `Option` instead of a `Vec`, to distinguish between it being absent or empty (!472). + - Replace all boolean attributes with the `bool` type, so that we can + use them just like normal booleans (!476). * Improvements: - Keep unsupported vCard elements as `minidom::Element`, so that they get serialized back instead of being dropped. We now also test for diff --git a/parsers/src/bookmarks.rs b/parsers/src/bookmarks.rs index fdcde6c5876be41b75a27578ccda54ff10005c98..9968327ec24b4a97c0ceaf56b6b67953d3879c7e 100644 --- a/parsers/src/bookmarks.rs +++ b/parsers/src/bookmarks.rs @@ -12,15 +12,13 @@ //! See [ModernXMPP docs](https://docs.modernxmpp.org/client/groupchat/#bookmarks) on how to handle all historic //! and newer specifications for your clients. //! -//! This module exposes the [`Autojoin`][crate::bookmarks::Autojoin] boolean flag, the [`Conference`][crate::bookmarks::Conference] chatroom element, and the [`crate::ns::BOOKMARKS`] XML namespace. -//! //! The [`Conference`][crate::bookmarks::Conference] struct used in [`private::Query`][`crate::private::Query`] is the one from this module. Only the querying mechanism changes from a legacy PubSub implementation here, to a legacy Private XML Query implementation in that other module. The [`Conference`][crate::bookmarks2::Conference] element from the [`bookmarks2`][crate::bookmarks2] module is a different structure, but conversion is possible from [`bookmarks::Conference`][crate::bookmarks::Conference] to [`bookmarks2::Conference`][crate::bookmarks2::Conference] via the [`Conference::into_bookmarks2`][crate::bookmarks::Conference::into_bookmarks2] method. use xso::{AsXml, FromXml}; use jid::BareJid; -pub use crate::bookmarks2::{self, Autojoin}; +pub use crate::bookmarks2; use crate::ns; /// A conference bookmark. @@ -29,7 +27,7 @@ use crate::ns; pub struct Conference { /// Whether a conference bookmark should be joined automatically. #[xml(attribute(default))] - pub autojoin: Autojoin, + pub autojoin: bool, /// The JID of the conference. #[xml(attribute)] @@ -139,7 +137,7 @@ mod tests { assert_eq!(storage.urls[0].clone().name.unwrap(), "Example"); assert_eq!(storage.urls[0].url, "https://example.org/"); assert_eq!(storage.conferences.len(), 1); - assert_eq!(storage.conferences[0].autojoin, Autojoin::True); + assert_eq!(storage.conferences[0].autojoin, true); assert_eq!( storage.conferences[0].jid, BareJid::new("test-muc@muc.localhost").unwrap() diff --git a/parsers/src/bookmarks2.rs b/parsers/src/bookmarks2.rs index 32a49529d67bd2c0d86650d763f132f1fd16df2b..4e11394554fa0e70d2650dfa8ce28a2f76e96e4e 100644 --- a/parsers/src/bookmarks2.rs +++ b/parsers/src/bookmarks2.rs @@ -11,21 +11,12 @@ //! //! See [ModernXMPP docs](https://docs.modernxmpp.org/client/groupchat/#bookmarks) on how to handle all historic //! and newer specifications for your clients. -//! -//! This module exposes the [`Autojoin`][crate::bookmarks2::Autojoin] boolean flag, the [`Conference`][crate::bookmarks2::Conference] chatroom element, and the [BOOKMARKS2][crate::ns::BOOKMARKS2] XML namespace. use xso::{AsXml, FromXml}; use crate::ns; use minidom::Element; -generate_attribute!( - /// Whether a conference bookmark should be joined automatically. - Autojoin, - "autojoin", - bool -); - /// Potential extensions in a conference. #[derive(FromXml, AsXml, Debug, Clone, Default)] #[xml(namespace = ns::BOOKMARKS2, name = "extensions")] @@ -41,7 +32,7 @@ pub struct Extensions { pub struct Conference { /// Whether a conference bookmark should be joined automatically. #[xml(attribute(default))] - pub autojoin: Autojoin, + pub autojoin: bool, /// A user-defined name for this conference. #[xml(attribute(default))] @@ -86,12 +77,12 @@ mod tests { #[test] fn simple() { - let elem: Element = "" + let elem: Element = "" .parse() .unwrap(); let elem1 = elem.clone(); let conference = Conference::try_from(elem).unwrap(); - assert_eq!(conference.autojoin, Autojoin::False); + assert_eq!(conference.autojoin, false); assert_eq!(conference.name, None); assert_eq!(conference.nick, None); assert_eq!(conference.password, None); @@ -104,7 +95,7 @@ mod tests { fn complete() { let elem: Element = "Coucousecret".parse().unwrap(); let conference = Conference::try_from(elem).unwrap(); - assert_eq!(conference.autojoin, Autojoin::True); + assert_eq!(conference.autojoin, true); assert_eq!(conference.name, Some(String::from("Test MUC"))); assert_eq!(conference.clone().nick.unwrap(), "Coucou"); assert_eq!(conference.clone().password.unwrap(), "secret"); @@ -122,7 +113,7 @@ mod tests { // let conference = Conference::try_from(payload).unwrap(); let conference = Conference::try_from(payload).unwrap(); println!("FOO: conference: {:?}", conference); - assert_eq!(conference.autojoin, Autojoin::True); + assert_eq!(conference.autojoin, true); assert_eq!(conference.name, Some(String::from("Test MUC"))); assert_eq!(conference.clone().nick.unwrap(), "Coucou"); assert_eq!(conference.clone().password.unwrap(), "secret"); @@ -139,7 +130,7 @@ mod tests { let item = items.pop().unwrap(); let payload = item.payload.clone().unwrap(); let conference = Conference::try_from(payload).unwrap(); - assert_eq!(conference.autojoin, Autojoin::True); + assert_eq!(conference.autojoin, true); assert_eq!(conference.name, Some(String::from("Test MUC"))); assert_eq!(conference.clone().nick.unwrap(), "Coucou"); assert_eq!(conference.clone().password.unwrap(), "secret"); diff --git a/parsers/src/extdisco.rs b/parsers/src/extdisco.rs index 7697abf9232a26c3bd8fa0cd3258db3678ad9aab..a0204ed2c1c668ffaa9d35bcee6cc7f43a96eb6c 100644 --- a/parsers/src/extdisco.rs +++ b/parsers/src/extdisco.rs @@ -48,14 +48,6 @@ generate_attribute!( } ); -generate_attribute!( - /// Username and password credentials are required and will need to be requested if not already - /// provided. - Restricted, - "restricted", - bool -); - /// Structure representing a `` element. #[derive(FromXml, AsXml, Debug, PartialEq, Clone)] #[xml(namespace = ns::EXT_DISCO, name = "service")] @@ -88,7 +80,7 @@ pub struct Service { /// A boolean value indicating that username and password credentials are required and will /// need to be requested if not already provided. #[xml(attribute(default))] - restricted: Restricted, + restricted: bool, /// The underlying transport protocol to be used when communicating with the service (typically /// either TCP or UDP). @@ -160,7 +152,6 @@ mod tests { fn test_size() { assert_size!(Action, 1); assert_size!(Transport, 1); - assert_size!(Restricted, 1); assert_size!(Type, 1); assert_size!(Service, 84); assert_size!(ServicesQuery, 1); @@ -173,7 +164,6 @@ mod tests { fn test_size() { assert_size!(Action, 1); assert_size!(Transport, 1); - assert_size!(Restricted, 1); assert_size!(Type, 1); assert_size!(Service, 144); assert_size!(ServicesQuery, 1); @@ -191,7 +181,7 @@ mod tests { assert!(service.name.is_none()); assert!(service.password.is_none()); assert_eq!(service.port.unwrap(), 9998); - assert_eq!(service.restricted, Restricted::False); + assert_eq!(service.restricted, false); assert_eq!(service.transport.unwrap(), Transport::Udp); assert_eq!(service.type_, Type::Stun); assert!(service.username.is_none()); diff --git a/parsers/src/fast.rs b/parsers/src/fast.rs index 3f6324440adcb34ceed358491e875ed24db9c6f5..1f0dd3110467f31efbe226267d53faa4ffd3ba6a 100644 --- a/parsers/src/fast.rs +++ b/parsers/src/fast.rs @@ -14,19 +14,13 @@ generate_elem_id!( Mechanism, "mechanism", FAST ); -// TODO: Replace this with a proper bool once we can derive FromXml and AsXml on FastQuery. -generate_attribute!( - /// Whether TLS zero-roundtrip is possible. - Tls0Rtt, "tls-0rtt", bool -); - /// This is the `` element sent by the server as a SASL2 inline feature. #[derive(FromXml, AsXml, Debug, Clone, PartialEq)] #[xml(namespace = ns::FAST, name = "fast")] pub struct FastQuery { /// Whether TLS zero-roundtrip is possible. #[xml(attribute(default, name = "tls-0rtt"))] - pub tls_0rtt: Tls0Rtt, + pub tls_0rtt: bool, /// A list of `` elements, listing all server allowed mechanisms. #[xml(child(n = ..))] @@ -86,7 +80,7 @@ mod tests { .parse() .unwrap(); let request = FastQuery::try_from(elem).unwrap(); - assert_eq!(request.tls_0rtt, Tls0Rtt::False); + assert_eq!(request.tls_0rtt, false); assert_eq!(request.mechanisms, [Mechanism(String::from("FOO-BAR"))]); let elem: Element = "" diff --git a/parsers/src/legacy_omemo.rs b/parsers/src/legacy_omemo.rs index 56bd7d6ebc505016b6e24e731a0fa89c07967f83..f893b51801be3a17e1dac1771e3c2e4e8acba90c 100644 --- a/parsers/src/legacy_omemo.rs +++ b/parsers/src/legacy_omemo.rs @@ -140,13 +140,6 @@ pub struct IV { pub data: Vec, } -generate_attribute!( - /// prekey attribute for the key element. - IsPreKey, - "prekey", - bool -); - /// Part of the OMEMO element header #[derive(FromXml, AsXml, PartialEq, Debug, Clone)] #[xml(namespace = ns::LEGACY_OMEMO, name = "key")] @@ -158,7 +151,7 @@ pub struct Key { /// The key element MUST be tagged with a prekey attribute set to true /// if a PreKeySignalMessage is being used. #[xml(attribute(default))] - pub prekey: IsPreKey, + pub prekey: bool, /// The 16 bytes key and the GCM authentication tag concatenated together /// and encrypted using the corresponding long-standing SignalProtocol @@ -303,7 +296,7 @@ mod tests { keys: vec![ Key { rid: 1236, - prekey: IsPreKey::True, + prekey: true, data: vec![ 51, 8, 233, 244, 1, 18, 33, 5, 114, 150, 199, 11, 62, 152, 255, 30, 90, 48, 105, 56, 136, 177, 160, 99, 239, 113, 194, 84, 73, 101, 94, 149, @@ -321,7 +314,7 @@ mod tests { }, Key { rid: 26052318, - prekey: IsPreKey::False, + prekey: false, data: vec![ 51, 10, 33, 5, 53, 122, 118, 155, 166, 47, 83, 177, 3, 211, 29, 32, 89, 174, 216, 77, 125, 249, 194, 22, 12, 117, 152, 126, 23, 110, 110, 250, @@ -334,7 +327,7 @@ mod tests { }, Key { rid: 1164059891, - prekey: IsPreKey::False, + prekey: false, data: vec![ 51, 10, 33, 5, 89, 225, 207, 218, 111, 18, 61, 108, 215, 6, 132, 30, 233, 57, 170, 148, 42, 133, 74, 233, 106, 252, 156, 23, 61, 33, 171, diff --git a/parsers/src/mam.rs b/parsers/src/mam.rs index 82b02ef959173f9589e50ff1edc2a7ba0d861537..fcf32e91dd8d303450fac90194914989d79c8e49 100644 --- a/parsers/src/mam.rs +++ b/parsers/src/mam.rs @@ -137,20 +137,13 @@ pub struct Result_ { impl MessagePayload for Result_ {} -generate_attribute!( - /// True when the end of a MAM query has been reached. - Complete, - "complete", - bool -); - /// Notes the end of a page in a query. #[derive(FromXml, AsXml, Debug, Clone, PartialEq)] #[xml(namespace = ns::MAM, name = "fin")] pub struct Fin { /// True when the end of a MAM query has been reached. #[xml(attribute(default))] - pub complete: Complete, + pub complete: bool, /// Describes the current page, it should contain at least [first] /// (with an [index]) and [last], and generally [count]. @@ -223,7 +216,6 @@ mod tests { assert_size!(QueryId, 12); assert_size!(Query, 120); assert_size!(Result_, 164); - assert_size!(Complete, 1); assert_size!(Fin, 44); assert_size!(Start, 28); assert_size!(End, 28); @@ -237,7 +229,6 @@ mod tests { assert_size!(QueryId, 24); assert_size!(Query, 240); assert_size!(Result_, 312); - assert_size!(Complete, 1); assert_size!(Fin, 88); assert_size!(Start, 40); assert_size!(End, 40); @@ -446,7 +437,7 @@ mod tests { #[test] fn test_serialize_fin() { - let reference: Element = "28482-98726-7362309af3-cc343-b409f" + let reference: Element = "28482-98726-7362309af3-cc343-b409f" .parse() .unwrap(); @@ -458,7 +449,7 @@ mod tests { let fin = Fin { set, - complete: Complete::default(), + complete: false, }; let serialized: Element = fin.into(); assert_eq!(serialized, reference); diff --git a/parsers/src/pubsub/pubsub.rs b/parsers/src/pubsub/pubsub.rs index ccae4b8fed04f518859640b4bfcfa939c1ef7f01..749ea6e668ac26723ef1cbf98114f4f84df9411e 100644 --- a/parsers/src/pubsub/pubsub.rs +++ b/parsers/src/pubsub/pubsub.rs @@ -160,13 +160,6 @@ pub struct PublishOptions { pub form: Option, } -generate_attribute!( - /// Whether a retract request should notify subscribers or not. - Notify, - "notify", - bool -); - /// A request to retract some items from a node. #[derive(FromXml, AsXml, Debug, PartialEq, Clone)] #[xml(namespace = ns::PUBSUB, name = "retract")] @@ -177,7 +170,7 @@ pub struct Retract { /// Whether a retract request should notify subscribers or not. #[xml(attribute(default))] - pub notify: Notify, + pub notify: bool, /// The items affected by this request. #[xml(child(n = ..))] diff --git a/parsers/src/sm.rs b/parsers/src/sm.rs index c9fcdf137be58439d44204434cfc0742483b94d3..bc68d81abfe53529bcf8107c69061e08bed14a83 100644 --- a/parsers/src/sm.rs +++ b/parsers/src/sm.rs @@ -25,13 +25,6 @@ impl A { } } -generate_attribute!( - /// Whether to allow resumption of a previous stream. - ResumeAttr, - "resume", - bool -); - /// Client request for enabling stream management. #[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)] #[xml(namespace = ns::SM, name = "enable")] @@ -43,7 +36,7 @@ pub struct Enable { /// Whether the client wants to be allowed to resume the stream. #[xml(attribute(default))] - pub resume: ResumeAttr, + pub resume: bool, } impl Enable { @@ -60,7 +53,7 @@ impl Enable { /// Asks for resumption to be possible. pub fn with_resume(mut self) -> Self { - self.resume = ResumeAttr::True; + self.resume = true; self } } @@ -90,7 +83,7 @@ pub struct Enabled { /// Whether stream resumption is allowed. #[xml(attribute(default))] - pub resume: ResumeAttr, + pub resume: bool, } /// A stream management error happened. @@ -196,7 +189,6 @@ mod tests { #[test] fn test_size() { assert_size!(A, 4); - assert_size!(ResumeAttr, 1); assert_size!(Enable, 12); assert_size!(StreamId, 12); assert_size!(Enabled, 36); @@ -213,7 +205,6 @@ mod tests { #[test] fn test_size() { assert_size!(A, 4); - assert_size!(ResumeAttr, 1); assert_size!(Enable, 12); assert_size!(StreamId, 24); assert_size!(Enabled, 64); @@ -256,14 +247,14 @@ mod tests { .unwrap(); let enable = Enable::try_from(elem).unwrap(); assert_eq!(enable.max, None); - assert_eq!(enable.resume, ResumeAttr::True); + assert_eq!(enable.resume, true); let elem: Element = "" .parse() .unwrap(); let enabled = Enabled::try_from(elem).unwrap(); let previd = enabled.id.unwrap(); - assert_eq!(enabled.resume, ResumeAttr::True); + assert_eq!(enabled.resume, true); assert_eq!(previd, StreamId(String::from("coucou"))); assert_eq!(enabled.max, Some(600)); assert_eq!(enabled.location, None); diff --git a/parsers/src/util/macros.rs b/parsers/src/util/macros.rs index 3921948a4c3a7348c3ce9ba95d16bc6b77655096..1a1b14087105b932157bad513d49a64dd148d29b 100644 --- a/parsers/src/util/macros.rs +++ b/parsers/src/util/macros.rs @@ -214,62 +214,6 @@ macro_rules! generate_attribute { } } ); - ($(#[$meta:meta])* $elem:ident, $name:tt, bool) => ( - $(#[$meta])* - #[derive(Debug, Clone, PartialEq)] - pub enum $elem { - /// True value, represented by either 'true' or '1'. - True, - /// False value, represented by either 'false' or '0'. - False, - } - impl ::std::str::FromStr for $elem { - type Err = xso::error::Error; - fn from_str(s: &str) -> Result { - Ok(match s { - "true" | "1" => $elem::True, - "false" | "0" => $elem::False, - _ => return Err(xso::error::Error::Other(concat!("Unknown value for '", $name, "' attribute."))), - }) - } - } - impl ::xso::FromXmlText for $elem { - fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> { - match s.parse::().map_err(xso::error::Error::text_parse_error)? { - true => Ok(Self::True), - false => Ok(Self::False), - } - } - } - impl ::xso::AsXmlText for $elem { - fn as_xml_text(&self) -> Result<::std::borrow::Cow<'_, str>, xso::error::Error> { - match self { - Self::True => Ok(::std::borrow::Cow::Borrowed("true")), - Self::False => Ok(::std::borrow::Cow::Borrowed("false")), - } - } - - fn as_optional_xml_text(&self) -> Result>, xso::error::Error> { - match self { - Self::True => Ok(Some(::std::borrow::Cow::Borrowed("true"))), - Self::False => Ok(None), - } - } - } - impl ::minidom::IntoAttributeValue for $elem { - fn into_attribute_value(self) -> Option { - match self { - $elem::True => Some(String::from("true")), - $elem::False => None - } - } - } - impl ::std::default::Default for $elem { - fn default() -> $elem { - $elem::False - } - } - ); ($(#[$meta:meta])* $elem:ident, $name:tt, $type:tt, Default = $default:expr) => ( $(#[$meta])* #[derive(Debug, Clone, PartialEq)]