| 1 | //! Contains high-level interface for a pull-based XML parser. |
| 2 | //! |
| 3 | //! The most important type in this module is `EventReader`, which provides an iterator |
| 4 | //! view for events in XML document. |
| 5 | |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#; |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/; |
| 9 | |
| 10 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::commonrust-analyzer cargo xml-rs 0.8.19 common/::{Positionrust-analyzer cargo xml-rs 0.8.19 common/Position#, TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#}; |
| 11 | |
| 12 | pub use selfrust-analyzer cargo xml-rs 0.8.19 reader/::configrust-analyzer cargo xml-rs 0.8.19 reader/config/::ParserConfigrust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig#; |
| 13 | pub use selfrust-analyzer cargo xml-rs 0.8.19 reader/::configrust-analyzer cargo xml-rs 0.8.19 reader/config/::ParserConfig2rust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig2#; |
| 14 | pub use selfrust-analyzer cargo xml-rs 0.8.19 reader/::errorrust-analyzer cargo xml-rs 0.8.19 reader/error/::{Errorrust-analyzer cargo xml-rs 0.8.19 reader/error/Error#, ErrorKindrust-analyzer cargo xml-rs 0.8.19 reader/error/ErrorKind#}; |
| 15 | pub use selfrust-analyzer cargo xml-rs 0.8.19 reader/::eventsrust-analyzer cargo xml-rs 0.8.19 reader/events/::XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#; |
| 16 | |
| 17 | use selfrust-analyzer cargo xml-rs 0.8.19 reader/::parserrust-analyzer cargo xml-rs 0.8.19 reader/parser/::PullParserrust-analyzer cargo xml-rs 0.8.19 reader/parser/PullParser#; |
| 18 | |
| 19 | mod configrust-analyzer cargo xml-rs 0.8.19 reader/config/; |
| 20 | mod eventsrust-analyzer cargo xml-rs 0.8.19 reader/events/; |
| 21 | mod lexerrust-analyzer cargo xml-rs 0.8.19 reader/lexer/; |
| 22 | mod parserrust-analyzer cargo xml-rs 0.8.19 reader/parser/; |
| 23 | mod indexsetrust-analyzer cargo xml-rs 0.8.19 reader/indexset/; |
| 24 | mod errorrust-analyzer cargo xml-rs 0.8.19 reader/error/; |
| 25 | |
| 26 | |
| 27 | /// A result type yielded by `XmlReader`. |
| 28 | pub type Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<T, E = Errorrust-analyzer cargo xml-rs 0.8.19 reader/error/Error#> = resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, E>; |
| 29 | |
| 30 | /// A wrapper around an `std::io::Read` instance which provides pull-based XML parsing. |
| 31 | pub struct EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> { |
| 32 | sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source.: R, |
| 33 | parserrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#parser.: PullParserrust-analyzer cargo xml-rs 0.8.19 reader/parser/PullParser#, |
| 34 | } |
| 35 | |
| 36 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R> { |
| 37 | /// Creates a new reader, consuming the given stream. |
| 38 | #[inline] |
| 39 | pub fn newrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]new().(source: R) -> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R> { |
| 40 | EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#::new_with_configrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]new_with_config().(source, ParserConfig2rust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig2#::newrust-analyzer cargo xml-rs 0.8.19 reader/config/impl#[ParserConfig2]new().()) |
| 41 | } |
| 42 | |
| 43 | /// Creates a new reader with the provded configuration, consuming the given stream. |
| 44 | #[inline] |
| 45 | pub fn new_with_configrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]new_with_config().(source: R, config: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<ParserConfig2rust-analyzer cargo xml-rs 0.8.19 reader/config/ParserConfig2#>) -> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R> { |
| 46 | EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader# { sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source., parserrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#parser.: PullParserrust-analyzer cargo xml-rs 0.8.19 reader/parser/PullParser#::newrust-analyzer cargo xml-rs 0.8.19 reader/parser/impl#[PullParser]new().(config) } |
| 47 | } |
| 48 | |
| 49 | /// Pulls and returns next XML event from the stream. |
| 50 | /// |
| 51 | /// If returned event is `XmlEvent::Error` or `XmlEvent::EndDocument`, then |
| 52 | /// further calls to this method will return this event again. |
| 53 | #[inline] |
| 54 | pub fn nextrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]next().(&mut self) -> Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#> { |
| 55 | self.parserrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#parser..nextrust-analyzer cargo xml-rs 0.8.19 reader/parser/impl#[PullParser]next().(&mut self.sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source.) |
| 56 | } |
| 57 | |
| 58 | /// Skips all XML events until the next end tag at the current level. |
| 59 | /// |
| 60 | /// Convenience function that is useful for the case where you have |
| 61 | /// encountered a start tag that is of no interest and want to |
| 62 | /// skip the entire XML subtree until the corresponding end tag. |
| 63 | #[inline] |
| 64 | pub fn skiprust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]skip().(&mut self) -> Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<()> { |
| 65 | let mut depth = 1; |
| 66 | |
| 67 | while depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]gt().0 { |
| 68 | match self.nextrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 69 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::StartElementrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#StartElement# { .. } => depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1, |
| 70 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::EndElementrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#EndElement# { .. } => depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().1, |
| 71 | XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::EndDocumentrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#EndDocument# => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(), |
| 72 | _ => {} |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 77 | } |
| 78 | |
| 79 | pub fn sourcerust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]source().(&self) -> &R { &self.sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source. } |
| 80 | pub fn source_mutrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]source_mut().(&mut self) -> &mut R { &mut self.sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source. } |
| 81 | |
| 82 | /// Unwraps this `EventReader`, returning the underlying reader. |
| 83 | /// |
| 84 | /// Note that this operation is destructive; unwrapping the reader and wrapping it |
| 85 | /// again with `EventReader::new()` will create a fresh reader which will attempt |
| 86 | /// to parse an XML document from the beginning. |
| 87 | pub fn into_innerrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]into_inner().(self) -> R { |
| 88 | self.sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source. |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl<B: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Positionrust-analyzer cargo xml-rs 0.8.19 common/Position# for EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<B> { |
| 93 | /// Returns the position of the last event produced by the reader. |
| 94 | #[inline] |
| 95 | fn positionrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<B>`][Position]position().(&self) -> TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# { |
| 96 | self.parserrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#parser..positionrust-analyzer cargo xml-rs 0.8.19 reader/parser/impl#[PullParser][Position]position().() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator# for EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R> { |
| 101 | type Itemrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`][IntoIterator]Item# = Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#>; |
| 102 | type IntoIterrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`][IntoIterator]IntoIter# = Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R>; |
| 103 | |
| 104 | fn into_iterrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`][IntoIterator]into_iter().(self) -> Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R> { |
| 105 | Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events# { readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader.: self, finishedrust-analyzer cargo xml-rs 0.8.19 reader/Events#finished.: false } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// An iterator over XML events created from some type implementing `Read`. |
| 110 | /// |
| 111 | /// When the next event is `xml::event::Error` or `xml::event::EndDocument`, then |
| 112 | /// it will be returned by the iterator once, and then it will stop producing events. |
| 113 | pub struct Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> { |
| 114 | readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader.: EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R>, |
| 115 | finishedrust-analyzer cargo xml-rs 0.8.19 reader/Events#finished.: bool, |
| 116 | } |
| 117 | |
| 118 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R> { |
| 119 | /// Unwraps the iterator, returning the internal `EventReader`. |
| 120 | #[inline] |
| 121 | pub fn into_innerrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`Events<R>`]into_inner().(self) -> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<R> { |
| 122 | self.readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader. |
| 123 | } |
| 124 | |
| 125 | pub fn sourcerust-analyzer cargo xml-rs 0.8.19 reader/impl#[`Events<R>`]source().(&self) -> &R { &self.readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader..sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source. } |
| 126 | pub fn source_mutrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`Events<R>`]source_mut().(&mut self) -> &mut R { &mut self.readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader..sourcerust-analyzer cargo xml-rs 0.8.19 reader/EventReader#source. } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R> { |
| 131 | } |
| 132 | |
| 133 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Eventsrust-analyzer cargo xml-rs 0.8.19 reader/Events#<R> { |
| 134 | type Itemrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`Events<R>`][Iterator]Item# = Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#>; |
| 135 | |
| 136 | #[inline] |
| 137 | fn nextrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`Events<R>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Resultrust-analyzer cargo xml-rs 0.8.19 reader/Result#<XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#>> { |
| 138 | if self.finishedrust-analyzer cargo xml-rs 0.8.19 reader/Events#finished. && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader..parserrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#parser..is_ignoring_end_of_streamrust-analyzer cargo xml-rs 0.8.19 reader/parser/impl#[PullParser]is_ignoring_end_of_stream().() { |
| 139 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 140 | } else { |
| 141 | let ev = self.readerrust-analyzer cargo xml-rs 0.8.19 reader/Events#reader..nextrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]next().(); |
| 142 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(XmlEventrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#::EndDocumentrust-analyzer cargo xml-rs 0.8.19 reader/events/XmlEvent#EndDocument#) | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) = ev { |
| 143 | self.finishedrust-analyzer cargo xml-rs 0.8.19 reader/Events#finished. = true; |
| 144 | } |
| 145 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ev) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl<'r> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<&'r [u8]> { |
| 151 | /// A convenience method to create an `XmlReader` from a string slice. |
| 152 | #[inline] |
| 153 | #[must_use] |
| 154 | pub fn from_strrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<&'r [u8]>`]from_str().(source: &'r str) -> EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#<&'r [u8]> { |
| 155 | EventReaderrust-analyzer cargo xml-rs 0.8.19 reader/EventReader#::newrust-analyzer cargo xml-rs 0.8.19 reader/impl#[`EventReader<R>`]new().(source.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) |
| 156 | } |
| 157 | } |