Skip to content

Commit 0d8ac7b

Browse files
committed
Implement high-level Reader
1 parent 8459297 commit 0d8ac7b

File tree

4 files changed

+466
-0
lines changed

4 files changed

+466
-0
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ Now references to entities (as predefined, such as `<`, as user-defined) repo
2020
Caller can parse the content of the entity and stream events from it as it is required by the
2121
XML specification. See the updated `custom_entities` example!
2222

23+
The `Reader` type was significantly reworked. The old type now called `RawReader` and should be
24+
used only for very low-level manipulations with the document. Since that it is preferred to use
25+
new `Reader` which can automatically handle entity references.
26+
2327
### New Features
2428

2529
- [#766]: Allow to parse resolved entities as XML fragments and stream events from them.
2630
- [#766]: Added new event `Event::GeneralRef` with content of [general entity].
31+
- [#766]: Added `quick_xml::reader::EntityResolver` which is able to resolve external entities.
32+
- [#766]: Added `quick_xml::reader::Reader` -- a new high-level reader which should be preferred
33+
over old `RawReader`.
2734

2835
### Bug Fixes
2936

src/reader/event.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Defines zero-copy XML reader events used throughout this library.
2+
//!
3+
//! An XML event often represents part of an XML element.
4+
//! They occur during reading and are usually used with the stream-oriented API.
5+
//!
6+
//! For example, the XML element
7+
//! ```xml
8+
//! <name attr="value">Inner text</name>
9+
//! ```
10+
//! consists of the three events `Start`, `Text` and `End`. They can also represent
11+
//! other parts in an XML document like the XML declaration. Each [`Event`] usually
12+
//! contains further information, like the tag name, the attribute or the inner text.
13+
//!
14+
//! See [`Event`] for a list of all possible events.
15+
//!
16+
//! # Reading
17+
//!
18+
//! When reading an XML stream, the events are emitted by [`Reader::read_event`]
19+
//! or [`Reader::read_event_into`]. You must listen for the different types of
20+
//! events you are interested in.
21+
//!
22+
//! See [`Reader`] for further information.
23+
//!
24+
//! # Writing
25+
//!
26+
//! The [`Event`] can be directly passed to the [`Writer::write_event`] or converted
27+
//! to a [`writer::Event`] if you want to change it before write.
28+
//!
29+
//! [`Reader::read_event`]: crate::reader::Reader::read_event
30+
//! [`Reader::read_event_into`]: crate::reader::Reader::read_event_into
31+
//! [`Reader`]: crate::reader::Reader
32+
//! [`Writer::write_event`]: crate::writer::Writer::write_event
33+
//! [`writer::Event`]: crate::writer::Event
34+
35+
use crate::events::{BytesCData, BytesEnd, BytesPI, BytesStart, BytesText};
36+
37+
/// Event emitted by [`Reader::read_event`].
38+
///
39+
/// # Lifetime
40+
///
41+
/// The `'i` lifetime of this struct is the lifetime of data that may be borrowed
42+
/// from the XML input (when reader reads from `&[u8]` or `&str`) or from the
43+
/// user-provided buffer.
44+
///
45+
/// [`Reader::read_event`]: crate::reader::Reader::read_event
46+
#[derive(Clone, Debug, Eq, PartialEq)]
47+
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
48+
pub enum Event<'i> {
49+
/// Empty element tag (with attributes) `<tag attr="value" />`.
50+
Empty(BytesStart<'i>),
51+
/// Start tag (with attributes) `<tag attr="value">`.
52+
Start(BytesStart<'i>),
53+
/// End tag `</tag>`.
54+
End(BytesEnd<'i>),
55+
/// Character data between `Start` and `End` element.
56+
Text(BytesText<'i>),
57+
/// CData `<![CDATA[...]]>`.
58+
CData(BytesCData<'i>),
59+
/// Processing instruction `<?...?>`.
60+
PI(BytesPI<'i>),
61+
/// End of XML document.
62+
Eof,
63+
}
64+
65+
impl<'i> Event<'i> {
66+
/// Ensures that all data is owned to extend the object's lifetime if necessary.
67+
#[inline]
68+
pub fn into_owned(self) -> Event<'static> {
69+
match self {
70+
Self::Empty(e) => Event::Empty(e.into_owned()),
71+
Self::Start(e) => Event::Start(e.into_owned()),
72+
Self::End(e) => Event::End(e.into_owned()),
73+
Self::Text(e) => Event::Text(e.into_owned()),
74+
Self::CData(e) => Event::CData(e.into_owned()),
75+
Self::PI(e) => Event::PI(e.into_owned()),
76+
Self::Eof => Event::Eof,
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)