pub struct ElementWriter<'a, W> {
writer: &'a mut Writer<W>,
start_tag: BytesStart<'a>,
state: AttributeIndent,
spaces: Vec<u8>,
}
Expand description
A struct to write an element. Contains methods to add attributes and inner elements to the element
Fields§
§writer: &'a mut Writer<W>
§start_tag: BytesStart<'a>
§state: AttributeIndent
§spaces: Vec<u8>
Contains spaces used to write space indents of attributes
Implementations§
source§impl<'a, W> ElementWriter<'a, W>
impl<'a, W> ElementWriter<'a, W>
sourcepub fn with_attribute<'b, I>(self, attr: I) -> Self
pub fn with_attribute<'b, I>(self, attr: I) -> Self
Adds an attribute to this element.
sourcepub fn with_attributes<'b, I>(self, attributes: I) -> Self
pub fn with_attributes<'b, I>(self, attributes: I) -> Self
Add additional attributes to this element using an iterator.
The yielded items must be convertible to Attribute
using Into
.
sourcepub fn new_line(self) -> Self
pub fn new_line(self) -> Self
Push a new line inside an element between attributes. Note, that this
method does nothing if Writer
was created without indentation support.
§Examples
The following code
let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 2);
writer
.create_element("element")
//.new_line() (1)
.with_attribute(("first", "1"))
.with_attribute(("second", "2"))
.new_line()
.with_attributes([
("third", "3"),
("fourth", "4"),
])
//.new_line() (2)
.write_empty();
will produce the following XMLs:
<!-- result of the code above. Spaces always is used -->
<element first="1" second="2"
third="3" fourth="4"/>
<!-- if uncomment only (1) - indent depends on indentation
settings - 2 spaces here -->
<element
first="1" second="2"
third="3" fourth="4"/>
<!-- if uncomment only (2). Spaces always is used -->
<element first="1" second="2"
third="3" fourth="4"
/>
sourcefn write_attr<'b>(&mut self, attr: Attribute<'b>)
fn write_attr<'b>(&mut self, attr: Attribute<'b>)
Writes attribute and maintain indentation state
source§impl<'a, W: Write> ElementWriter<'a, W>
impl<'a, W: Write> ElementWriter<'a, W>
sourcepub fn write_text_content(
self,
text: BytesText<'_>,
) -> Result<&'a mut Writer<W>>
pub fn write_text_content( self, text: BytesText<'_>, ) -> Result<&'a mut Writer<W>>
Write some text inside the current element.
sourcepub fn write_cdata_content(
self,
text: BytesCData<'_>,
) -> Result<&'a mut Writer<W>>
pub fn write_cdata_content( self, text: BytesCData<'_>, ) -> Result<&'a mut Writer<W>>
Write a CData event <![CDATA[...]]>
inside the current element.
sourcepub fn write_pi_content(self, pi: BytesPI<'_>) -> Result<&'a mut Writer<W>>
pub fn write_pi_content(self, pi: BytesPI<'_>) -> Result<&'a mut Writer<W>>
Write a processing instruction <?...?>
inside the current element.
sourcepub fn write_empty(self) -> Result<&'a mut Writer<W>>
pub fn write_empty(self) -> Result<&'a mut Writer<W>>
Write an empty (self-closing) tag.