minidom: loosen type requirement on Element::attr{,_ns} getters

Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
pep 2025-10-21 19:34:37 +02:00
commit 15ac51829f
5 changed files with 52 additions and 85 deletions

View file

@ -1126,35 +1126,19 @@ mod tests {
#[test] #[test]
fn minidom() { fn minidom() {
let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap(); let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap();
let to: Jid = elem let to: Jid = elem.attr("from").unwrap().parse().unwrap();
.attr("from".try_into().unwrap())
.unwrap()
.parse()
.unwrap();
assert_eq!(to, Jid::from(FullJid::new("a@b/c").unwrap())); assert_eq!(to, Jid::from(FullJid::new("a@b/c").unwrap()));
let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap(); let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap();
let to: Jid = elem let to: Jid = elem.attr("from").unwrap().parse().unwrap();
.attr("from".try_into().unwrap())
.unwrap()
.parse()
.unwrap();
assert_eq!(to, Jid::from(BareJid::new("a@b").unwrap())); assert_eq!(to, Jid::from(BareJid::new("a@b").unwrap()));
let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap(); let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap();
let to: FullJid = elem let to: FullJid = elem.attr("from").unwrap().parse().unwrap();
.attr("from".try_into().unwrap())
.unwrap()
.parse()
.unwrap();
assert_eq!(to, FullJid::new("a@b/c").unwrap()); assert_eq!(to, FullJid::new("a@b/c").unwrap());
let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap(); let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap();
let to: BareJid = elem let to: BareJid = elem.attr("from").unwrap().parse().unwrap();
.attr("from".try_into().unwrap())
.unwrap()
.parse()
.unwrap();
assert_eq!(to, BareJid::new("a@b").unwrap()); assert_eq!(to, BareJid::new("a@b").unwrap());
} }
@ -1165,28 +1149,19 @@ mod tests {
let elem = minidom::Element::builder("message", "jabber:client") let elem = minidom::Element::builder("message", "jabber:client")
.attr("from".try_into().unwrap(), full.clone()) .attr("from".try_into().unwrap(), full.clone())
.build(); .build();
assert_eq!( assert_eq!(elem.attr("from"), Some(full.to_string().as_str()));
elem.attr("from".try_into().unwrap()),
Some(full.to_string().as_str())
);
let bare = BareJid::new("a@b").unwrap(); let bare = BareJid::new("a@b").unwrap();
let elem = minidom::Element::builder("message", "jabber:client") let elem = minidom::Element::builder("message", "jabber:client")
.attr("from".try_into().unwrap(), bare.clone()) .attr("from".try_into().unwrap(), bare.clone())
.build(); .build();
assert_eq!( assert_eq!(elem.attr("from"), Some(bare.to_string().as_str()));
elem.attr("from".try_into().unwrap()),
Some(bare.to_string().as_str())
);
let jid = Jid::from(bare.clone()); let jid = Jid::from(bare.clone());
let _elem = minidom::Element::builder("message", "jabber:client") let _elem = minidom::Element::builder("message", "jabber:client")
.attr("from".try_into().unwrap(), jid) .attr("from".try_into().unwrap(), jid)
.build(); .build();
assert_eq!( assert_eq!(elem.attr("from"), Some(bare.to_string().as_str()));
elem.attr("from".try_into().unwrap()),
Some(bare.to_string().as_str())
);
} }
#[test] #[test]

View file

