diff --git a/src/data/client.rs b/src/data/client.rs index 6d631cc..7d6dd6b 100644 --- a/src/data/client.rs +++ b/src/data/client.rs @@ -10,6 +10,7 @@ use reqwest::header::CONTENT_TYPE; use minidom::Element; use url::Url; +use crate::utils::{find_elem, find_elems}; use crate::data::Calendar; static DAVCLIENT_BODY: &str = r#" @@ -194,41 +195,6 @@ impl Client { } -/// Walks the tree and returns every element that has the given name -pub fn find_elems>(root: &Element, searched_name: S) -> Vec<&Element> { - let searched_name = searched_name.as_ref(); - let mut elems: Vec<&Element> = Vec::new(); - - for el in root.children() { - if el.name() == searched_name { - elems.push(el); - } else { - let ret = find_elems(el, searched_name); - elems.extend(ret); - } - } - elems -} - -/// Walks the tree until it finds an elements with the given name -pub fn find_elem>(root: &Element, searched_name: S) -> Option<&Element> { - let searched_name = searched_name.as_ref(); - if root.name() == searched_name { - return Some(root); - } - - for el in root.children() { - if el.name() == searched_name { - return Some(el); - } else { - let ret = find_elem(el, searched_name); - if ret.is_some() { - return ret; - } - } - } - None -} diff --git a/src/lib.rs b/src/lib.rs index 19c950f..4635634 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod data; pub mod settings; +pub mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..ff93203 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,39 @@ +///! Some utility functions + +use minidom::Element; + +/// Walks an XML tree and returns every element that has the given name +pub fn find_elems>(root: &Element, searched_name: S) -> Vec<&Element> { + let searched_name = searched_name.as_ref(); + let mut elems: Vec<&Element> = Vec::new(); + + for el in root.children() { + if el.name() == searched_name { + elems.push(el); + } else { + let ret = find_elems(el, searched_name); + elems.extend(ret); + } + } + elems +} + +/// Walks an XML tree until it finds an elements with the given name +pub fn find_elem>(root: &Element, searched_name: S) -> Option<&Element> { + let searched_name = searched_name.as_ref(); + if root.name() == searched_name { + return Some(root); + } + + for el in root.children() { + if el.name() == searched_name { + return Some(el); + } else { + let ret = find_elem(el, searched_name); + if ret.is_some() { + return ret; + } + } + } + None +} \ No newline at end of file