pub struct Reader<'a> { /* private fields */ }Expand description
controls XML lexing and parsing
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn append_path(&mut self, append_path: AppendPath<'_>)
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
Sourcepub fn exit_path(&mut self)
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>
impl<'a> Reader<'a>
Sourcepub fn attr(&mut self) -> Result<(&'a str, &'a str), DeXmlError>
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
Sourcepub fn attr_opt(&mut self) -> Result<Option<(&'a str, &'a str)>, DeXmlError>
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>
impl<'a> Reader<'a>
Sourcepub const fn new(xml: &'a str) -> Self
pub const fn new(xml: &'a str) -> Self
construct a new lexer from the xml input string and start an empty path
Sourcepub fn open_tag(&mut self, tag: &str) -> Result<(), DeXmlError>
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
Sourcepub fn close_tag(&mut self) -> Result<(), DeXmlError>
pub fn close_tag(&mut self) -> Result<(), DeXmlError>
attempt to close a tag via “</TAG>” doesn’t check if the tag is specific name
Sourcepub fn text(&mut self) -> Result<&'a str, DeXmlError>
pub fn text(&mut self) -> Result<&'a str, DeXmlError>
attempt to grab a text node from the xml
Sourcepub fn err(&mut self, message: Cow<'static, str>) -> DeXmlError
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
Sourcepub fn visit<T: DeXml<'a>>(&mut self) -> Result<T, DeXmlError>
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
Sourcepub fn dexml<T: DeXml<'a>>(&mut self, arg: &'a str) -> Result<T, DeXmlError>
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>
impl<'a> Reader<'a>
Sourcepub fn peek_tag(&mut self) -> Option<&'a str>
pub fn peek_tag(&mut self) -> Option<&'a str>
inspect the next tag, can be used for parsing std::vec::Vec
Sourcepub fn peek_closing_tag(&mut self) -> Option<&'a str>
pub fn peek_closing_tag(&mut self) -> Option<&'a str>
similar to Reader::peek_tag but inspects the next closing tag i.e “</tag”
Sourcepub fn peek_open_or_closing_tag(&mut self) -> Option<PeekedTag<'a>>
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>
impl<'a> Reader<'a>
Sourcepub fn tag_end(&mut self) -> Result<TagEnd, DeXmlError>
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
Sourcepub fn assert_children(&mut self) -> Result<(), DeXmlError>
pub fn assert_children(&mut self) -> Result<(), DeXmlError>
assert this element IS NOT a TagEnd::SelfClosing
Sourcepub fn tag_close_infer(&mut self, tag_end: TagEnd) -> Result<(), DeXmlError>
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>
impl<'a> Reader<'a>
Sourcepub fn till_open_tag(&mut self, tag: &str) -> Result<(), DeXmlError>
pub fn till_open_tag(&mut self, tag: &str) -> Result<(), DeXmlError>
parse elements till you find the tag, once you do parse it
Sourcepub fn till_close_tag(&mut self, tag: &str) -> Result<(), DeXmlError>
pub fn till_close_tag(&mut self, tag: &str) -> Result<(), DeXmlError>
parse elements till you find the ending tag, once you do parse it
Sourcepub fn till_close_infer_tag(
&mut self,
tag: &str,
tag_end: TagEnd,
) -> Result<(), DeXmlError>
pub fn till_close_infer_tag( &mut self, tag: &str, tag_end: TagEnd, ) -> Result<(), DeXmlError>
if TagEnd::SelfClosing then this does nothing else parse elements till you find the ending tag, once you do parse it