pub struct Element {
pub name: CompactString,
pub text_content: CompactString,
pub attributes: IndexMap<CompactString, CompactString>,
pub children: Vec<Element>,
}
Expand description
An owned XML element. Slightly easier to work with than ElementRef
.
Fields§
§name: CompactString
The name of the element, e.g. LuaComponent
in <LuaComponent />
.
text_content: CompactString
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.
attributes: IndexMap<CompactString, CompactString>
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<Element>
A list of child elements, e.g. [<SomeComponent/>
,
<SomeOtherComponent/>
] in
<Entity>
<SomeComponent/>
<SomeOtherComponent/>
</Entity>
Implementations§
Source§impl Element
impl Element
Sourcepub fn new(name: impl ToCompactString) -> Element
pub fn new(name: impl ToCompactString) -> Element
Create a new element with the given name.
Sourcepub fn as_ref(&self) -> ElementRef<'_>
pub fn as_ref(&self) -> ElementRef<'_>
Create an ElementRef
view of this element.
§Example
let element = nxml!(<root><thing/><thing/></root>);
let element_ref: ElementRef = element.as_ref();
assert_eq!(element_ref.to_string(), "<root><thing/><thing/></root>");
Source§impl Element
impl Element
Sourcepub fn attr(&self, key: &str) -> Option<&str>
pub fn attr(&self, key: &str) -> Option<&str>
A shorthand for getting an attribute value.
§Example
let element = nxml!(<Entity key="value"/>);
assert_eq!(element.attr("key"), Some("value"));
Sourcepub fn child(&self, name: &str) -> Option<&Self>
pub fn child(&self, name: &str) -> Option<&Self>
Find the first child element with the given name.
§Example
let element = nxml!(<Entity><Child>"hello"</Child></Entity>);
assert_eq!(element.child("Child").unwrap().text_content, "hello");
Sourcepub fn child_mut(&mut self, name: &str) -> Option<&mut Self>
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!(<Entity><Child/></Entity>);
element.child_mut("Child").unwrap().text_content = "world".into();
assert_eq!(element.child("Child").unwrap().text_content, "world");
Sourcepub fn children<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = &'a Self> + 'a
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!(<Entity><Child/><Other/><Child/></Entity>);
assert_eq!(element.children("Child").count(), 2);
Sourcepub fn children_mut<'a>(
&'a mut self,
name: &'a str,
) -> impl Iterator<Item = &'a mut Self> + 'a
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!(<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>");
Sourcepub fn set_attr(
&mut self,
key: impl ToCompactString,
value: impl ToCompactString,
)
pub fn set_attr( &mut self, key: impl ToCompactString, value: impl ToCompactString, )
A shorthand for setting an attribute value.
§Example
let mut element = nxml!(<Entity />);
element.set_attr("key", "value");
assert_eq!(element.to_string(), "<Entity key=\"value\"/>");
Sourcepub fn remove_attr(&mut self, key: &str) -> Option<CompactString>
pub fn remove_attr(&mut self, key: &str) -> Option<CompactString>
A shorthand for removing an attribute value.
§Example
let mut element = nxml!(<Entity key="value" other="other" />);
element.remove_attr("key");
assert_eq!(element.to_string(), "<Entity other=\"other\"/>");
Sourcepub fn with_attr(
self,
key: impl ToCompactString,
value: impl ToCompactString,
) -> Self
pub fn with_attr( self, key: impl ToCompactString, value: impl ToCompactString, ) -> Self
Sourcepub fn with_text(self, text: impl ToCompactString) -> Self
pub fn with_text(self, text: impl ToCompactString) -> Self
Chained shorthand for setting the text content.
§Example
let element = Element::new("Entity")
.with_text("hello");
assert_eq!(element.to_string(), "<Entity>hello</Entity>");
Sourcepub fn with_child(self, element: Self) -> Self
pub fn with_child(self, element: Self) -> Self
Chained shorthand for adding a child element.
§Example
let element = Element::new("Entity")
.with_child(Element::new("Child"));
assert_eq!(element.to_string(), "<Entity><Child/></Entity>");
Sourcepub fn display(&self) -> PrettyDisplay<'_, Self>
pub fn display(&self) -> PrettyDisplay<'_, Self>
Trait Implementations§
Source§impl<'e> Div<&str> for &'e Element
impl<'e> Div<&str> for &'e Element
Source§impl<'e> Div<&str> for &'e mut Element
impl<'e> Div<&str> for &'e mut Element
Source§impl<'e> Rem<&str> for &'e Element
impl<'e> Rem<&str> for &'e Element
impl Eq for Element
impl StructuralPartialEq for Element
Auto Trait Implementations§
impl Freeze for Element
impl RefUnwindSafe for Element
impl Send for Element
impl Sync for Element
impl Unpin for Element
impl UnwindSafe for Element
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
. Read more