Reader

Struct Reader 

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

controls XML lexing and parsing

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub fn append_path(&mut self, append_path: AppendPath<'_>)

used to control the path parameter in error handling

you should use Reader::append_path and Reader::exit_path to control “where” you are in the xml tree so if you encounter an error you can give a helpful message to the user

Source

pub fn exit_path(&mut self)

remove the last part of the path until “:” or “/” for example

if the path was “/document/element:my_attr” and Reader::exit_path is called then the result is “/document/element”

reader.append_path(AppendPath::Element("document"));
reader.append_path(AppendPath::Element("element"));
reader.append_path(AppendPath::NamedAttr("my_attr"));
assert_eq!(reader.path(), "/document/element:my_attr");

reader.exit_path();
assert_eq!(reader.path(), "/document/element");
Source§

impl<'a> Reader<'a>

Source

pub fn attr(&mut self) -> Result<(&'a str, &'a str), DeXmlError>

deserialize a key value attribute and error if there isn’t one or is malformed

if you want to parse the attr value into a type look at Reader::dexml

Source

pub fn attr_opt(&mut self) -> Result<Option<(&'a str, &'a str)>, DeXmlError>

deserialize a key value attribute and if there isn’t an attribute return None

if you want to parse the attr value into a type look at Reader::dexml

Source§

impl<'a> Reader<'a>

Source

pub const fn new(xml: &'a str) -> Self

construct a new lexer from the xml input string and start an empty path

Source

pub const fn path(&self) -> &str

get the path for debugging where you are in the xml tree

Source

pub fn open_tag(&mut self, tag: &str) -> Result<(), DeXmlError>

attempt to parse “<[TAG]” where TAG is your input, for example <soap:Envelope can be parsed via Reader::open_tag(..., "Envelope") from the xml input string

Source

pub fn close_tag(&mut self) -> Result<(), DeXmlError>

attempt to close a tag via “</TAG>” doesn’t check if the tag is specific name

Source

pub fn text(&mut self) -> Result<&'a str, DeXmlError>

attempt to grab a text node from the xml

Source

pub fn err(&mut self, message: Cow<'static, str>) -> DeXmlError

return an error with the current path and xml input src. The message can be owned or static borrow

Source

pub fn visit<T: DeXml<'a>>(&mut self) -> Result<T, DeXmlError>

visit a type that impls DeXml if you have a custom type this is where it is parsed and deserialized

if you are needing to parse a sub document see Reader::dexml

Source

pub fn dexml<T: DeXml<'a>>(&mut self, arg: &'a str) -> Result<T, DeXmlError>

similar to Reader::visit but used for attribute parsing for example

when you call Reader::attr or Reader::attr_opt you will end up with the attribute value as a str slice and you may want to parse it into a custom type

of course if this parsing fails, you want some error logic to show the current path of the parser and where it is the xml tree for debugging. this method does exactly that, provides a new lexer and reader and parses the str as if it were a different document but moves over it’s path, after parsing is complete takes back the path into the crrent reader

Source§

impl<'a> Reader<'a>

Source

pub fn peek_tag(&mut self) -> Option<&'a str>

inspect the next tag, can be used for parsing std::vec::Vec

Source

pub fn peek_closing_tag(&mut self) -> Option<&'a str>

similar to Reader::peek_tag but inspects the next closing tag i.e “</tag”

Source

pub fn peek_open_or_closing_tag(&mut self) -> Option<PeekedTag<'a>>

similar to Reader::peek_tag but accepts either an opening tag or closing tag

Source§

impl<'a> Reader<'a>

Source

pub fn tag_end(&mut self) -> Result<TagEnd, DeXmlError>

parse the ending of a tag, if there are attributes it will consume them and then the end of the element

Source

pub fn assert_children(&mut self) -> Result<(), DeXmlError>

assert this element IS NOT a TagEnd::SelfClosing

Source

pub fn tag_close_infer(&mut self, tag_end: TagEnd) -> Result<(), DeXmlError>

if the element has a TagEnd::SelfClosing does nothing if the element has a TagEnd::Normal attempts to parse the closing “</Element>”

some elements end in self closing tags such as: “<Element />” while others look like: “<Element></Element>” Reader::tag_close_infer can be used in combination with the output of Reader::tag_end to parse these correctly

e.x:

let tag_end = reader.tag_end()?;
// if the element was self closing it does nothing, else parse the closing tag
reader.tag_close_infer(tag_end)?;
Source§

impl<'a> Reader<'a>

Source

pub fn till_open_tag(&mut self, tag: &str) -> Result<(), DeXmlError>

parse elements till you find the tag, once you do parse it

Source

pub fn till_close_tag(&mut self, tag: &str) -> Result<(), DeXmlError>

parse elements till you find the ending tag, once you do parse it

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Reader<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.