be able to contructor a serializer with output configuration

This commit is contained in:
Marc-Antoine Arnaud 2019-10-08 08:10:18 +02:00
parent e11deedccb
commit 779bddf40a

View File

@ -12,6 +12,13 @@ pub fn to_string<T: YaSerialize>(model: &T) -> Result<String, String> {
Ok(String::from(data)) Ok(String::from(data))
} }
pub fn to_string_with_config<T: YaSerialize>(model: &T, config: &Config) -> Result<String, String> {
let buf = Cursor::new(Vec::new());
let cursor = serialize_with_writer(model, buf)?;
let data = str::from_utf8(cursor.get_ref()).expect("Found invalid UTF-8");
Ok(String::from(data))
}
pub fn serialize_with_writer<W: Write, T: YaSerialize>(model: &T, writer: W) -> Result<W, String> { pub fn serialize_with_writer<W: Write, T: YaSerialize>(model: &T, writer: W) -> Result<W, String> {
let mut serializer = Serializer::new_from_writer(writer); let mut serializer = Serializer::new_from_writer(writer);
match model.serialize(&mut serializer) { match model.serialize(&mut serializer) {
@ -93,3 +100,9 @@ impl<'de, W: Write> Serializer<W> {
self.writer.write(event) self.writer.write(event)
} }
} }
pub struct Config {
perform_indent: bool,
write_document_declaration: bool,
indent_string: Option<String>
}