Skip to main content

fakecloud_aws/
xml.rs

1/// Helper to wrap an XML body with the standard XML declaration.
2pub fn wrap_xml(inner: &str) -> String {
3    format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>{inner}")
4}
5
6/// Escape a string for safe embedding in XML content.
7///
8/// Handles the five standard XML entities plus control characters that are
9/// invalid in XML 1.0 (everything below U+0020 except `\t`, `\n`, `\r`).
10pub fn xml_escape(s: &str) -> String {
11    let mut out = String::with_capacity(s.len());
12    for c in s.chars() {
13        match c {
14            '&' => out.push_str("&amp;"),
15            '<' => out.push_str("&lt;"),
16            '>' => out.push_str("&gt;"),
17            '"' => out.push_str("&quot;"),
18            '\'' => out.push_str("&apos;"),
19            // XML 1.0 allows \t, \n, \r as valid characters; all other control chars
20            // need to be encoded as numeric character references.
21            c if (c as u32) < 0x20 && c != '\t' && c != '\n' && c != '\r' => {
22                out.push_str(&format!("&#x{:X};", c as u32));
23            }
24            c => out.push(c),
25        }
26    }
27    out
28}