Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow srializer with custom eventwriter #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions tests/emiter_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::Serialize;
use serde_xml_rs::Serializer;
use xml::EmitterConfig;

#[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));
}