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
53
54
55
56
57
58
59
use quick_xml::events::BytesEnd;
use quick_xml::events::BytesStart;
use std::fmt;
pub fn start_tag_string(bytes_start: &BytesStart) -> Result<String, Box<dyn std::error::Error>> {
let tag = bytes_start.name();
let tag = tag.to_owned();
let tag = String::from_utf8(tag)?;
Ok(tag)
}
pub fn end_tag_string(bytes_end: &BytesEnd) -> Result<String, Box<dyn std::error::Error>> {
let tag = bytes_end.name();
let tag = tag.to_owned();
let tag = String::from_utf8(tag)?;
Ok(tag)
}
pub struct XPath(Vec<String>);
impl XPath {
pub fn new() -> Self {
Self(vec!["".to_owned()])
}
pub fn push(&mut self, tag: String) {
self.0.push(tag);
}
pub fn pop(&mut self) -> Option<String> {
self.0.pop()
}
pub fn pop_checked(&mut self, tag: String) {
assert_eq!(self.pop().expect("can't end without starting."), tag);
}
pub fn as_string(&self) -> String {
let nodes = &self.0;
if nodes.len() == 1 {
"/".to_owned()
} else {
self.0.join("/")
}
}
}
impl Default for XPath {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for XPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl fmt::Display for XPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_string())
}
}