diff --git a/implementations/rust/preserves/src/value/text/writer.rs b/implementations/rust/preserves/src/value/text/writer.rs index edb938b..8ac796d 100644 --- a/implementations/rust/preserves/src/value/text/writer.rs +++ b/implementations/rust/preserves/src/value/text/writer.rs @@ -24,6 +24,7 @@ pub struct TextWriter { w: Suspendable, pub comma_style: CommaStyle, pub indentation: usize, + pub escape_spaces: bool, indent: String, } @@ -54,10 +55,16 @@ impl TextWriter { w: Suspendable::new(w), comma_style: CommaStyle::default(), indentation: 0, + escape_spaces: false, indent: "\n".to_owned(), } } + pub fn set_escape_spaces(mut self, v: bool) -> Self { + self.escape_spaces = v; + self + } + pub fn suspend(&mut self) -> Self { TextWriter { w: self.w.suspend(), indent: self.indent.clone(), .. *self } } @@ -229,6 +236,7 @@ impl Writer for TextWriter { for c in v.chars() { match c { '"' => write!(self.w, "\\\"")?, + ' ' if self.escape_spaces => write!(self.w, "\\u0020")?, _ => self.write_stringlike_char(c)?, } } @@ -249,6 +257,7 @@ impl Writer for TextWriter { for c in v.chars() { match c { '|' => write!(self.w, "\\|")?, + ' ' if self.escape_spaces => write!(self.w, "\\u0020")?, _ => self.write_stringlike_char(c)?, } }