|
| 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