Skip to content

Commit

Permalink
Add to_utf8_io_writer accepting std::io::Write
Browse files Browse the repository at this point in the history
  • Loading branch information
pronebird authored and Mingun committed Dec 29, 2024
1 parent 0793d6a commit 3b5c74d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

### New Features

- [#836]: Add `se::to_utf8_io_writer()` helper compatible with `std::io::Write` and restricted to UTF-8 encoding.

### Bug Fixes

### Misc Changes

[#836]: https://github.com/tafia/quick-xml/pull/836


## 0.37.1 -- 2024-11-17

Expand Down
50 changes: 49 additions & 1 deletion src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod text;
use self::content::ContentSerializer;
use self::element::{ElementSerializer, Map, Struct, Tuple};
use crate::de::TEXT_KEY;
use crate::writer::Indentation;
use crate::writer::{Indentation, ToFmtWrite};
use serde::ser::{self, Serialize};
use serde::serde_if_integer128;
use std::fmt::Write;
Expand Down Expand Up @@ -136,6 +136,54 @@ where
value.serialize(Serializer::new(&mut writer))
}

/// Serialize struct into a `io::Write`r restricted to utf-8 encoding.
///
/// Returns the classification of the last written type.
///
/// # Examples
///
/// ```
/// # use quick_xml::se::to_utf8_io_writer;
/// # use serde::Serialize;
/// # use pretty_assertions::assert_eq;
/// # use std::io::BufWriter;
/// # use std::str;
/// #[derive(Serialize)]
/// struct Root<'a> {
/// #[serde(rename = "@attribute")]
/// attribute: &'a str,
/// element: &'a str,
/// #[serde(rename = "$text")]
/// text: &'a str,
/// }
///
/// let data = Root {
/// attribute: "attribute content",
/// element: "element content",
/// text: "text content",
/// };
///
/// let mut buffer = Vec::new();
/// to_utf8_io_writer(&mut BufWriter::new(&mut buffer), &data).unwrap();
///
/// assert_eq!(
/// str::from_utf8(&buffer).unwrap(),
/// // The root tag name is automatically deduced from the struct name
/// // This will not work for other types or struct with #[serde(flatten)] fields
/// "<Root attribute=\"attribute content\">\
/// <element>element content</element>\
/// text content\
/// </Root>"
/// );
/// ```
pub fn to_utf8_io_writer<W, T>(writer: W, value: &T) -> Result<WriteResult, SeError>
where
W: std::io::Write,
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(&mut ToFmtWrite(writer)))
}

/// Serialize struct into a `String`.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
}
}
#[cfg(feature = "serialize")]
struct ToFmtWrite<T>(pub T);
pub(crate) struct ToFmtWrite<T>(pub T);

#[cfg(feature = "serialize")]
impl<T> std::fmt::Write for ToFmtWrite<T>
Expand Down

0 comments on commit 3b5c74d

Please sign in to comment.