xmpp-parsers: Replace all instances of bool attributes with bool

These bring basically nothing, so we can throw them away alongside their
generator macro.
This commit is contained in:
Emmanuel Gil Peyrot 2024-08-03 20:02:06 +02:00 committed by xmpp ftw
commit 866991bda5
10 changed files with 28 additions and 141 deletions

View file

@ -28,6 +28,8 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
- The bookmarks2 Conference `extensions` child is now an
`Option<Extensions>` instead of a `Vec<Element>`, 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

View file

@ -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()

View file

@ -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 = "<conference xmlns='urn:xmpp:bookmarks:1'/>"
let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='false'/>"
.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 = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password><extensions><test xmlns='urn:xmpp:unknown' /></extensions></conference>".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");

View file

@ -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 `<service xmlns='urn:xmpp:extdisco:2'/>` 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());

View file

@ -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 `<fast/>` 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 `<mechanism/>` 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 = "<fast xmlns='urn:xmpp:fast:0' count='123'/>"

View file

@ -140,13 +140,6 @@ pub struct IV {
pub data: Vec<u8>,
}
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,

View file

@ -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 = "<fin xmlns='urn:xmpp:mam:2'><set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set></fin>"
let reference: Element = "<fin xmlns='urn:xmpp:mam:2' complete='false'><set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set></fin>"
.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);

View file

@ -160,13 +160,6 @@ pub struct PublishOptions {
pub form: Option<DataForm>,
}
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 = ..))]

View file

@ -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 = "<enabled xmlns='urn:xmpp:sm:3' resume='true' id='coucou' max='600'/>"
.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);

View file

@ -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<Self, xso::error::Error> {
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::<bool>().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<Option<::std::borrow::Cow<'_, str>>, 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<String> {
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)]