minidom: Use NSChoice in more places

This commit is contained in:
Xidorn Quan 2019-11-26 22:47:35 +11:00
parent c4c285d3d5
commit 219b0bbe87
2 changed files with 37 additions and 17 deletions

View file

@ -258,7 +258,7 @@ impl Element {
/// assert_eq!(elem.is("name", NSChoice::None), false); /// assert_eq!(elem.is("name", NSChoice::None), false);
/// assert_eq!(elem.is("name", NSChoice::OneOf("namespace")), true); /// assert_eq!(elem.is("name", NSChoice::OneOf("namespace")), true);
/// assert_eq!(elem.is("name", NSChoice::OneOf("foo")), false); /// assert_eq!(elem.is("name", NSChoice::OneOf("foo")), false);
/// assert_eq!(elem.is("name", NSChoice::AnyOf(vec!["foo", "namespace"])), true); /// assert_eq!(elem.is("name", NSChoice::AnyOf(&["foo", "namespace"])), true);
/// assert_eq!(elem.is("name", NSChoice::Any), true); /// assert_eq!(elem.is("name", NSChoice::Any), true);
/// ///
/// let elem2 = Element::builder("name").build(); /// let elem2 = Element::builder("name").build();
@ -285,7 +285,7 @@ impl Element {
/// assert_eq!(elem.has_ns(NSChoice::None), false); /// assert_eq!(elem.has_ns(NSChoice::None), false);
/// assert_eq!(elem.has_ns(NSChoice::OneOf("namespace")), true); /// assert_eq!(elem.has_ns(NSChoice::OneOf("namespace")), true);
/// assert_eq!(elem.has_ns(NSChoice::OneOf("foo")), false); /// assert_eq!(elem.has_ns(NSChoice::OneOf("foo")), false);
/// assert_eq!(elem.has_ns(NSChoice::AnyOf(vec!["foo", "namespace"])), true); /// assert_eq!(elem.has_ns(NSChoice::AnyOf(&["foo", "namespace"])), true);
/// assert_eq!(elem.has_ns(NSChoice::Any), true); /// assert_eq!(elem.has_ns(NSChoice::Any), true);
/// ///
/// let elem2 = Element::builder("name").build(); /// let elem2 = Element::builder("name").build();
@ -661,25 +661,31 @@ impl Element {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use minidom::Element; /// use minidom::{Element, NSChoice};
/// ///
/// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap(); /// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
///
/// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns")); /// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns"));
/// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns")); /// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns"));
/// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns")); /// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns"));
/// assert_eq!(elem.get_child("c", "ns"), None); /// assert_eq!(elem.get_child("c", "ns"), None);
/// assert_eq!(elem.get_child("b", "other_ns"), None); /// assert_eq!(elem.get_child("b", "other_ns"), None);
/// assert_eq!(elem.get_child("a", "inexistent_ns"), None); /// assert_eq!(elem.get_child("a", "inexistent_ns"), None);
///
/// let elem: Element = r#"<node><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
/// assert_eq!(elem.get_child("a", NSChoice::None), None);
/// assert!(elem.get_child("a", NSChoice::Any).unwrap().is("a", "other_ns"));
/// assert!(elem.get_child("b", NSChoice::None).unwrap().is("b", NSChoice::None));
/// assert!(elem.get_child("b", NSChoice::Any).unwrap().is("b", NSChoice::None));
/// ``` /// ```
pub fn get_child<N: AsRef<str>, NS: AsRef<str>>( pub fn get_child<'a, N: AsRef<str>, NS: Into<NSChoice<'a>>>(
&self, &self,
name: N, name: N,
namespace: NS, namespace: NS,
) -> Option<&Element> { ) -> Option<&Element> {
let namespace = namespace.into();
for fork in &self.children { for fork in &self.children {
if let Node::Element(ref e) = *fork { if let Node::Element(ref e) = *fork {
if e.is(name.as_ref(), namespace.as_ref()) { if e.is(name.as_ref(), namespace) {
return Some(e); return Some(e);
} }
} }
@ -689,14 +695,15 @@ impl Element {
/// Returns a mutable reference to the first child element with the specific name and namespace, /// Returns a mutable reference to the first child element with the specific name and namespace,
/// if it exists in the direct descendants of this `Element`, else returns `None`. /// if it exists in the direct descendants of this `Element`, else returns `None`.
pub fn get_child_mut<N: AsRef<str>, NS: AsRef<str>>( pub fn get_child_mut<'a, N: AsRef<str>, NS: Into<NSChoice<'a>>>(
&mut self, &mut self,
name: N, name: N,
namespace: NS, namespace: NS,
) -> Option<&mut Element> { ) -> Option<&mut Element> {
let namespace = namespace.into();
for fork in &mut self.children { for fork in &mut self.children {
if let Node::Element(ref mut e) = *fork { if let Node::Element(ref mut e) = *fork {
if e.is(name.as_ref(), namespace.as_ref()) { if e.is(name.as_ref(), namespace) {
return Some(e); return Some(e);
} }
} }
@ -710,18 +717,27 @@ impl Element {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use minidom::Element; /// use minidom::{Element, NSChoice};
/// ///
/// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap(); /// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
///
/// assert_eq!(elem.has_child("a", "other_ns"), true); /// assert_eq!(elem.has_child("a", "other_ns"), true);
/// assert_eq!(elem.has_child("a", "ns"), true); /// assert_eq!(elem.has_child("a", "ns"), true);
/// assert_eq!(elem.has_child("a", "inexistent_ns"), false); /// assert_eq!(elem.has_child("a", "inexistent_ns"), false);
/// assert_eq!(elem.has_child("b", "ns"), true); /// assert_eq!(elem.has_child("b", "ns"), true);
/// assert_eq!(elem.has_child("b", "other_ns"), false); /// assert_eq!(elem.has_child("b", "other_ns"), false);
/// assert_eq!(elem.has_child("b", "inexistent_ns"), false); /// assert_eq!(elem.has_child("b", "inexistent_ns"), false);
///
/// let elem: Element = r#"<node><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
/// assert_eq!(elem.has_child("a", NSChoice::None), false);
/// assert_eq!(elem.has_child("a", NSChoice::OneOf("other_ns")), true);
/// assert_eq!(elem.has_child("a", NSChoice::Any), true);
/// assert_eq!(elem.has_child("b", NSChoice::None), true);
/// ``` /// ```
pub fn has_child<N: AsRef<str>, NS: AsRef<str>>(&self, name: N, namespace: NS) -> bool { pub fn has_child<'a, N: AsRef<str>, NS: Into<NSChoice<'a>>>(
&self,
name: N,
namespace: NS,
) -> bool {
self.get_child(name, namespace).is_some() self.get_child(name, namespace).is_some()
} }
@ -732,21 +748,25 @@ impl Element {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use minidom::Element; /// use minidom::{Element, NSChoice};
/// ///
/// let mut elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap(); /// let mut elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
///
/// assert!(elem.remove_child("a", "ns").unwrap().is("a", "ns")); /// assert!(elem.remove_child("a", "ns").unwrap().is("a", "ns"));
/// assert!(elem.remove_child("a", "ns").is_none()); /// assert!(elem.remove_child("a", "ns").is_none());
/// assert!(elem.remove_child("inexistent", "inexistent").is_none()); /// assert!(elem.remove_child("inexistent", "inexistent").is_none());
///
/// let mut elem: Element = r#"<node><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
/// assert!(elem.remove_child("a", NSChoice::None).is_none());
/// assert!(elem.remove_child("a", NSChoice::Any).unwrap().is("a", "other_ns"));
/// assert!(elem.remove_child("b", NSChoice::None).unwrap().is("b", NSChoice::None));
/// ``` /// ```
pub fn remove_child<N: AsRef<str>, NS: AsRef<str>>( pub fn remove_child<'a, N: AsRef<str>, NS: Into<NSChoice<'a>>>(
&mut self, &mut self,
name: N, name: N,
namespace: NS, namespace: NS,
) -> Option<Element> { ) -> Option<Element> {
let name = name.as_ref(); let name = name.as_ref();
let namespace = namespace.as_ref(); let namespace = namespace.into();
let idx = self.children.iter().position(|x| { let idx = self.children.iter().position(|x| {
if let Node::Element(ref elm) = x { if let Node::Element(ref elm) = x {
elm.is(name, namespace) elm.is(name, namespace)

View file

@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// Use to compare namespaces /// Use to compare namespaces
pub enum NSChoice<'a> { pub enum NSChoice<'a> {
/// The element must have no namespace /// The element must have no namespace
@ -11,7 +11,7 @@ pub enum NSChoice<'a> {
/// The element's namespace must match the specified namespace /// The element's namespace must match the specified namespace
OneOf(&'a str), OneOf(&'a str),
/// The element's namespace must be in the specified vector /// The element's namespace must be in the specified vector
AnyOf(Vec<&'a str>), AnyOf(&'a [&'a str]),
/// The element can have any namespace, or no namespace /// The element can have any namespace, or no namespace
Any, Any,
} }