1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use quick_xml::events::Event;
use quick_xml::Reader;
use std::collections::HashSet;
pub mod xpath;
pub fn paths(path: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let mut reader = Reader::from_file(path)?;
let mut xpath: xpath::XPath = xpath::XPath::new();
let mut buf = Vec::new();
let mut xpath_strings = HashSet::new();
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) => {
xpath.push(xpath::start_tag_string(e)?);
}
Ok(Event::End(ref e)) => {
xpath.pop_checked(xpath::end_tag_string(e)?);
}
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
xpath_strings.insert(xpath.as_string());
}
let mut xpath_strings: Vec<String> = xpath_strings.into_iter().collect();
xpath_strings.sort();
Ok(xpath_strings)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_paths() {
let paths = paths("example_data/notes.xml").unwrap();
assert_eq!(
paths,
vec![
"/",
"/notes",
"/notes/note",
"/notes/note/body",
"/notes/note/from",
"/notes/note/heading",
"/notes/note/to"
]
);
}
}