Skip to content

Commit 6fbd4e8

Browse files
authored
Merge pull request #556 from Mingun/helpers
Add helper functions `to_*_with_root` that writes XML with the specified root tag name
2 parents 64292c7 + f783f2d commit 6fbd4e8

File tree

2 files changed

+161
-4
lines changed

2 files changed

+161
-4
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
- [#541]: Deserialize specially named `$text` enum variant in [externally tagged]
1616
enums from textual content
17+
- [#556]: `to_writer` and `to_string` now accept `?Sized` types
18+
- [#556]: Add new `to_writer_with_root` and `to_string_with_root` helper functions
1719

1820
### Bug Fixes
1921

@@ -29,6 +31,7 @@
2931
[#510]: https://github.com/tafia/quick-xml/issues/510
3032
[#537]: https://github.com/tafia/quick-xml/issues/537
3133
[#541]: https://github.com/tafia/quick-xml/pull/541
34+
[#556]: https://github.com/tafia/quick-xml/pull/556
3235

3336
## 0.27.1 -- 2022-12-28
3437

src/se/mod.rs

Lines changed: 158 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,170 @@ use serde::serde_if_integer128;
8787
use std::fmt::Write;
8888
use std::str::from_utf8;
8989

90-
/// Serialize struct into a `Write`r
91-
pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> Result<W, DeError> {
90+
/// Serialize struct into a `Write`r.
91+
///
92+
/// # Examples
93+
///
94+
/// ```
95+
/// # use quick_xml::se::to_writer;
96+
/// # use serde::Serialize;
97+
/// # use pretty_assertions::assert_eq;
98+
/// #[derive(Serialize)]
99+
/// struct Root<'a> {
100+
/// #[serde(rename = "@attribute")]
101+
/// attribute: &'a str,
102+
/// element: &'a str,
103+
/// #[serde(rename = "$text")]
104+
/// text: &'a str,
105+
/// }
106+
///
107+
/// let data = Root {
108+
/// attribute: "attribute content",
109+
/// element: "element content",
110+
/// text: "text content",
111+
/// };
112+
///
113+
/// assert_eq!(
114+
/// to_writer(String::new(), &data).unwrap(),
115+
/// // The root tag name is automatically deduced from the struct name
116+
/// // This will not work for other types or struct with #[serde(flatten)] fields
117+
/// "<Root attribute=\"attribute content\">\
118+
/// <element>element content</element>\
119+
/// text content\
120+
/// </Root>"
121+
/// );
122+
/// ```
123+
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<W, DeError>
124+
where
125+
W: Write,
126+
T: ?Sized + Serialize,
127+
{
92128
value.serialize(Serializer::new(writer))
93129
}
94130

95-
/// Serialize struct into a `String`
96-
pub fn to_string<S: Serialize>(value: &S) -> Result<String, DeError> {
131+
/// Serialize struct into a `String`.
132+
///
133+
/// # Examples
134+
///
135+
/// ```
136+
/// # use quick_xml::se::to_string;
137+
/// # use serde::Serialize;
138+
/// # use pretty_assertions::assert_eq;
139+
/// #[derive(Serialize)]
140+
/// struct Root<'a> {
141+
/// #[serde(rename = "@attribute")]
142+
/// attribute: &'a str,
143+
/// element: &'a str,
144+
/// #[serde(rename = "$text")]
145+
/// text: &'a str,
146+
/// }
147+
///
148+
/// let data = Root {
149+
/// attribute: "attribute content",
150+
/// element: "element content",
151+
/// text: "text content",
152+
/// };
153+
///
154+
/// assert_eq!(
155+
/// to_string(&data).unwrap(),
156+
/// // The root tag name is automatically deduced from the struct name
157+
/// // This will not work for other types or struct with #[serde(flatten)] fields
158+
/// "<Root attribute=\"attribute content\">\
159+
/// <element>element content</element>\
160+
/// text content\
161+
/// </Root>"
162+
/// );
163+
/// ```
164+
pub fn to_string<T>(value: &T) -> Result<String, DeError>
165+
where
166+
T: ?Sized + Serialize,
167+
{
97168
to_writer(String::new(), value)
98169
}
99170

171+
/// Serialize struct into a `Write`r using specified root tag name.
172+
/// `root_tag` should be valid [XML name], otherwise error is returned.
173+
///
174+
/// # Examples
175+
///
176+
/// ```
177+
/// # use quick_xml::se::to_writer_with_root;
178+
/// # use serde::Serialize;
179+
/// # use pretty_assertions::assert_eq;
180+
/// #[derive(Serialize)]
181+
/// struct Root<'a> {
182+
/// #[serde(rename = "@attribute")]
183+
/// attribute: &'a str,
184+
/// element: &'a str,
185+
/// #[serde(rename = "$text")]
186+
/// text: &'a str,
187+
/// }
188+
///
189+
/// let data = Root {
190+
/// attribute: "attribute content",
191+
/// element: "element content",
192+
/// text: "text content",
193+
/// };
194+
///
195+
/// assert_eq!(
196+
/// to_writer_with_root(String::new(), "top-level", &data).unwrap(),
197+
/// "<top-level attribute=\"attribute content\">\
198+
/// <element>element content</element>\
199+
/// text content\
200+
/// </top-level>"
201+
/// );
202+
/// ```
203+
///
204+
/// [XML name]: https://www.w3.org/TR/REC-xml/#NT-Name
205+
pub fn to_writer_with_root<W, T>(writer: W, root_tag: &str, value: &T) -> Result<W, DeError>
206+
where
207+
W: Write,
208+
T: ?Sized + Serialize,
209+
{
210+
value.serialize(Serializer::with_root(writer, Some(root_tag))?)
211+
}
212+
213+
/// Serialize struct into a `String` using specified root tag name.
214+
/// `root_tag` should be valid [XML name], otherwise error is returned.
215+
///
216+
/// # Examples
217+
///
218+
/// ```
219+
/// # use quick_xml::se::to_string_with_root;
220+
/// # use serde::Serialize;
221+
/// # use pretty_assertions::assert_eq;
222+
/// #[derive(Serialize)]
223+
/// struct Root<'a> {
224+
/// #[serde(rename = "@attribute")]
225+
/// attribute: &'a str,
226+
/// element: &'a str,
227+
/// #[serde(rename = "$text")]
228+
/// text: &'a str,
229+
/// }
230+
///
231+
/// let data = Root {
232+
/// attribute: "attribute content",
233+
/// element: "element content",
234+
/// text: "text content",
235+
/// };
236+
///
237+
/// assert_eq!(
238+
/// to_string_with_root("top-level", &data).unwrap(),
239+
/// "<top-level attribute=\"attribute content\">\
240+
/// <element>element content</element>\
241+
/// text content\
242+
/// </top-level>"
243+
/// );
244+
/// ```
245+
///
246+
/// [XML name]: https://www.w3.org/TR/REC-xml/#NT-Name
247+
pub fn to_string_with_root<T>(root_tag: &str, value: &T) -> Result<String, DeError>
248+
where
249+
T: ?Sized + Serialize,
250+
{
251+
to_writer_with_root(String::new(), root_tag, value)
252+
}
253+
100254
////////////////////////////////////////////////////////////////////////////////////////////////////
101255

102256
/// Defines which characters would be escaped in [`Text`] events and attribute

0 commit comments

Comments
 (0)