minidom: Fix some issues reported by clippy

This commit is contained in:
Emmanuel Gil Peyrot 2021-10-11 15:34:07 +02:00
parent 464b5de0d0
commit 4e914e5d3a
2 changed files with 11 additions and 24 deletions

View file

@ -41,10 +41,7 @@ pub fn escape(raw: &[u8]) -> Cow<[u8]> {
let mut escapes: Vec<(usize, &'static [u8])> = Vec::new(); let mut escapes: Vec<(usize, &'static [u8])> = Vec::new();
let mut bytes = raw.iter(); let mut bytes = raw.iter();
fn to_escape(b: u8) -> bool { fn to_escape(b: u8) -> bool {
match b { matches!(b, b'<' | b'>' | b'\'' | b'&' | b'"')
b'<' | b'>' | b'\'' | b'&' | b'"' => true,
_ => false,
}
} }
let mut loc = 0; let mut loc = 0;
@ -124,8 +121,7 @@ impl PartialEq for Element {
} }
fn ensure_no_prefix<S: AsRef<str>>(s: &S) -> Result<()> { fn ensure_no_prefix<S: AsRef<str>>(s: &S) -> Result<()> {
let name_parts = s.as_ref().split(':').collect::<Vec<&str>>(); match s.as_ref().split(':').count() {
match name_parts.len() {
1 => Ok(()), 1 => Ok(()),
_ => Err(Error::InvalidElement), _ => Err(Error::InvalidElement),
} }
@ -397,9 +393,8 @@ impl Element {
// There was no prefix on the closing tag // There was no prefix on the closing tag
None => { None => {
// Is there a prefix on the opening tag? // Is there a prefix on the opening tag?
match opening_prefix { if opening_prefix.is_some() {
Some(_) => return Err(Error::InvalidElementClosed), return Err(Error::InvalidElementClosed);
_ => (),
} }
// Does the opening tag name match the closing one? // Does the opening tag name match the closing one?
if possible_prefix != elem.name().as_bytes() { if possible_prefix != elem.name().as_bytes() {
@ -412,14 +407,14 @@ impl Element {
} }
Event::Text(s) => { Event::Text(s) => {
let text = s.unescape_and_decode(reader)?; let text = s.unescape_and_decode(reader)?;
if text != "" { if !text.is_empty() {
let current_elem = stack.last_mut().unwrap(); let current_elem = stack.last_mut().unwrap();
current_elem.append_text_node(text); current_elem.append_text_node(text);
} }
} }
Event::CData(s) => { Event::CData(s) => {
let text = reader.decode(&s)?.to_owned(); let text = reader.decode(&s)?.to_owned();
if text != "" { if !text.is_empty() {
let current_elem = stack.last_mut().unwrap(); let current_elem = stack.last_mut().unwrap();
current_elem.append_text_node(text); current_elem.append_text_node(text);
} }
@ -477,14 +472,14 @@ impl Element {
let self_prefix: (Option<String>, bool) = match existing_self_prefix { let self_prefix: (Option<String>, bool) = match existing_self_prefix {
// No prefix exists already for our namespace // No prefix exists already for our namespace
None => { None => {
if local_keys.find(|p| p == &None).is_none() { if !local_keys.any(|p| p.is_none()) {
// Use the None prefix if available // Use the None prefix if available
(None, true) (None, true)
} else { } else {
// Otherwise generate one. Check if it isn't already used, if so increase the // Otherwise generate one. Check if it isn't already used, if so increase the
// number until we find a suitable one. // number until we find a suitable one.
let mut prefix_n = 0u8; let mut prefix_n = 0u8;
while let Some(_) = all_keys.find(|p| p == &Some(format!("ns{}", prefix_n))) { while all_keys.any(|p| p == Some(format!("ns{}", prefix_n))) {
prefix_n += 1; prefix_n += 1;
} }
(Some(format!("ns{}", prefix_n)), true) (Some(format!("ns{}", prefix_n)), true)
@ -510,7 +505,7 @@ impl Element {
all_prefixes.insert(self_prefix.0, self.namespace.clone()); all_prefixes.insert(self_prefix.0, self.namespace.clone());
} }
(None, true) => { (None, true) => {
let key = format!("xmlns"); let key = String::from("xmlns");
start.push_attribute((key.as_bytes(), self.namespace.as_bytes())); start.push_attribute((key.as_bytes(), self.namespace.as_bytes()));
all_prefixes.insert(self_prefix.0, self.namespace.clone()); all_prefixes.insert(self_prefix.0, self.namespace.clone());
} }
@ -1007,7 +1002,7 @@ impl ElementBuilder {
prefix: Prefix, prefix: Prefix,
namespace: S, namespace: S,
) -> Result<ElementBuilder> { ) -> Result<ElementBuilder> {
if let Some(_) = self.root.prefixes.get(&prefix) { if self.root.prefixes.get(&prefix).is_some() {
return Err(Error::DuplicatePrefix); return Err(Error::DuplicatePrefix);
} }
self.root.prefixes.insert(prefix, namespace.into()); self.root.prefixes.insert(prefix, namespace.into());

View file

@ -13,19 +13,11 @@ use std::fmt;
pub type Prefix = Option<String>; pub type Prefix = Option<String>;
pub type Namespace = String; pub type Namespace = String;
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, Default, PartialEq, Eq)]
pub struct Prefixes { pub struct Prefixes {
prefixes: BTreeMap<Prefix, Namespace>, prefixes: BTreeMap<Prefix, Namespace>,
} }
impl Default for Prefixes {
fn default() -> Self {
Prefixes {
prefixes: BTreeMap::new(),
}
}
}
impl fmt::Debug for Prefixes { impl fmt::Debug for Prefixes {
// TODO: Fix end character // TODO: Fix end character
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {