Skip to content

Commit 3b5c74d

Browse files
pronebirdMingun
authored andcommitted
Add to_utf8_io_writer accepting std::io::Write
1 parent 0793d6a commit 3b5c74d

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515

1616
### New Features
1717

18+
- [#836]: Add `se::to_utf8_io_writer()` helper compatible with `std::io::Write` and restricted to UTF-8 encoding.
19+
1820
### Bug Fixes
1921

2022
### Misc Changes
2123

24+
[#836]: https://github.com/tafia/quick-xml/pull/836
25+
2226

2327
## 0.37.1 -- 2024-11-17
2428

src/se/mod.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod text;
8282
use self::content::ContentSerializer;
8383
use self::element::{ElementSerializer, Map, Struct, Tuple};
8484
use crate::de::TEXT_KEY;
85-
use crate::writer::Indentation;
85+
use crate::writer::{Indentation, ToFmtWrite};
8686
use serde::ser::{self, Serialize};
8787
use serde::serde_if_integer128;
8888
use std::fmt::Write;
@@ -136,6 +136,54 @@ where
136136
value.serialize(Serializer::new(&mut writer))
137137
}
138138

139+
/// Serialize struct into a `io::Write`r restricted to utf-8 encoding.
140+
///
141+
/// Returns the classification of the last written type.
142+
///
143+
/// # Examples
144+
///
145+
/// ```
146+
/// # use quick_xml::se::to_utf8_io_writer;
147+
/// # use serde::Serialize;
148+
/// # use pretty_assertions::assert_eq;
149+
/// # use std::io::BufWriter;
150+
/// # use std::str;
151+
/// #[derive(Serialize)]
152+
/// struct Root<'a> {
153+
/// #[serde(rename = "@attribute")]
154+
/// attribute: &'a str,
155+
/// element: &'a str,
156+
/// #[serde(rename = "$text")]
157+
/// text: &'a str,
158+
/// }
159+
///
160+
/// let data = Root {
161+
/// attribute: "attribute content",
162+
/// element: "element content",
163+
/// text: "text content",
164+
/// };
165+
///
166+
/// let mut buffer = Vec::new();
167+
/// to_utf8_io_writer(&mut BufWriter::new(&mut buffer), &data).unwrap();
168+
///
169+
/// assert_eq!(
170+
/// str::from_utf8(&buffer).unwrap(),
171+
/// // The root tag name is automatically deduced from the struct name
172+
/// // This will not work for other types or struct with #[serde(flatten)] fields
173+
/// "<Root attribute=\"attribute content\">\
174+
/// <element>element content</element>\
175+
/// text content\
176+
/// </Root>"
177+
/// );
178+
/// ```
179+
pub fn to_utf8_io_writer<W, T>(writer: W, value: &T) -> Result<WriteResult, SeError>
180+
where
181+
W: std::io::Write,
182+
T: ?Sized + Serialize,
183+
{
184+
value.serialize(Serializer::new(&mut ToFmtWrite(writer)))
185+
}
186+
139187
/// Serialize struct into a `String`.
140188
///
141189
/// # Examples

src/writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
577577
}
578578
}
579579
#[cfg(feature = "serialize")]
580-
struct ToFmtWrite<T>(pub T);
580+
pub(crate) struct ToFmtWrite<T>(pub T);
581581

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

0 commit comments

Comments
 (0)