From 67fc8e7fb80b9b88c00be037b3a322cb36f8df72 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 2 Feb 2022 03:37:39 +0000 Subject: [PATCH] node: cleanup more unnecessary helper methods --- kbinxml/src/node/mod.rs | 67 +++++++++++------------------------------ 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/kbinxml/src/node/mod.rs b/kbinxml/src/node/mod.rs index 87fee15..60a75e9 100644 --- a/kbinxml/src/node/mod.rs +++ b/kbinxml/src/node/mod.rs @@ -160,19 +160,14 @@ impl Node { self.value.as_mut() } - pub fn attr(&self, key: &str) -> Option<&str> { - self.attributes.get(key).map(String::as_str) - } - - pub fn attr_mut(&mut self, key: &str) -> Option<&mut String> { - self.attributes.get_mut(key) - } - - pub fn into_key_and_value(self) -> (String, Option) { + pub fn into_key_value(self) -> (String, Option) { (self.key, self.value) } - pub fn set_key>(&mut self, key: K) { + pub fn set_key(&mut self, key: K) + where + K: Into, + { self.key = key.into(); } @@ -184,10 +179,6 @@ impl Node { self.attributes.insert(key.into(), value.into()) } - pub fn remove_attr(&mut self, key: &str) -> Option { - self.attributes.swap_remove(key) - } - pub fn sort_attrs(&mut self) { self.attributes.sort_keys(); } @@ -201,59 +192,37 @@ impl Node { } pub fn has(&self, key: &str) -> bool { - for node in self.children.iter() { - if node.key == key { - return true; - } - } - - false + self.children.iter().any(|node| node.key == key) } pub fn get_child(&self, key: &str) -> Option<&Node> { - for node in self.children.iter() { - if node.key == key { - return Some(node); - } - } - - None + self.children.iter().find(|node| node.key == key) } pub fn get_child_mut(&mut self, key: &str) -> Option<&mut Node> { - for node in self.children.iter_mut() { - if node.key == key { - return Some(node); - } - } - - None + self.children.iter_mut().find(|node| node.key == key) } pub fn remove_child(&mut self, key: &str) -> Option { - let index = self.children.iter().position(|child| child.key() == key); - - if let Some(index) = index { - return Some(self.children.remove(index)); + if let Some(index) = self.children.iter().position(|node| node.key == key) { + Some(self.children.remove(index)) + } else { + None } - - None } pub fn pointer<'a>(&'a self, pointer: &[&str]) -> Option<&'a Node> { if pointer.is_empty() { return Some(self); } + let mut target = self; for token in pointer { let target_opt = if let Some(index) = parse_index(token) { target.children.get(index) } else { - target - .children - .iter() - .find(|ref child| child.key() == *token) + target.children.iter().find(|child| child.key == *token) }; if let Some(t) = target_opt { @@ -262,6 +231,7 @@ impl Node { return None; } } + Some(target) } @@ -269,16 +239,14 @@ impl Node { if pointer.is_empty() { return Some(self); } + let mut target = self; for token in pointer { let target_opt = if let Some(index) = parse_index(token) { target.children.get_mut(index) } else { - target - .children - .iter_mut() - .find(|ref child| child.key() == *token) + target.children.iter_mut().find(|child| child.key == *token) }; if let Some(t) = target_opt { @@ -287,6 +255,7 @@ impl Node { return None; } } + Some(target) } }