Skip to content

Commit

Permalink
fix: update serde API for custom escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
phdavis1027 committed Apr 21, 2024
1 parent 7e18f7e commit 5ef4e03
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod escapei;
pub mod escape {
//! Manage xml character escapes
pub use crate::escapei::{
escape, minimal_escape, partial_escape, unescape, unescape_with, EscapeError,
escape, escape_with, minimal_escape, partial_escape, unescape, unescape_with, EscapeError,
};
}
pub mod events;
Expand Down
134 changes: 90 additions & 44 deletions src/se/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,63 +33,96 @@ fn escape_item(value: &str, target: QuoteTarget, level: QuoteLevel) -> Cow<str>
match (target, level) {
(_, Full) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items, cannot be used in the item
b' ' | b'\r' | b'\n' | b'\t' => true,
// Required characters to escape
b'&' | b'<' | b'>' | b'\'' | b'\"' => true,
_ => false,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
b'\'' => Some(b"&apos;"),
b'"' => Some(b"&quot;"),
_ => None,
}),
//----------------------------------------------------------------------
(Text, Partial) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items, cannot be used in the item
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' | b'>' => true,
_ => false,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
_ => None,
}),
(Text, Minimal) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items, cannot be used in the item
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' => true,
_ => false,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
_ => None,
}),
//----------------------------------------------------------------------
(DoubleQAttr, Partial) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items, cannot be used in the item
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' | b'>' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
// Double quoted attribute should escape quote
b'"' => true,
_ => false,
b'"' => Some(b"&quot;"),
_ => None,
}),
(DoubleQAttr, Minimal) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items, cannot be used in the item
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
// Double quoted attribute should escape quote
b'"' => true,
_ => false,
b'"' => Some(b"&quot;"),
_ => None,
}),
//----------------------------------------------------------------------
(SingleQAttr, Partial) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' | b'>' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
// Single quoted attribute should escape quote
b'\'' => true,
_ => false,
b'\'' => Some(b"&apos;"),
_ => None,
}),
(SingleQAttr, Minimal) => _escape(value, |ch| match ch {
// Spaces used as delimiters of list items
b' ' | b'\r' | b'\n' | b'\t' => true,
b' ' => Some(b"&#32;"),
b'\r' => Some(b"&#13;"),
b'\n' => Some(b"&#10;"),
b'\t' => Some(b"&#9;"),
// Required characters to escape
b'&' | b'<' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
// Single quoted attribute should escape quote
b'\'' => true,
_ => false,
b'\'' => Some(b"&apos;"),
_ => None,
}),
}
}
Expand All @@ -102,49 +135,62 @@ fn escape_list(value: &str, target: QuoteTarget, level: QuoteLevel) -> Cow<str>
match (target, level) {
(_, Full) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' | b'>' | b'\'' | b'\"' => true,
_ => false,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
b'\'' => Some(b"&apos;"),
b'"' => Some(b"&quot;"),
_ => None,
}),
//----------------------------------------------------------------------
(Text, Partial) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' | b'>' => true,
_ => false,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
_ => None,
}),
(Text, Minimal) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' => true,
_ => false,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
_ => None,
}),
//----------------------------------------------------------------------
(DoubleQAttr, Partial) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' | b'>' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
// Double quoted attribute should escape quote
b'"' => true,
_ => false,
b'"' => Some(b"&quot;"),
_ => None,
}),
(DoubleQAttr, Minimal) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
// Double quoted attribute should escape quote
b'"' => true,
_ => false,
b'"' => Some(b"&quot;"),
_ => None,
}),
//----------------------------------------------------------------------
(SingleQAttr, Partial) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' | b'>' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
b'>' => Some(b"&gt;"),
// Single quoted attribute should escape quote
b'\'' => true,
_ => false,
b'\'' => Some(b"&apos;"),
_ => None,
}),
(SingleQAttr, Minimal) => _escape(value, |ch| match ch {
// Required characters to escape
b'&' | b'<' => true,
b'&' => Some(b"&amp;"),
b'<' => Some(b"&lt;"),
// Single quoted attribute should escape quote
b'\'' => true,
_ => false,
b'\'' => Some(b"&apos;"),
_ => None,
}),
}
}
Expand Down

0 comments on commit 5ef4e03

Please sign in to comment.