Skip to content

Commit

Permalink
feat: Allow srializer with custom eventwriter
Browse files Browse the repository at this point in the history
This way, one can configure parametes for the xml crate, like indentation
  • Loading branch information
jaysonsantos committed Sep 1, 2022
1 parent 920e54d commit 44bca2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ where
Self::new_from_writer(EmitterConfig::new().create_writer(writer))
}

pub fn with_writer(writer: EventWriter<W>) -> Self {
Self::new_from_writer(writer)
}

fn next(&mut self, event: XmlEvent) -> Result<()> {
self.writer.write(event)?;
Ok(())
Expand Down
24 changes: 24 additions & 0 deletions tests/emiter_config.rs
Original file line number Diff line number Diff line change
@@ -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!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Item>\n <name>john</name>\n <source>outerworld</source>\n</Item>", String::from_utf8_lossy(&output));
}

0 comments on commit 44bca2d

Please sign in to comment.