From 44bca2d032ca7b09f057652421c255503f8dc3d8 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Thu, 1 Sep 2022 17:44:22 +0200 Subject: [PATCH] feat: Allow srializer with custom eventwriter This way, one can configure parametes for the xml crate, like indentation --- src/ser/mod.rs | 4 ++++ tests/emiter_config.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/emiter_config.rs diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 8130a8d..4d12163 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -100,6 +100,10 @@ where Self::new_from_writer(EmitterConfig::new().create_writer(writer)) } + pub fn with_writer(writer: EventWriter) -> Self { + Self::new_from_writer(writer) + } + fn next(&mut self, event: XmlEvent) -> Result<()> { self.writer.write(event)?; Ok(()) diff --git a/tests/emiter_config.rs b/tests/emiter_config.rs new file mode 100644 index 0000000..b37fa80 --- /dev/null +++ b/tests/emiter_config.rs @@ -0,0 +1,24 @@ +use serde::{Serialize}; +use xml::EmitterConfig; +use serde_xml_rs::Serializer; + +#[derive(Debug, Serialize, PartialEq)] +struct Item { + name: String, + source: String, +} + +#[test] +fn serializer_should_accept_custom_emitter() { + let item = Item { + name: "john".to_string(), + source: "outerworld".to_string(), + }; + let mut output = Vec::new(); + { + let w = EmitterConfig::default().perform_indent(true).create_writer(&mut output); + let mut serializer = Serializer::with_writer(w); + item.serialize(&mut serializer).unwrap(); + } + assert_eq!("\n\n john\n outerworld\n", String::from_utf8_lossy(&output)); +} \ No newline at end of file