Skip to content

Commit

Permalink
Remove write_serializable_content from element writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jun 18, 2023
1 parent d30e8b2 commit f23517e
Showing 1 changed file with 67 additions and 139 deletions.
206 changes: 67 additions & 139 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,14 @@ impl<W: Write> Writer<W> {
pub fn write_serializable<T: Serialize>(
&mut self,
content: &T,
tag_name: &str,
) -> std::result::Result<(), DeError> {
use crate::se::Serializer;

self.write_indent()?;
let indent = self.indent.clone();
let mut fmt = ToFmtWrite(self.get_mut());
let mut serializer = Serializer::new(&mut fmt);
let mut serializer = Serializer::with_root(&mut fmt, Some(tag_name))?;

if let Some(indent) = indent {
serializer.indent(
Expand Down Expand Up @@ -370,39 +371,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
.write_event(Event::End(self.start_tag.to_end()))?;
Ok(self.writer)
}

/// Serialize an arbitrary value inside the current element
#[cfg(feature = "serialize")]
pub fn write_serializable_content<T: Serialize>(
self,
content: &T,
) -> std::result::Result<&'a mut Writer<W>, DeError> {
use crate::se::Serializer;

self.writer
.write_event(Event::Start(self.start_tag.borrow()))?;
self.writer.write_indent()?;

let indent = self.writer.indent.clone();
let fmt = &mut ToFmtWrite(self.writer.get_mut());
let mut serializer = Serializer::new(fmt);

if let Some(indent) = indent {
serializer.indent(
indent.indent_char as char,
indent.indent_size,
indent.current_indent_len,
);
}

content.serialize(serializer)?;

self.writer
.write_event(Event::End(self.start_tag.to_end()))?;
Ok(self.writer)
}
}

#[cfg(feature = "serialize")]
struct ToFmtWrite<T>(pub T);

Expand Down Expand Up @@ -628,6 +597,71 @@ mod indentation {
);
}

#[cfg(feature = "serialize")]
#[test]
fn serializable() {
#[derive(Serialize)]
struct Foo {
#[serde(rename = "@attribute")]
attribute: &'static str,

element: Bar,
list: Vec<&'static str>,

#[serde(rename = "$text")]
text: &'static str,

val: String,
}

#[derive(Serialize)]
struct Bar {
baz: usize,
bat: usize,
}

let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 4);

let content = Foo {
attribute: "attribute",
element: Bar { baz: 42, bat: 43 },
list: vec!["first element", "second element"],
text: "text",
val: "foo".to_owned(),
};

let start = BytesStart::new("paired")
.with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter());
let end = start.to_end();

writer
.write_event(Event::Start(start.clone()))
.expect("write start tag failed");
writer
.write_serializable(&content, "foo_element")
.expect("write serializable inner contents failed");
writer
.write_event(Event::End(end))
.expect("write end tag failed");

assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2">
<foo_element attribute="attribute">
<element>
<baz>42</baz>
<bat>43</bat>
</element>
<list>first element</list>
<list>second element</list>
text
<val>foo</val>
</foo_element>
</paired>"#
);
}

#[test]
fn element_writer_empty() {
let mut buffer = Vec::new();
Expand Down Expand Up @@ -704,110 +738,4 @@ mod indentation {
</outer>"#
);
}

#[cfg(feature = "serialize")]
mod write_serializable {
use super::*;

#[derive(Serialize)]
struct Foo {
#[serde(rename = "@attribute")]
attribute: &'static str,

element: Bar,
list: Vec<&'static str>,

#[serde(rename = "$text")]
text: &'static str,

val: String,
}

#[derive(Serialize)]
struct Bar {
baz: usize,
bat: usize,
}

#[cfg(feature = "serialize")]
#[test]
fn serializable() {
let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 4);

let content = Foo {
attribute: "attribute",
element: Bar { baz: 42, bat: 43 },
list: vec!["first element", "second element"],
text: "text",
val: "foo".to_owned(),
};

let start = BytesStart::new("paired")
.with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter());
let end = start.to_end();

writer
.write_event(Event::Start(start.clone()))
.expect("write start tag failed");
writer
.write_serializable(&content)
.expect("write serializable inner contents failed");
writer
.write_event(Event::End(end))
.expect("write end tag failed");

assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2">
<Foo attribute="attribute">
<element>
<baz>42</baz>
<bat>43</bat>
</element>
<list>first element</list>
<list>second element</list>
text
<val>foo</val>
</Foo>
</paired>"#
);
}

#[test]
fn element_writer_serializable() {
let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 4);
let content = Foo {
attribute: "attribute",
element: Bar { baz: 42, bat: 43 },
list: vec!["first element", "second element"],
text: "text",
val: "foo".to_string(),
};

writer
.create_element("paired")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
.write_serializable_content(&content)
.expect("failure");

assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2">
<Foo attribute="attribute">
<element>
<baz>42</baz>
<bat>43</bat>
</element>
<list>first element</list>
<list>second element</list>
text
<val>foo</val>
</Foo>
</paired>"#
);
}
}
}

0 comments on commit f23517e

Please sign in to comment.