Skip to content

Commit

Permalink
Add ability to set quoting level when writing content
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Nov 23, 2023
1 parent 7924fdb commit d9de2d8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ configuration is serializable.
well-formedness checks.
- [#362]: Added `escape::minimal_escape()` which escapes only `&` and `<`.
- [#362]: Added `BytesCData::minimal_escape()` which escapes only `&` and `<`.
- [#362]: Added `Serializer::set_quote_level()` which allow to set desired level of escaping.

### Bug Fixes

Expand Down
104 changes: 104 additions & 0 deletions src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ impl<'w, 'r, W: Write> Serializer<'w, 'r, W> {
self
}

/// Set the level of quoting used when writing texts
///
/// Default: [`QuoteLevel::Minimal`]
pub fn set_quote_level(&mut self, level: QuoteLevel) -> &mut Self {
self.ser.level = level;
self
}

/// Set the indent object for a serializer
pub(crate) fn set_indent(&mut self, indent: Indent<'r>) -> &mut Self {
self.ser.indent = indent;
Expand Down Expand Up @@ -779,3 +787,99 @@ impl<'w, 'r, W: Write> ser::Serializer for Serializer<'w, 'r, W> {
}
}
}

#[cfg(test)]
mod quote_level {
use super::*;
use pretty_assertions::assert_eq;
use serde::Serialize;

#[derive(Debug, PartialEq, Serialize)]
struct Element(&'static str);

#[derive(Debug, PartialEq, Serialize)]
struct Example {
#[serde(rename = "@attribute")]
attribute: &'static str,
element: Element,
}

#[test]
fn default_() {
let example = Example {
attribute: "special chars: &, <, >, \", '",
element: Element("special chars: &, <, >, \", '"),
};

let mut buffer = String::new();
let ser = Serializer::new(&mut buffer);

example.serialize(ser).unwrap();
assert_eq!(
buffer,
"<Example attribute=\"special chars: &amp;, &lt;, &gt;, &quot;, '\">\
<element>special chars: &amp;, &lt;, &gt;, \", '</element>\
</Example>"
);
}

#[test]
fn minimal() {
let example = Example {
attribute: "special chars: &, <, >, \", '",
element: Element("special chars: &, <, >, \", '"),
};

let mut buffer = String::new();
let mut ser = Serializer::new(&mut buffer);
ser.set_quote_level(QuoteLevel::Minimal);

example.serialize(ser).unwrap();
assert_eq!(
buffer,
"<Example attribute=\"special chars: &amp;, &lt;, >, &quot;, '\">\
<element>special chars: &amp;, &lt;, >, \", '</element>\
</Example>"
);
}

#[test]
fn partial() {
let example = Example {
attribute: "special chars: &, <, >, \", '",
element: Element("special chars: &, <, >, \", '"),
};

let mut buffer = String::new();
let mut ser = Serializer::new(&mut buffer);
ser.set_quote_level(QuoteLevel::Partial);

example.serialize(ser).unwrap();
assert_eq!(
buffer,
"<Example attribute=\"special chars: &amp;, &lt;, &gt;, &quot;, '\">\
<element>special chars: &amp;, &lt;, &gt;, \", '</element>\
</Example>"
);
}

#[test]
fn full() {
let example = Example {
attribute: "special chars: &, <, >, \", '",
element: Element("special chars: &, <, >, \", '"),
};

let mut buffer = String::new();
let mut ser = Serializer::new(&mut buffer);
ser.set_quote_level(QuoteLevel::Full);

example.serialize(ser).unwrap();
assert_eq!(
buffer,
"<Example attribute=\"special chars: &amp;, &lt;, &gt;, &quot;, &apos;\">\
<element>special chars: &amp;, &lt;, &gt;, &quot;, &apos;</element>\
</Example>"
);
}
}

0 comments on commit d9de2d8

Please sign in to comment.