summaryrefslogtreecommitdiffstats
path: root/src/tools/rustfmt/src/emitter/checkstyle/xml.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rustfmt/src/emitter/checkstyle/xml.rs')
-rw-r--r--src/tools/rustfmt/src/emitter/checkstyle/xml.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/tools/rustfmt/src/emitter/checkstyle/xml.rs b/src/tools/rustfmt/src/emitter/checkstyle/xml.rs
new file mode 100644
index 000000000..f251aabe8
--- /dev/null
+++ b/src/tools/rustfmt/src/emitter/checkstyle/xml.rs
@@ -0,0 +1,52 @@
+use std::fmt::{self, Display};
+
+/// Convert special characters into XML entities.
+/// This is needed for checkstyle output.
+pub(super) struct XmlEscaped<'a>(pub(super) &'a str);
+
+impl<'a> Display for XmlEscaped<'a> {
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ for char in self.0.chars() {
+ match char {
+ '<' => write!(formatter, "&lt;"),
+ '>' => write!(formatter, "&gt;"),
+ '"' => write!(formatter, "&quot;"),
+ '\'' => write!(formatter, "&apos;"),
+ '&' => write!(formatter, "&amp;"),
+ _ => write!(formatter, "{}", char),
+ }?;
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn special_characters_are_escaped() {
+ assert_eq!(
+ "&lt;&gt;&quot;&apos;&amp;",
+ format!("{}", XmlEscaped(r#"<>"'&"#)),
+ );
+ }
+
+ #[test]
+ fn special_characters_are_escaped_in_string_with_other_characters() {
+ assert_eq!(
+ "The quick brown &quot;🦊&quot; jumps &lt;over&gt; the lazy 🐶",
+ format!(
+ "{}",
+ XmlEscaped(r#"The quick brown "🦊" jumps <over> the lazy 🐶"#)
+ ),
+ );
+ }
+
+ #[test]
+ fn other_characters_are_not_escaped() {
+ let string = "The quick brown 🦊 jumps over the lazy 🐶";
+ assert_eq!(string, format!("{}", XmlEscaped(string)));
+ }
+}