Skip to main content

embedded_xml/
lib.rs

1/*!
2A no_std XML reader using embedded-io for memory constrainted environment.
3
4## Features
5- no_std
6- alloc optional
7- streaming
8
9## Usage
10```
11# use embedded_xml as xml;
12# fn main() -> Result<(), xml::Error> {
13# let xml = "<?xml version=\"1.0\"?>";
14# let mut reader = xml.as_bytes();
15let mut reader = xml::Reader::new(&mut reader, xml.len(), 64)?;
16loop {
17    match reader.next_event()? {
18        xml::Event::StartElement { name, attrs } => {
19            println!("Start element: {name} with attributes: {attrs:?}");
20        }
21        xml::Event::EndElement { name } => {
22            println!("End element: {name}");
23        }
24        xml::Event::EndOfFile => break,
25        _ => {}
26    }
27}
28# Ok(())
29# }
30```
31
32## Limitations & non-goals
33- no rewinding
34- no DTD support
35- no XPath
36- no decoding
37- individual "Events" have to fit inside the internal buffer
38*/
39
40#![no_std]
41// stable in 1.95
42#![feature(assert_matches)]
43
44mod reader;
45mod attributes;
46mod events;
47
48#[cfg(test)]
49mod tests;
50
51#[cfg(feature = "alloc")]
52extern crate alloc;
53
54pub use events::Event;
55pub use reader::Reader;
56pub use attributes::AttributeReader;
57
58#[cfg(feature = "alloc")]
59pub type OwnedReader<R> = Reader<R, alloc::vec::Vec<u8>>;
60
61#[derive(Debug)]
62pub enum Error {
63    IoError(embedded_io::ErrorKind),
64    Utf8Error(core::str::Utf8Error),
65    InvalidState,
66    Eof,
67}
68
69type Result<T> = core::result::Result<T, Error>;
70
71impl From<core::str::Utf8Error> for Error {
72    fn from(err: core::str::Utf8Error) -> Self {
73        Error::Utf8Error(err)
74    }
75}