Struct ElementBuilder

Source
pub struct ElementBuilder { /* private fields */ }
Expand description

An easy way to build a new element by chaining methods to add properties.

Call Element::build() to start building. To finish building, either call .finish() or .push_to(parent) which returns Element.

§Examples

use edit_xml::{Document, Element, Node};

let mut doc = Document::new();

let root = Element::build("root")
    .attribute("id", "main")
    .attribute("class", "main")
    .finish(&mut doc);
doc.push_root_node(root.as_node());

let name = Element::build("name")
    .add_text("No Name")
    .push_to(&mut doc, root);

/* Equivalent xml:
  <root id="main" class="main">
    <name>No Name</name>
  </root>
*/

Implementations§

Source§

impl ElementBuilder

Source

pub fn new(full_name: impl Into<String>) -> ElementBuilder

Creates a new ElementBuilder with the full name of the element.

Source

pub fn new_with_capacities( full_name: impl Into<String>, attribute_capacity: usize, namespace_capacity: usize, content_capacity: usize, ) -> ElementBuilder

Creates a new ElementBuilder with the full name of the element and the capacities of the attributes, namespace declarations, and content.

Source

pub fn prefix(self, prefix: &str) -> Self

Removes previous prefix if it exists, and attach new prefix.

Source

pub fn attribute<S, T>(self, name: S, value: T) -> Self
where S: Into<String>, T: Into<String>,

Add an attribute to the element.

Source

pub fn namespace_decl<S, T>(self, prefix: S, namespace: T) -> Self
where S: Into<String>, T: Into<String>,

Add a namespace declaration to the element.

Source

pub fn add_text<S: Into<String>>(self, text: S) -> Self

Add text content to the element.

use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
   .add_text("Hello")
   .finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_text());
Source

pub fn add_comment<S: Into<String>>(self, text: S) -> Self

Add a comment node to the element.

use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
  .add_comment("This is a comment")
 .finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_comment());
Source

pub fn add_cdata<S: Into<String>>(self, text: S) -> Self

Add a CDATA node to the element.

use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
  .add_cdata("This is a CDATA")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_cdata());
Source

pub fn add_pi<S: Into<String>>(self, text: S) -> Self

Add a Processing Instruction node to the element.

use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
 .add_pi("target")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_pi());
Source

pub fn add_element(self, elem: ElementBuilder) -> Self

Add an element to the element.

Source

pub fn create_element<F>(self, name: impl Into<String>, f: F) -> Self

Adds an element to the element.

use edit_xml::{Document, Element};
let mut doc = Document::new();

let root = Element::build("root")
   .create_element("child", |builder| {
      builder
         .add_text("Hello")
}).finish(&mut doc);

let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert_eq!(content[0].text_content(&doc), "Hello");
Source

pub fn finish(self, doc: &mut Document) -> Element

Finish building the element and return it. The result must be pushed to a parent element or the root node.

use edit_xml::{Document, Element, ElementBuilder};
let mut doc = Document::new();
let root = ElementBuilder::new("root")
  .add_text("Hello")
  .finish(&mut doc);
doc.push_root_node(root.as_node());
Source

pub fn push_to(self, doc: &mut Document, parent: Element) -> Element

Push this element to the parent’s children.

§Panics

If the parent is not an element.

Source

pub fn push_to_root_node(self, doc: &mut Document) -> Element

Push this element to the root node.

use edit_xml::{Document, Element, ElementBuilder};
let mut doc = Document::new();
let root = ElementBuilder::new("root")
  .add_text("Hello")
  .push_to_root_node(&mut doc);
assert_eq!(doc.root_element().unwrap(), root);

Trait Implementations§

Source§

impl Clone for ElementBuilder

Source§

fn clone(&self) -> ElementBuilder

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 Debug for ElementBuilder

Source§

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

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

impl PartialEq for ElementBuilder

Source§

fn eq(&self, other: &ElementBuilder) -> 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 Eq for ElementBuilder

Source§

impl StructuralPartialEq for ElementBuilder

Auto Trait Implementations§

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