Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_utf8_io_writer helper serializing into std::io::Write #836

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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