pub trait ElementState: Sized {
type Output: Sized;
// Required methods
fn parse_element_start(tag: &str) -> Option<Self>;
fn parse_element_finish(self) -> Result<Self::Output>;
// Provided methods
fn parse_element_attribute(
&mut self,
key: &str,
value: Cow<'_, str>,
) -> Result<()> { ... }
fn parse_element_inner_text(&mut self, text: Cow<'_, str>) -> Result<()> { ... }
fn parse_element_inner_node<P: ElementParser>(
&mut self,
tag: &str,
parser: P,
) -> Result<()> { ... }
fn parse_error_not_found<T>() -> Result<T> { ... }
}Expand description
A state to parse exactly one element
The idea is that a parser will try different implementors of this to parse an element it finds;
the first implementor returning Some(..) on parse_element_start will be used to actually
parse it.
After a successful parse_element_start the parser needs to call parse_element_attribute for
all attributes on the element, then parse_element_inner_text and parse_element_inner_node
until the closing tag of the element is hit, upon which it needs to call parse_element_finish.
Required Associated Types§
Required Methods§
Sourcefn parse_element_start(tag: &str) -> Option<Self>
fn parse_element_start(tag: &str) -> Option<Self>
Try creating state to parse an element with the passed tag.
Sourcefn parse_element_finish(self) -> Result<Self::Output>
fn parse_element_finish(self) -> Result<Self::Output>
Finish parsing an element.
This is where you make sure you got all required data (unpacking their types) and can optionally check data for consistency.
Provided Methods§
Sourcefn parse_element_attribute(
&mut self,
key: &str,
value: Cow<'_, str>,
) -> Result<()>
fn parse_element_attribute( &mut self, key: &str, value: Cow<'_, str>, ) -> Result<()>
Parse attribute into state
The default implementation will fail with “unexpected attribute”.
Sourcefn parse_element_inner_text(&mut self, text: Cow<'_, str>) -> Result<()>
fn parse_element_inner_text(&mut self, text: Cow<'_, str>) -> Result<()>
Parse text or CDATA into state.
The default implementation will ignore whitespace and fail otherwise.
Sourcefn parse_element_inner_node<P: ElementParser>(
&mut self,
tag: &str,
parser: P,
) -> Result<()>
fn parse_element_inner_node<P: ElementParser>( &mut self, tag: &str, parser: P, ) -> Result<()>
Parse inner elements.
The default implementation will fail with “unexpected element”.
Sourcefn parse_error_not_found<T>() -> Result<T>
fn parse_error_not_found<T>() -> Result<T>
In case parse_element_start didn’t get to accept any element (either because it always
returned None or there just wasn’t enough data), a parser can use this to generate an
error.
ParseElementOnce uses this.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".