1use core::iter::FusedIterator;
7use core::result;
8
9use crate::common::{Position, TextPosition};
10
11pub use self::config::ParserConfig;
12pub use self::config::ParserConfig2;
13pub use self::error::{Error, ErrorKind};
14pub use self::events::XmlEvent;
15
16use self::parser::PullParser;
17
18mod config;
19mod events;
20mod lexer;
21mod parser;
22mod error;
23
24
25pub type Result<T, E = Error> = result::Result<T, E>;
27
28pub struct EventReader<'a, S: Iterator<Item = &'a u8>> {
30 source: S,
31 parser: PullParser,
32}
33
34impl<'a, S: Iterator<Item = &'a u8>> EventReader<'a, S> {
35 #[inline]
37 pub fn new(source: S) -> EventReader<'a, S> {
38 EventReader::new_with_config(source, ParserConfig2::new())
39 }
40
41 #[inline]
43 pub fn new_with_config(source: S, config: impl Into<ParserConfig2>) -> EventReader<'a, S> {
44 EventReader { source, parser: PullParser::new(config) }
45 }
46
47 #[inline]
52 pub fn next(&mut self) -> Result<XmlEvent> {
53 self.parser.next(&mut self.source)
54 }
55
56 #[inline]
62 pub fn skip(&mut self) -> Result<()> {
63 let mut depth = 1;
64
65 while depth > 0 {
66 match self.next()? {
67 XmlEvent::StartElement { .. } => depth += 1,
68 XmlEvent::EndElement { .. } => depth -= 1,
69 XmlEvent::EndDocument => return Err(Error {
70 kind: ErrorKind::UnexpectedEof,
71 pos: self.parser.position(),
72 }),
73 _ => {},
74 }
75 }
76
77 Ok(())
78 }
79
80 pub fn source(&self) -> &S { &self.source }
81 pub fn source_mut(&mut self) -> &mut S { &mut self.source }
82
83 pub fn into_inner(self) -> S {
85 self.source
86 }
87}
88
89impl<'a, S: Iterator<Item = &'a u8>> Position for EventReader<'a, S> {
90 #[inline]
92 fn position(&self) -> TextPosition {
93 self.parser.position()
94 }
95}
96
97impl<'a, S: Iterator<Item = &'a u8>> IntoIterator for EventReader<'a, S> {
98 type Item = Result<XmlEvent>;
99 type IntoIter = Events<'a, S>;
100
101 fn into_iter(self) -> Events<'a, S> {
102 Events { reader: self, finished: false }
103 }
104}
105
106pub struct Events<'a, S: Iterator<Item = &'a u8>> {
111 reader: EventReader<'a, S>,
112 finished: bool,
113}
114
115impl<'a, S: Iterator<Item = &'a u8>> Events<'a, S> {
116 #[inline]
118 pub fn into_inner(self) -> EventReader<'a, S> {
119 self.reader
120 }
121
122 pub fn source(&self) -> &S { &self.reader.source }
123 pub fn source_mut(&mut self) -> &mut S { &mut self.reader.source }
124
125}
126
127impl<'a, S: Iterator<Item = &'a u8>> FusedIterator for Events<'a, S> {
128}
129
130impl<'a, S: Iterator<Item = &'a u8>> Iterator for Events<'a, S> {
131 type Item = Result<XmlEvent>;
132
133 #[inline]
134 fn next(&mut self) -> Option<Result<XmlEvent>> {
135 if self.finished && !self.reader.parser.is_ignoring_end_of_stream() {
136 None
137 } else {
138 let ev = self.reader.next();
139 if let Ok(XmlEvent::EndDocument) | Err(_) = ev {
140 self.finished = true;
141 }
142 Some(ev)
143 }
144 }
145}
146
147impl<'a> EventReader<'a, core::slice::Iter<'a, u8>> {
148 #[inline]
150 #[must_use]
151 pub fn from_str(source: &str) -> EventReader<core::slice::Iter<u8>> {
152 EventReader::new(source.as_bytes().into_iter())
153 }
154}