pub struct ElementWriter<'a, W> { /* private fields */ }
Expand description

A struct to write an element. Contains methods to add attributes and inner elements to the element

Implementations§

source§

impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W>

source

pub async fn write_text_content_async( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>

Available on crate feature async-tokio only.

Write some text inside the current element.

Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);

writer
    .create_element("paired")
    .with_attribute(("attr1", "value1"))
    .with_attribute(("attr2", "value2"))
    .write_text_content_async(BytesText::new("text"))
    .await
    .expect("cannot write content");

tokio_buffer.flush().await.expect("flush failed");

assert_eq!(
    std::str::from_utf8(&buffer).unwrap(),
    r#"<paired attr1="value1" attr2="value2">text</paired>"#
);
source

pub async fn write_cdata_content_async( self, text: BytesCData<'_> ) -> Result<&'a mut Writer<W>>

Available on crate feature async-tokio only.

Write a CData event <![CDATA[...]]> inside the current element.

Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);

writer
    .create_element("paired")
    .with_attribute(("attr1", "value1"))
    .with_attribute(("attr2", "value2"))
    .write_cdata_content_async(BytesCData::new("text & content"))
    .await
    .expect("cannot write content");

tokio_buffer.flush().await.expect("flush failed");

assert_eq!(
    std::str::from_utf8(&buffer).unwrap(),
    r#"<paired attr1="value1" attr2="value2"><![CDATA[text & content]]></paired>"#
);
source

pub async fn write_pi_content_async( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>

Available on crate feature async-tokio only.

Write a processing instruction <?...?> inside the current element.

Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);

writer
    .create_element("paired")
    .with_attribute(("attr1", "value1"))
    .with_attribute(("attr2", "value2"))
    // NOTE: We cannot use BytesText::new here, because it escapes strings,
    // but processing instruction content should not be escaped
    .write_pi_content_async(BytesText::from_escaped(r#"xml-stylesheet href="style.css""#))
    .await
    .expect("cannot write content");

tokio_buffer.flush().await.expect("flush failed");

assert_eq!(
    std::str::from_utf8(&buffer).unwrap(),
    r#"<paired attr1="value1" attr2="value2">
    <?xml-stylesheet href="style.css"?>
</paired>"#
);
source

pub async fn write_empty_async(self) -> Result<&'a mut Writer<W>>

Available on crate feature async-tokio only.

Write an empty (self-closing) tag.

Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);

writer
    .create_element("empty")
    .with_attribute(("attr1", "value1"))
    .with_attribute(("attr2", "value2"))
    .write_empty_async()
    .await
    .expect("cannot write content");

tokio_buffer.flush().await.expect("flush failed");

assert_eq!(
    std::str::from_utf8(&buffer).unwrap(),
    r#"<empty attr1="value1" attr2="value2"/>"#
);
source

pub async fn write_inner_content_async<F, Fut, E>( self, closure: F ) -> StdResult<&'a mut Writer<W>, E>where F: FnOnce(&'a mut Writer<W>) -> Fut, Fut: Future<Output = StdResult<&'a mut Writer<W>, E>>, E: From<Error>,

Available on crate feature async-tokio only.

Create a new scope for writing XML inside the current element.

Example
use quick_xml::Error;

let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);

writer
    .create_element("outer")
    .with_attributes([("attr1", "value1"), ("attr2", "value2")])
    // We need to provide error type, because it is not named somewhere explicitly
    .write_inner_content_async::<_, _, Error>(|writer| async move {
        let fruits = ["apple", "orange", "banana"];
        for (quant, item) in fruits.iter().enumerate() {
            writer
                .create_element("fruit")
                .with_attributes([("quantity", quant.to_string().as_str())])
                .write_text_content_async(BytesText::new(item))
                .await?;
        }
        writer
            .create_element("inner")
            .write_inner_content_async(|writer| async move {
                writer.create_element("empty").write_empty_async().await
            })
            .await?;

        Ok(writer)
    })
    .await
    .expect("cannot write content");

tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
    std::str::from_utf8(&buffer).unwrap(),
    r#"<outer attr1="value1" attr2="value2">
    <fruit quantity="0">apple</fruit>
    <fruit quantity="1">orange</fruit>
    <fruit quantity="2">banana</fruit>
    <inner>
        <empty/>
    </inner>
</outer>"#
);
source§

impl<'a, W> ElementWriter<'a, W>

source

pub fn with_attribute<'b, I>(self, attr: I) -> Selfwhere I: Into<Attribute<'b>>,

Adds an attribute to this element.

source

pub fn with_attributes<'b, I>(self, attributes: I) -> Selfwhere I: IntoIterator, I::Item: Into<Attribute<'b>>,

Add additional attributes to this element using an iterator.

The yielded items must be convertible to Attribute using Into.

source§

impl<'a, W: Write> ElementWriter<'a, W>

source

pub fn write_text_content( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>

Write some text inside the current element.

source

pub fn write_cdata_content( self, text: BytesCData<'_> ) -> Result<&'a mut Writer<W>>

Write a CData event <![CDATA[...]]> inside the current element.

source

pub fn write_pi_content(self, text: BytesText<'_>) -> Result<&'a mut Writer<W>>

Write a processing instruction <?...?> inside the current element.

source

pub fn write_empty(self) -> Result<&'a mut Writer<W>>

Write an empty (self-closing) tag.

source

pub fn write_inner_content<F, E>( self, closure: F ) -> StdResult<&'a mut Writer<W>, E>where F: FnOnce(&mut Writer<W>) -> StdResult<(), E>, E: From<Error>,

Create a new scope for writing XML inside the current element.

Auto Trait Implementations§

§

impl<'a, W> RefUnwindSafe for ElementWriter<'a, W>where W: RefUnwindSafe,

§

impl<'a, W> Send for ElementWriter<'a, W>where W: Send,

§

impl<'a, W> Sync for ElementWriter<'a, W>where W: Sync,

§

impl<'a, W> Unpin for ElementWriter<'a, W>

§

impl<'a, W> !UnwindSafe for ElementWriter<'a, W>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.