Struct ElementRef

Source
pub struct ElementRef<'s> {
    pub name: &'s str,
    pub text_content: Cow<'s, str>,
    pub attributes: IndexMap<&'s str, &'s str>,
    pub children: Vec<ElementRef<'s>>,
}
Expand description

An XML element.

This is a result of zero-copy parsing, meaning you might run into lifetime issues.

If you need to own the element separately from the source XML, you can convert it to Element using into_owned.

Fields§

§name: &'s str

The name of the element, e.g. LuaComponent in <LuaComponent />.

§text_content: Cow<'s, str>

The text content of the element, e.g. hello in <SomeComponent>hello</SomeComponent>.

If there are multiple text nodes, they are concatenated into a single string with spaces between them. This is the only case where the parsing is not zero-copy, as the text is discontinuous in the source XML.

If there is no text content, the value is Cow::Borrowed("").

§attributes: IndexMap<&'s str, &'s str>

A map of element attributes, e.g. name="comp" in <SomeComponent name="comp" />, where the key is name and the value is comp.

§children: Vec<ElementRef<'s>>

A list of child elements, e.g. [<SomeComponent/>, <SomeOtherComponent/>] in

<Entity>
    <SomeComponent/>
    <SomeOtherComponent/>
</Entity>

Implementations§

Source§

impl<'s> ElementRef<'s>

Source

pub fn new(name: &'s str) -> Self

Create a new element with the given name.

Source

pub fn to_owned(&self) -> Element

Convert this element to an owned Element by cloning all the strings.

§Example
let nonstatic_prop = String::from("value");
let element = nxml_ref!(<Entity {&nonstatic_prop} />);

let owned_element = element.to_owned();

assert_static(owned_element);
Source§

impl<'s> ElementRef<'s>

Source

pub fn attr(&self, key: &str) -> Option<&str>

A shorthand for getting an attribute value.

§Example
let element = nxml_ref!(<Entity key="value"/>);

assert_eq!(element.attr("key"), Some("value"));
Source

pub fn child(&self, name: &str) -> Option<&Self>

Find the first child element with the given name.

§Example
let element = nxml_ref!(<Entity><Child>"hello"</Child></Entity>);

assert_eq!(element.child("Child").unwrap().text_content, "hello");
Source

pub fn child_mut(&mut self, name: &str) -> Option<&mut Self>

Find the first child element with the given name, mutable version.

§Example
let mut element = nxml_ref!(<Entity><Child/></Entity>);

element.child_mut("Child").unwrap().text_content = "world".into();

assert_eq!(element.child("Child").unwrap().text_content, "world");
Source

pub fn children<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = &'a Self> + 'a

Iterate over all child elements with the given name.

§Example
let element = nxml_ref!(<Entity><Child/><Other/><Child/></Entity>);

assert_eq!(element.children("Child").count(), 2);
Source

pub fn children_mut<'a>( &'a mut self, name: &'a str, ) -> impl Iterator<Item = &'a mut Self> + 'a

Iterate over all child elements with the given name, mutable version.

§Example
let mut element = nxml_ref!(<Entity><Child/><Other/><Child/></Entity>);

for child in element.children_mut("Child") {
   child.text_content = "text".into();
}

assert_eq!(element.to_string(), "<Entity><Child>text</Child><Other/><Child>text</Child></Entity>");
Source

pub fn set_attr(&mut self, key: &'s str, value: &'s str)

A shorthand for setting an attribute value.

§Example
let mut element = nxml_ref!(<Entity />);

element.set_attr("key", "value");

assert_eq!(element.to_string(), "<Entity key=\"value\"/>");
Source

pub fn remove_attr(&mut self, key: &str) -> Option<&'s str>

A shorthand for removing an attribute value.

§Example
let mut element = nxml_ref!(<Entity key="value" other="other" />);

element.remove_attr("key");

assert_eq!(element.to_string(), "<Entity other=\"other\"/>");
Source

pub fn with_attr(self, key: &'s str, value: &'s str) -> Self

Chained version of set_attr.

§Example
let element = ElementRef::new("Entity")
    .with_attr("key", "value");

assert_eq!(element.to_string(), "<Entity key=\"value\"/>");
Source

pub fn with_text(self, text: &'s str) -> Self

Chained shorthand for setting the text content.

§Example
let element = ElementRef::new("Entity")
    .with_text("hello");

assert_eq!(element.to_string(), "<Entity>hello</Entity>");
Source

pub fn with_child(self, element: Self) -> Self

Chained shorthand for adding a child element.

§Example
let element = ElementRef::new("Entity")
     .with_child(ElementRef::new("Child"));

assert_eq!(element.to_string(), "<Entity><Child/></Entity>");
Source

pub fn display(&self) -> PrettyDisplay<'_, Self>

A customizable Display impl that pretty-prints the element.

§Example
let element = nxml_ref!(<Entity><Child/></Entity>);

assert_eq!(element.display().indent_width(0).to_string(), "<Entity>\n<Child/>\n</Entity>");

Trait Implementations§

Source§

impl<'s> Clone for ElementRef<'s>

Source§

fn clone(&self) -> ElementRef<'s>

Returns a copy 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<'s> Debug for ElementRef<'s>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'s> Display for ElementRef<'s>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'s, 'e> Div<&str> for &'e ElementRef<'s>

Source§

fn div(self, rhs: &str) -> Self::Output

A chainable child element accessor

§Example
let element = nxml_ref!(<Entity><Child><Grandchild>"hello"</Grandchild></Child></Entity>);

assert_eq!(&element / "Child" / "Grandchild" % Text, "hello");
Source§

type Output = &'e ElementRef<'s>

The resulting type after applying the / operator.
Source§

impl<'s, 'e> Div<&str> for &'e mut ElementRef<'s>

Source§

fn div(self, rhs: &str) -> Self::Output

A mutable version of the child accessor.

§Example
let mut element = nxml_ref!(<Entity><Child><Grandchild>hello</Grandchild></Child></Entity>);

(&mut element / "Child").children.clear();

assert_eq!(element.to_string(), "<Entity><Child/></Entity>");
Source§

type Output = &'e mut ElementRef<'s>

The resulting type after applying the / operator.
Source§

impl<'s> PartialEq for ElementRef<'s>

Source§

fn eq(&self, other: &ElementRef<'s>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'s, 'e> Rem<&str> for &'e ElementRef<'s>

Source§

fn rem(self, rhs: &str) -> Self::Output

A shorthand for getting an attribute value. Not index because &element / "child" % "key" is better than &(&element / "child")["key"].

§Example
let element = nxml_ref!(<Entity key="value"/>);

assert_eq!(&element % "key", "value");
Source§

type Output = &'e str

The resulting type after applying the % operator.
Source§

impl<'s, 'e> Rem<Text> for &'e ElementRef<'s>

Source§

fn rem(self, _: Text) -> Self::Output

A shorthand for getting the text content.

§Example
let element = nxml_ref!(<Entity>"hello"</Entity>);

assert_eq!(&element % Text, "hello");
Source§

type Output = &'e str

The resulting type after applying the % operator.
Source§

impl<'s> Eq for ElementRef<'s>

Source§

impl<'s> StructuralPartialEq for ElementRef<'s>

Auto Trait Implementations§

§

impl<'s> Freeze for ElementRef<'s>

§

impl<'s> RefUnwindSafe for ElementRef<'s>

§

impl<'s> Send for ElementRef<'s>

§

impl<'s> Sync for ElementRef<'s>

§

impl<'s> Unpin for ElementRef<'s>

§

impl<'s> UnwindSafe for ElementRef<'s>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.