xml_sax/
lib.rs

1// Old Push API
2// pub trait SAXAttributes {
3//     fn get_length(&self) -> usize;
4//     // Need for boxing https://doc.rust-lang.org/book/trait-objects.html
5//     fn iter(&self) -> Box<dyn Iterator<Item = Box<dyn SAXAttribute>>>;
6//     // fn get_by_qualified_name(&mut self, index: usize) -> Option<Attribute>;
7//     // when using trait as object use box otherwise:
8//     // the trait `std::marker::Sized` is not implemented for `sax::SAXAttribute + 'static`
9//     fn get_by_index(&self, index: usize) -> Option<Box<dyn SAXAttribute>>;
10// }
11
12// pub trait SAXAttribute {
13//     // fn get_index() -> usize;
14//     fn get_value(&self) -> &str;
15//     fn get_local_name(&self) -> &str;
16//     fn get_qualified_name(&self) -> &str;
17//     fn get_uri(&self) -> &str;
18// }
19
20// pub trait ContentHandler {
21//     fn start_document(&mut self);
22//     fn end_document(&mut self);
23
24//     fn start_element(
25//         &mut self,
26//         uri: &str,
27//         local_name: &str,
28//         qualified_name: &str,
29//         attributes: &dyn SAXAttributes,
30//     );
31//     fn end_element(&mut self, uri: &str, local_name: &str, qualified_name: &str);
32//     fn characters(&mut self, characters: &str);
33
34//     //fn start_prefix_mapping(&mut self, prefix: &str , uri: &str);
35//     //fn end_prefix_mapping(&mut self, prefix: &str , uri: &str);
36// }
37
38// pub trait StatsHandler {
39//     fn offset(&mut self, offset: usize);
40// }
41//ErrorHandler
42
43// Pull API
44
45#[derive(Clone, Debug, Eq, PartialEq)]
46pub struct Attribute<'a> {
47    pub value: &'a str,
48    pub name: &'a str,
49    // namespace aware
50    pub local_name: &'a str,
51    pub prefix: &'a str,
52    pub namespace: &'a str,
53}
54#[derive(Clone, Debug, Eq, PartialEq)]
55pub struct StartElement<'a> {
56    pub name: &'a str,
57    pub attributes: Vec<Attribute<'a>>,
58    pub is_empty: bool,
59    // namespace aware
60    pub local_name: &'a str,
61    pub prefix: &'a str,
62    pub namespace: &'a str,
63}
64
65#[derive(Clone, Debug, Eq, PartialEq)]
66pub struct EndElement<'a> {
67    pub name: &'a str,
68    // namespace aware
69    pub local_name: &'a str,
70    pub prefix: &'a str,
71    pub namespace: &'a str,
72}
73
74#[derive(Clone, Debug, Eq, PartialEq)]
75pub struct Reference<'a> {
76    pub raw: &'a str,
77    pub resolved: Option<&'a str>,
78}
79
80#[derive(Clone, Debug, Eq, PartialEq)]
81pub enum Event<'a> {
82    StartDocument,
83    EndDocument,
84
85    StartElement(StartElement<'a>),
86    EndElement(EndElement<'a>),
87    Characters(&'a str),
88    Reference(Reference<'a>),
89
90    StartComment,
91    Comment(&'a str),
92    EndComment,
93
94    StartCdataSection,
95    Cdata(&'a str),
96    EndCdataSection,
97
98    DocumentTypeDeclaration(&'a str),
99    ProcessingInstruction(&'a str),
100    XmlDeclaration(&'a str),
101    Whitespace(&'a str),
102}