From ffb7a97f41a10a7b2e1bd14e854676459512cbf3 Mon Sep 17 00:00:00 2001 From: Thomas Tanon Date: Sun, 23 Aug 2026 19:00:10 +0200 Subject: [PATCH] Implement PartialEq on Namespace, Prefix and LocalName Allows to easily write comparisons on both --- Changelog.md | 5 +++++ src/name.rs | 46 +++++++++++++++++++++++++++++++++++++-- src/reader/async_tokio.rs | 4 ++-- src/reader/ns_reader.rs | 8 +++---- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index 28137215..b86f7d7c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -18,6 +18,9 @@ ### New Features +- [#1010]: Implement `fmt::Display` on `QName`, `LocalName`, `Prefix` and `Namespace`. +- [#1011]: Implement `PartialEq<&str>` on `LocalName`, `Prefix` and `Namespace`. + ### Bug Fixes - [#1007]: Remove error-prone implementations of `Hash`, `PartialOrd`, and `Ord` from all @@ -31,6 +34,8 @@ [#1007]: https://github.com/tafia/quick-xml/pull/1007 [#1009]: https://github.com/tafia/quick-xml/pull/1009 +[#1010]: https://github.com/tafia/quick-xml/pull/1010 +[#1011]: https://github.com/tafia/quick-xml/pull/1011 ## 0.42.0 -- 2026-08-22 diff --git a/src/name.rs b/src/name.rs index bd3de8cb..80677c0a 100644 --- a/src/name.rs +++ b/src/name.rs @@ -267,6 +267,20 @@ impl<'a> AsRef for LocalName<'a> { } } +impl PartialEq<&str> for LocalName<'_> { + #[inline] + fn eq(&self, other: &&str) -> bool { + self.0 == *other + } +} + +impl PartialEq> for &str { + #[inline] + fn eq(&self, other: &LocalName<'_>) -> bool { + *self == other.0 + } +} + impl<'a> From> for LocalName<'a> { /// Creates `LocalName` from a [`QName`] /// @@ -337,6 +351,20 @@ impl<'a> AsRef for Prefix<'a> { } } +impl PartialEq<&str> for Prefix<'_> { + #[inline] + fn eq(&self, other: &&str) -> bool { + self.0 == *other + } +} + +impl PartialEq> for &str { + #[inline] + fn eq(&self, other: &Prefix<'_>) -> bool { + *self == other.0 + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// /// A namespace prefix declaration, `xmlns` or `xmlns:`, as defined in @@ -421,6 +449,20 @@ impl<'a> AsRef for Namespace<'a> { } } +impl PartialEq<&str> for Namespace<'_> { + #[inline] + fn eq(&self, other: &&str) -> bool { + self.0 == *other + } +} + +impl PartialEq> for &str { + #[inline] + fn eq(&self, other: &Namespace<'_>) -> bool { + *self == other.0 + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// /// Result of [prefix] resolution which creates by [`NamespaceResolver::resolve`], @@ -965,11 +1007,11 @@ impl NamespaceResolver { /// match reader.resolver().resolve_event(event) { /// (Bound(Namespace("www.xxxx")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag1").into()); + /// assert_eq!(e.local_name(), "tag1"); /// } /// (Bound(Namespace("www.yyyy")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag2").into()); + /// assert_eq!(e.local_name(), "tag2"); /// } /// (_, Event::Start(_)) => unreachable!(), /// diff --git a/src/reader/async_tokio.rs b/src/reader/async_tokio.rs index f4b3dc5a..4a5f7558 100644 --- a/src/reader/async_tokio.rs +++ b/src/reader/async_tokio.rs @@ -529,11 +529,11 @@ impl NsReader { /// match reader.read_resolved_event_into_async(&mut buf).await.unwrap() { /// (Bound(Namespace("www.xxxx")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag1").into()); + /// assert_eq!(e.local_name(), "tag1"); /// } /// (Bound(Namespace("www.yyyy")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag2").into()); + /// assert_eq!(e.local_name(), "tag2"); /// } /// (_, Event::Start(_)) => unreachable!(), /// diff --git a/src/reader/ns_reader.rs b/src/reader/ns_reader.rs index e7b1c6c7..43a254f7 100644 --- a/src/reader/ns_reader.rs +++ b/src/reader/ns_reader.rs @@ -223,11 +223,11 @@ impl NsReader { /// match reader.read_resolved_event_into(&mut buf).unwrap() { /// (Bound(Namespace("www.xxxx")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag1").into()); + /// assert_eq!(e.local_name(), "tag1"); /// } /// (Bound(Namespace("www.yyyy")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag2").into()); + /// assert_eq!(e.local_name(), "tag2"); /// } /// (_, Event::Start(_)) => unreachable!(), /// @@ -555,11 +555,11 @@ impl<'i> NsReader<&'i [u8]> { /// match reader.read_resolved_event().unwrap() { /// (Bound(Namespace("www.xxxx")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag1").into()); + /// assert_eq!(e.local_name(), "tag1"); /// } /// (Bound(Namespace("www.yyyy")), Event::Start(e)) => { /// count += 1; - /// assert_eq!(e.local_name(), QName("tag2").into()); + /// assert_eq!(e.local_name(), "tag2"); /// } /// (_, Event::Start(_)) => unreachable!(), ///