@ -26,10 +26,12 @@ use alloc::vec::Vec;
use core::slice; use core::slice;
use core::str::FromStr; use core::str::FromStr;
use std::borrow::Borrow;
use std::hash::Hash;
use std::io; use std::io;
use rxml::writer::{Encoder, Item, TrackNamespace}; use rxml::writer::{Encoder, Item, TrackNamespace};
use rxml::{AttrMap, Namespace as RxmlNamespace, NcName, NcNameStr, RawReader, XmlVersion}; use rxml::{AttrMap, Namespace as RxmlNamespace, NcName, RawReader, XmlVersion};
fn encode_and_write<W: io::Write, T: rxml::writer::TrackNamespace>( fn encode_and_write<W: io::Write, T: rxml::writer::TrackNamespace>(
item: Item<'_>, item: Item<'_>,
@ -188,8 +190,8 @@ impl Element {
/// ///
/// assert_eq!(elem.name(), "name"); /// assert_eq!(elem.name(), "name");
/// assert_eq!(elem.ns(), "namespace".to_owned()); /// assert_eq!(elem.ns(), "namespace".to_owned());
/// assert_eq!(elem.attr(xml_ncname!("name")), Some("value")); /// assert_eq!(elem.attr("name"), Some("value"));
/// assert_eq!(elem.attr(xml_ncname!("inexistent")), None); /// assert_eq!(elem.attr("inexistent"), None);
/// assert_eq!(elem.text(), "inner"); /// assert_eq!(elem.text(), "inner");
/// ``` /// ```
pub fn builder<S: AsRef<str>, NS: Into<String>>(name: S, namespace: NS) -> ElementBuilder { pub fn builder<S: AsRef<str>, NS: Into<String>>(name: S, namespace: NS) -> ElementBuilder {
@ -210,13 +212,13 @@ impl Element {
/// ///
/// ```rust /// ```rust
/// use minidom::Element; /// use minidom::Element;
/// use rxml::{Namespace, xml_ncname}; /// use rxml::Namespace;
/// ///
/// let bare = Element::bare("name", "namespace"); /// let bare = Element::bare("name", "namespace");
/// ///
/// assert_eq!(bare.name(), "name"); /// assert_eq!(bare.name(), "name");
/// assert_eq!(bare.ns(), "namespace"); /// assert_eq!(bare.ns(), "namespace");
/// assert_eq!(bare.attr(xml_ncname!("name")), None); /// assert_eq!(bare.attr("name"), None);
/// assert_eq!(bare.text(), ""); /// assert_eq!(bare.text(), "");
/// ``` /// ```
pub fn bare<S: Into<String>, NS: Into<String>>(name: S, namespace: NS) -> Element { pub fn bare<S: Into<String>, NS: Into<String>>(name: S, namespace: NS) -> Element {
@ -243,7 +245,10 @@ impl Element {
/// Returns a reference to the value of the given attribute, if it exists, else `None`. /// Returns a reference to the value of the given attribute, if it exists, else `None`.
#[must_use] #[must_use]
pub fn attr<'a>(&'a self, name: &'a NcNameStr) -> Option<&'a str> { pub fn attr<'a, N: Ord + Hash + Eq + ?Sized>(&'a self, name: &'a N) -> Option<&'a str>
where
NcName: Borrow<N>,
{
if let Some(value) = self.attributes.get(&RxmlNamespace::NONE, name) { if let Some(value) = self.attributes.get(&RxmlNamespace::NONE, name) {
return Some(value); return Some(value);
} }
@ -252,7 +257,15 @@ impl Element {
/// Returns a reference to the value of the given namespaced attribute, if it exists, else `None`. /// Returns a reference to the value of the given namespaced attribute, if it exists, else `None`.
#[must_use] #[must_use]
pub fn attr_ns<'a>(&'a self, ns: &'a RxmlNamespace, name: &'a NcNameStr) -> Option<&'a str> { pub fn attr_ns<'a, NS: Ord + Hash + Eq + ?Sized, N: Ord + Hash + Eq + ?Sized>(
&'a self,
ns: &'a NS,
name: &'a N,
) -> Option<&'a str>
where
RxmlNamespace: Borrow<NS>,
NcName: Borrow<N>,
{
if let Some(value) = self.attributes.get(ns, name) { if let Some(value) = self.attributes.get(ns, name) {
return Some(value); return Some(value);
} }
@ -953,11 +966,8 @@ mod tests {
assert_eq!(elem.name(), "name"); assert_eq!(elem.name(), "name");
assert_eq!(elem.ns(), "namespace".to_owned()); assert_eq!(elem.ns(), "namespace".to_owned());
assert_eq!( assert_eq!(elem.attr_ns("namespace", "name"), Some("value"));
elem.attr_ns(&String::from("namespace").into(), xml_ncname!("name")), assert_eq!(elem.attr("inexistent"), None);
Some("value")
);
assert_eq!(elem.attr(xml_ncname!("inexistent")), None);
} }
#[test] #[test]

View file

@ -281,8 +281,8 @@ fn builder_works() {
.build(); .build();
assert_eq!(elem.name(), "a"); assert_eq!(elem.name(), "a");
assert_eq!(elem.ns(), "b".to_owned()); assert_eq!(elem.ns(), "b".to_owned());
assert_eq!(elem.attr(xml_ncname!("c")), Some("d")); assert_eq!(elem.attr("c"), Some("d"));
assert_eq!(elem.attr(xml_ncname!("x")), None); assert_eq!(elem.attr("x"), None);
assert_eq!(elem.text(), "e"); assert_eq!(elem.text(), "e");
assert!(elem.has_child("child", "b")); assert!(elem.has_child("child", "b"));
assert!(elem.is("a", "b")); assert!(elem.is("a", "b"));
@ -311,15 +311,11 @@ fn get_child_works() {
.unwrap() .unwrap()
.is("child", "child_ns")); .is("child", "child_ns"));
assert_eq!( assert_eq!(
root.get_child("child", "root_ns") root.get_child("child", "root_ns").unwrap().attr("c"),
.unwrap()
.attr(xml_ncname!("c")),
Some("d") Some("d")
); );
assert_eq!( assert_eq!(
root.get_child("child", "child_ns") root.get_child("child", "child_ns").unwrap().attr("d"),
.unwrap()
.attr(xml_ncname!("d")),
Some("e") Some("e")
); );
} }
@ -357,15 +353,12 @@ fn two_elements_with_same_arguments_different_order_are_equal() {
#[test] #[test]
fn namespace_attributes_works() { fn namespace_attributes_works() {
let root = Element::from_reader(TEST_STRING).unwrap(); let root = Element::from_reader(TEST_STRING).unwrap();
assert_eq!( assert_eq!(Some("en"), root.attr_ns(RxmlNamespace::xml(), "lang"));
Some("en"),
root.attr_ns(RxmlNamespace::xml(), xml_ncname!("lang"))
);
assert_eq!( assert_eq!(
"fr", "fr",
root.get_child("child", "child_ns") root.get_child("child", "child_ns")
.unwrap() .unwrap()
.attr_ns(RxmlNamespace::xml(), xml_ncname!("lang")) .attr_ns(RxmlNamespace::xml(), "lang")
.unwrap() .unwrap()
); );
} }

View file

@ -15,13 +15,13 @@ macro_rules! get_attr {
) )
}; };
($elem:ident, $attr:tt, Option, $value:ident, $func:expr) => { ($elem:ident, $attr:tt, Option, $value:ident, $func:expr) => {
match $elem.attr(::xso::exports::rxml::xml_ncname!($attr).into()) { match $elem.attr($attr) {
Some($value) => Some($func), Some($value) => Some($func),
None => None, None => None,
} }
}; };
($elem:ident, $attr:tt, Required, $value:ident, $func:expr) => { ($elem:ident, $attr:tt, Required, $value:ident, $func:expr) => {
match $elem.attr(::xso::exports::rxml::xml_ncname!($attr).into()) { match $elem.attr($attr) {
Some($value) => $func, Some($value) => $func,
None => { None => {
return Err(xso::error::Error::Other( return Err(xso::error::Error::Other(
@ -32,7 +32,7 @@ macro_rules! get_attr {
} }
}; };
($elem:ident, $attr:tt, Default, $value:ident, $func:expr) => { ($elem:ident, $attr:tt, Default, $value:ident, $func:expr) => {
match $elem.attr(::xso::exports::rxml::xml_ncname!($attr).into()) { match $elem.attr($attr) {
Some($value) => $func, Some($value) => $func,
None => ::core::default::Default::default(), None => ::core::default::Default::default(),
} }

View file

@ -77,7 +77,7 @@ impl TryFrom<Element> for XhtmlIm {
if child.is("body", ns::XHTML) { if child.is("body", ns::XHTML) {
let child = child.clone(); let child = child.clone();
let lang = child let lang = child
.attr_ns(rxml::Namespace::xml(), rxml::xml_ncname!("lang").into()) .attr_ns(rxml::Namespace::xml(), "lang")
.unwrap_or("") .unwrap_or("")
.to_string(); .to_string();
let body = Body::try_from(child)?; let body = Body::try_from(child)?;
@ -168,12 +168,9 @@ impl TryFrom<Element> for Body {
} }
Ok(Body { Ok(Body {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
xml_lang: elem xml_lang: elem
.attr_ns( .attr_ns("xml", "lang")
&Into::<Namespace>::into(String::from("xml")),
rxml::xml_ncname!("lang").into(),
)
.map(|xml_lang| xml_lang.to_string()), .map(|xml_lang| xml_lang.to_string()),
children, children,
}) })
@ -328,52 +325,44 @@ impl TryFrom<Element> for Tag {
Ok(match elem.name() { Ok(match elem.name() {
"a" => Tag::A { "a" => Tag::A {
href: elem href: elem.attr("href").map(|href| href.to_string()),
.attr(rxml::xml_ncname!("href"))
.map(|href| href.to_string()),
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr(rxml::xml_ncname!("style"))),
type_: elem type_: elem.attr("type").map(|type_| type_.to_string()),
.attr(rxml::xml_ncname!("type"))
.map(|type_| type_.to_string()),
children, children,
}, },
"blockquote" => Tag::Blockquote { "blockquote" => Tag::Blockquote {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"br" => Tag::Br, "br" => Tag::Br,
"cite" => Tag::Cite { "cite" => Tag::Cite {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"em" => Tag::Em { children }, "em" => Tag::Em { children },
"img" => Tag::Img { "img" => Tag::Img {
src: elem src: elem.attr("src").map(|src| src.to_string()),
.attr(rxml::xml_ncname!("src")) alt: elem.attr("alt").map(|alt| alt.to_string()),
.map(|src| src.to_string()),
alt: elem
.attr(rxml::xml_ncname!("alt"))
.map(|alt| alt.to_string()),
}, },
"li" => Tag::Li { "li" => Tag::Li {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"ol" => Tag::Ol { "ol" => Tag::Ol {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"p" => Tag::P { "p" => Tag::P {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"span" => Tag::Span { "span" => Tag::Span {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
"strong" => Tag::Strong { children }, "strong" => Tag::Strong { children },
"ul" => Tag::Ul { "ul" => Tag::Ul {
style: parse_css(elem.attr(rxml::xml_ncname!("style"))), style: parse_css(elem.attr("style")),
children, children,
}, },
_ => Tag::Unknown(children), _ => Tag::Unknown(children),