From 8042d6ea23ec8df24a560652a21b0b3a428fb17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 9 Nov 2019 12:45:31 +0100 Subject: [PATCH] minidom: Move compare_ns out of NamespaceSet into NSChoice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- minidom-rs/src/namespace_set.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/minidom-rs/src/namespace_set.rs b/minidom-rs/src/namespace_set.rs index 7efdf8b..24eacd1 100644 --- a/minidom-rs/src/namespace_set.rs +++ b/minidom-rs/src/namespace_set.rs @@ -22,6 +22,19 @@ impl<'a> From<&'a str> for NSChoice<'a> { } } +impl<'a> NSChoice<'a> { + fn compare(&self, ns: Option<&str>) -> bool { + match (ns, &self) { + (None, NSChoice::None) | (None, NSChoice::Any) => true, + (None, NSChoice::OneOf(_)) | (None, NSChoice::AnyOf(_)) => false, + (Some(_), NSChoice::None) => false, + (Some(_), NSChoice::Any) => true, + (Some(ns), NSChoice::OneOf(wanted_ns)) => &ns == wanted_ns, + (Some(ns), NSChoice::AnyOf(wanted_nss)) => wanted_nss.iter().any(|w| &ns == w), + } + } +} + #[derive(Clone, PartialEq, Eq)] pub struct NamespaceSet { parent: RefCell>>, @@ -70,22 +83,11 @@ impl NamespaceSet { } } - fn compare_ns<'a>(&self, ns: Option<&str>, wanted_ns: NSChoice<'a>) -> bool { - match (ns, wanted_ns) { - (None, NSChoice::None) | (None, NSChoice::Any) => true, - (None, NSChoice::OneOf(_)) | (None, NSChoice::AnyOf(_)) => false, - (Some(_), NSChoice::None) => false, - (Some(_), NSChoice::Any) => true, - (Some(ns), NSChoice::OneOf(wanted_ns)) => ns == wanted_ns, - (Some(ns), NSChoice::AnyOf(wanted_nss)) => wanted_nss.iter().any(|w| &ns == w), - } - } - pub fn has<'a, NS: Into>>(&self, prefix: &Option, wanted_ns: NS) -> bool { match self.namespaces.get(prefix) { - Some(ns) => self.compare_ns(Some(ns), wanted_ns.into()), + Some(ns) => wanted_ns.into().compare(Some(ns)), None => match *self.parent.borrow() { - None => self.compare_ns(None, wanted_ns.into()), + None => wanted_ns.into().compare(None), Some(ref parent) => parent.has(prefix, wanted_ns), }, }