Skip to content

Commit e1b2c46

Browse files
authored
Merge pull request #652 from Mingun/custom-errors-in-writing
Allow to raise application errors in `ElementWriter::write_inner_content`
2 parents ad7553b + d4f0621 commit e1b2c46

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ MSRV bumped to 1.56! Crate now uses Rust 2021 edition.
1616

1717
- [#545]: Resolve well-known namespaces (`xml` and `xmlns`) to their appropriate URIs.
1818
Also, enforce namespace constraints related to these well-known namespaces.
19+
- [#635]: Add support for async `ElementWriter` operations.
1920

2021
### Bug Fixes
2122

@@ -27,8 +28,12 @@ MSRV bumped to 1.56! Crate now uses Rust 2021 edition.
2728
- [#651]: Relax requirement for version of `arbitrary` dependency -- we're actually
2829
compatible with version 1.0.0 and up.
2930
- [#649]: Make features linkable and reference them in the docs.
31+
- [#619]: Allow to raise application errors in `ElementWriter::write_inner_content`
32+
(and newly added `ElementWriter::write_inner_content_async` of course).
3033

3134
[#545]: https://github.com/tafia/quick-xml/pull/545
35+
[#619]: https://github.com/tafia/quick-xml/issues/619
36+
[#635]: https://github.com/tafia/quick-xml/pull/635
3237
[#643]: https://github.com/tafia/quick-xml/pull/643
3338
[#649]: https://github.com/tafia/quick-xml/pull/646
3439
[#651]: https://github.com/tafia/quick-xml/pull/651

src/writer.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Contains high-level interface for an events-based XML emitter.
22
33
use std::io::Write;
4+
use std::result::Result as StdResult;
45

56
use crate::encoding::UTF8_BOM;
6-
use crate::errors::Result;
7+
use crate::errors::{Error, Result};
78
use crate::events::{attributes::Attribute, BytesCData, BytesStart, BytesText, Event};
89

910
#[cfg(feature = "async-tokio")]
@@ -105,7 +106,7 @@ impl<W> Writer<W> {
105106
///
106107
/// # Example
107108
///
108-
/// ```rust
109+
/// ```
109110
/// # use quick_xml::Result;
110111
/// # fn main() -> Result<()> {
111112
/// use quick_xml::events::{BytesStart, BytesText, Event};
@@ -127,7 +128,8 @@ impl<W> Writer<W> {
127128
///
128129
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
129130
/// writer.create_element("tag")
130-
/// .write_inner_content(|writer| {
131+
/// // We need to provide error type, because it is not named somewhere explicitly
132+
/// .write_inner_content::<_, Error>(|writer| {
131133
/// let fruits = ["apple", "orange"];
132134
/// for (quant, item) in fruits.iter().enumerate() {
133135
/// writer
@@ -401,9 +403,10 @@ impl<'a, W: Write> ElementWriter<'a, W> {
401403
}
402404

403405
/// Create a new scope for writing XML inside the current element.
404-
pub fn write_inner_content<F>(self, closure: F) -> Result<&'a mut Writer<W>>
406+
pub fn write_inner_content<F, E>(self, closure: F) -> StdResult<&'a mut Writer<W>, E>
405407
where
406-
F: FnOnce(&mut Writer<W>) -> Result<()>,
408+
F: FnOnce(&mut Writer<W>) -> StdResult<(), E>,
409+
E: From<Error>,
407410
{
408411
self.writer
409412
.write_event(Event::Start(self.start_tag.borrow()))?;
@@ -748,7 +751,7 @@ mod indentation {
748751
.create_element("outer")
749752
.with_attribute(("attr1", "value1"))
750753
.with_attribute(("attr2", "value2"))
751-
.write_inner_content(|writer| {
754+
.write_inner_content::<_, Error>(|writer| {
752755
let fruits = ["apple", "orange", "banana"];
753756
for (quant, item) in fruits.iter().enumerate() {
754757
writer
@@ -759,8 +762,7 @@ mod indentation {
759762
writer
760763
.create_element("inner")
761764
.write_inner_content(|writer| {
762-
writer.create_element("empty").write_empty()?;
763-
Ok(())
765+
writer.create_element("empty").write_empty().map(|_| ())
764766
})?;
765767

766768
Ok(())

src/writer/async_tokio.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::future::Future;
2+
use std::result::Result as StdResult;
23

34
use tokio::io::{AsyncWrite, AsyncWriteExt};
45

5-
use crate::errors::Result;
6+
use crate::errors::{Error, Result};
67
use crate::events::{BytesCData, BytesText, Event};
78
use crate::{ElementWriter, Writer};
89

@@ -252,6 +253,8 @@ impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W> {
252253
/// # use quick_xml::writer::Writer;
253254
/// # use quick_xml::events::BytesText;
254255
/// # use tokio::io::AsyncWriteExt;
256+
/// use quick_xml::Error;
257+
///
255258
/// # #[tokio::main(flavor = "current_thread")] async fn main() {
256259
/// let mut buffer = Vec::new();
257260
/// let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
@@ -260,7 +263,8 @@ impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W> {
260263
/// writer
261264
/// .create_element("outer")
262265
/// .with_attributes([("attr1", "value1"), ("attr2", "value2")])
263-
/// .write_inner_content_async(|writer| async move {
266+
/// // We need to provide error type, because it is not named somewhere explicitly
267+
/// .write_inner_content_async::<_, _, Error>(|writer| async move {
264268
/// let fruits = ["apple", "orange", "banana"];
265269
/// for (quant, item) in fruits.iter().enumerate() {
266270
/// writer
@@ -272,12 +276,11 @@ impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W> {
272276
/// writer
273277
/// .create_element("inner")
274278
/// .write_inner_content_async(|writer| async move {
275-
/// writer.create_element("empty").write_empty_async().await?;
276-
/// Ok(writer)
279+
/// writer.create_element("empty").write_empty_async().await
277280
/// })
278281
/// .await?;
279282
///
280-
/// Ok(writer)
283+
/// Ok(writer)
281284
/// })
282285
/// .await
283286
/// .expect("cannot write content");
@@ -295,13 +298,14 @@ impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W> {
295298
/// </outer>"#
296299
/// );
297300
/// # }
298-
pub async fn write_inner_content_async<F, Fut>(
301+
pub async fn write_inner_content_async<F, Fut, E>(
299302
mut self,
300303
closure: F,
301-
) -> Result<&'a mut Writer<W>>
304+
) -> StdResult<&'a mut Writer<W>, E>
302305
where
303306
F: FnOnce(&'a mut Writer<W>) -> Fut,
304-
Fut: Future<Output = Result<&'a mut Writer<W>>>,
307+
Fut: Future<Output = StdResult<&'a mut Writer<W>, E>>,
308+
E: From<Error>,
305309
{
306310
self.writer
307311
.write_event_async(Event::Start(self.start_tag.borrow()))

0 commit comments

Comments
 (0)