Skip to content

Commit efb2dc0

Browse files
authored
Merge pull request #635 from tevoinea/tevoinea/AsyncElementWriter
Add support for async element writer
2 parents cced485 + e2de822 commit efb2dc0

File tree

3 files changed

+292
-58
lines changed

3 files changed

+292
-58
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ path = "benches/macrobenches.rs"
4949
[features]
5050
default = []
5151

52-
## Enables support for asynchronous reading from `tokio`'s IO-Traits by enabling
52+
## Enables support for asynchronous reading and writing from `tokio`'s IO-Traits by enabling
5353
## [reading events] from types implementing [`tokio::io::AsyncBufRead`].
5454
##
5555
## [reading events]: crate::reader::Reader::read_event_into_async

Diff for: src/writer.rs

+57-55
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,59 @@ impl<W> Writer<W> {
9797
pub fn get_ref(&self) -> &W {
9898
&self.writer
9999
}
100+
101+
/// Provides a simple, high-level API for writing XML elements.
102+
///
103+
/// Returns an [`ElementWriter`] that simplifies setting attributes and writing
104+
/// content inside the element.
105+
///
106+
/// # Example
107+
///
108+
/// ```rust
109+
/// # use quick_xml::Result;
110+
/// # fn main() -> Result<()> {
111+
/// use quick_xml::events::{BytesStart, BytesText, Event};
112+
/// use quick_xml::writer::Writer;
113+
/// use quick_xml::Error;
114+
/// use std::io::Cursor;
115+
///
116+
/// let mut writer = Writer::new(Cursor::new(Vec::new()));
117+
///
118+
/// // writes <tag attr1="value1"/>
119+
/// writer.create_element("tag")
120+
/// .with_attribute(("attr1", "value1")) // chain `with_attribute()` calls to add many attributes
121+
/// .write_empty()?;
122+
///
123+
/// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
124+
/// writer.create_element("tag")
125+
/// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
126+
/// .write_text_content(BytesText::new("with some text inside"))?;
127+
///
128+
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
129+
/// writer.create_element("tag")
130+
/// .write_inner_content(|writer| {
131+
/// let fruits = ["apple", "orange"];
132+
/// for (quant, item) in fruits.iter().enumerate() {
133+
/// writer
134+
/// .create_element("fruit")
135+
/// .with_attribute(("quantity", quant.to_string().as_str()))
136+
/// .write_text_content(BytesText::new(item))?;
137+
/// }
138+
/// Ok(())
139+
/// })?;
140+
/// # Ok(())
141+
/// # }
142+
/// ```
143+
#[must_use]
144+
pub fn create_element<'a, N>(&'a mut self, name: &'a N) -> ElementWriter<W>
145+
where
146+
N: 'a + AsRef<str> + ?Sized,
147+
{
148+
ElementWriter {
149+
writer: self,
150+
start_tag: BytesStart::new(name.as_ref()),
151+
}
152+
}
100153
}
101154

102155
impl<W: Write> Writer<W> {
@@ -213,59 +266,6 @@ impl<W: Write> Writer<W> {
213266
Ok(())
214267
}
215268

216-
/// Provides a simple, high-level API for writing XML elements.
217-
///
218-
/// Returns an [`ElementWriter`] that simplifies setting attributes and writing
219-
/// content inside the element.
220-
///
221-
/// # Example
222-
///
223-
/// ```rust
224-
/// # use quick_xml::Result;
225-
/// # fn main() -> Result<()> {
226-
/// use quick_xml::events::{BytesStart, BytesText, Event};
227-
/// use quick_xml::writer::Writer;
228-
/// use quick_xml::Error;
229-
/// use std::io::Cursor;
230-
///
231-
/// let mut writer = Writer::new(Cursor::new(Vec::new()));
232-
///
233-
/// // writes <tag attr1="value1"/>
234-
/// writer.create_element("tag")
235-
/// .with_attribute(("attr1", "value1")) // chain `with_attribute()` calls to add many attributes
236-
/// .write_empty()?;
237-
///
238-
/// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
239-
/// writer.create_element("tag")
240-
/// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
241-
/// .write_text_content(BytesText::new("with some text inside"))?;
242-
///
243-
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
244-
/// writer.create_element("tag")
245-
/// .write_inner_content(|writer| {
246-
/// let fruits = ["apple", "orange"];
247-
/// for (quant, item) in fruits.iter().enumerate() {
248-
/// writer
249-
/// .create_element("fruit")
250-
/// .with_attribute(("quantity", quant.to_string().as_str()))
251-
/// .write_text_content(BytesText::new(item))?;
252-
/// }
253-
/// Ok(())
254-
/// })?;
255-
/// # Ok(())
256-
/// # }
257-
/// ```
258-
#[must_use]
259-
pub fn create_element<'a, N>(&'a mut self, name: &'a N) -> ElementWriter<W>
260-
where
261-
N: 'a + AsRef<str> + ?Sized,
262-
{
263-
ElementWriter {
264-
writer: self,
265-
start_tag: BytesStart::new(name.as_ref()),
266-
}
267-
}
268-
269269
/// Write an arbitrary serializable type
270270
///
271271
/// Note: If you are attempting to write XML in a non-UTF-8 encoding, this may not
@@ -335,12 +335,12 @@ impl<W: Write> Writer<W> {
335335

336336
/// A struct to write an element. Contains methods to add attributes and inner
337337
/// elements to the element
338-
pub struct ElementWriter<'a, W: Write> {
338+
pub struct ElementWriter<'a, W> {
339339
writer: &'a mut Writer<W>,
340340
start_tag: BytesStart<'a>,
341341
}
342342

343-
impl<'a, W: Write> ElementWriter<'a, W> {
343+
impl<'a, W> ElementWriter<'a, W> {
344344
/// Adds an attribute to this element.
345345
pub fn with_attribute<'b, I>(mut self, attr: I) -> Self
346346
where
@@ -361,7 +361,9 @@ impl<'a, W: Write> ElementWriter<'a, W> {
361361
self.start_tag.extend_attributes(attributes);
362362
self
363363
}
364+
}
364365

366+
impl<'a, W: Write> ElementWriter<'a, W> {
365367
/// Write some text inside the current element.
366368
pub fn write_text_content(self, text: BytesText) -> Result<&'a mut Writer<W>> {
367369
self.writer

0 commit comments

Comments
 (0)