Struct jupiter::ig::xml::Element

source ·
pub struct Element<'a, B: BufRead> { /* private fields */ }
Expand description

Represents a matched element while processing XML data.

Using this type we can enforce proper nesting when handling a stream of XML events with the help of the rust type system.

Implementations§

source§

impl<B: BufRead> Element<'_, B>

source

pub fn name(&self) -> Cow<'_, str>

Returns the name of the element.

§Example
let mut reader = PullReader::new("<xml></xml>".as_bytes());
assert_eq!(reader.root().unwrap().name().as_ref(), "xml");
source

pub fn attributes(&self) -> AttributesView<'_>

Provides access to the attributes of the element.

§Example
let mut reader = PullReader::new(r#"<xml test="foo"></xml>"#.as_bytes());
let root = reader.root().unwrap();
let attr = root.attributes().last().unwrap().unwrap();
assert_eq!(attr.key(), "test");
assert_eq!(attr.value().unwrap().contents().as_ref(), "foo");
source

pub fn find_attribute( &self, name: impl AsRef<str> ) -> Result<Option<AttributeView<'_>>>

Returns the requested attribute.

§Example
let mut reader = PullReader::new(r#"<xml test="foo"></xml>"#.as_bytes());
let root = reader.root().unwrap();
assert_eq!(root.find_attribute("test").unwrap().unwrap().value().unwrap().contents().as_ref(), "foo");
source

pub fn next_child<'a>(&'a mut self) -> Result<Option<Element<'a, B>>>

Returns the next child element of this element.

If an empty value is returned, this element has no more children.

§Example
let mut reader = PullReader::new(r#"<xml><item></item></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
assert_eq!(root.next_child().unwrap().unwrap().name().as_ref(), "item");
assert_eq!(root.next_child().unwrap().is_none(), true);
source

pub fn find_next_child<'a>( &'a mut self, name: impl AsRef<str> ) -> Result<Option<Element<'a, B>>>

Returns the next child element of this element with the given tag name.

This will skip over all other tags until a match is found. If the end of the enclosing tag is reached, none is returned.

§Example
let mut reader = PullReader::new(r#"<xml><item></item><test></test></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
assert_eq!(root.find_next_child("test").unwrap().unwrap().name().as_ref(), "test");
assert_eq!(root.find_next_child("test").unwrap().is_none(), true);
source

pub fn try_text<'a>(&'a mut self) -> Result<Option<Text<'a>>>

Tries to fetch the text contents of this element.

Returns none if this element has no text at all.

§Example
let mut reader = PullReader::new(r#"<xml><item>test</item><empty></empty></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
let mut item = root.next_child().unwrap().unwrap();
assert_eq!(item.try_text().unwrap().unwrap().contents().as_ref(), "test");
drop(item);

let mut test = root.next_child().unwrap().unwrap();
assert_eq!(test.try_text().unwrap().is_none(), true);
source

pub fn text(&mut self) -> Result<Text<'_>>

Fetches the text contents of this element or returns an empty string if no text is present.

§Example
let mut reader = PullReader::new(r#"<xml><item>test</item><empty></empty></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
let mut item = root.next_child().unwrap().unwrap();
assert_eq!(item.text().unwrap().contents().as_ref(), "test");
drop(item);

let mut test = root.next_child().unwrap().unwrap();
assert_eq!(test.text().unwrap().contents().as_ref(), "");

Trait Implementations§

source§

impl<B: BufRead> Drop for Element<'_, B>

source§

fn drop(&mut self)

Once an element is dropped, we consume all events up until the end tag of the element in the XML data.

This permits to utilize the Rust type system to ensure proper nesting when parsing XML data.

Auto Trait Implementations§

§

impl<'a, B> Freeze for Element<'a, B>

§

impl<'a, B> !RefUnwindSafe for Element<'a, B>

§

impl<'a, B> !Send for Element<'a, B>

§

impl<'a, B> !Sync for Element<'a, B>

§

impl<'a, B> Unpin for Element<'a, B>

§

impl<'a, B> !UnwindSafe for Element<'a, B>

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more