An XML writer that protects you from XML injections through type safety. If
you forget to escape a string, your code just doesn't compile. Contrary to
other XML writing libraries xmlsafe doesn't require you to escape everything:
you get to choose. Furthermore xmlsafe never panics and avoids allocations by
just writing to a std::fmt::Write
.
xmlsafe introduces three marker traits to mark the XML safety of Display
implementations. Please keep two things in mind:
Whenever you supply a string literal (&'static str
), take care that it
is syntactically valid for the respective context.
Whenever you implement one of the marker traits, take care that you fulfill its requirements.
```rust use std::fmt::{Error, Write}; use xmlsafe::{XmlWriter, formattext, escapetext};
fn writegreeting(w: XmlWriter, name: &str) -> Result<(), Error> { let mut w = w.openstarttag("greeting")?.attr("id", 42)?.close()?; w.write(formattext!("Hello {}!", escapetext(name)))?; w.writeend_tag("greeting")?; Ok(()) }
fn main() {
let mut out = String::new();
writegreeting(XmlWriter::new(&mut out), "Ferris").unwrap();
asserteq!(out, "
```
Note how the XmlWriter
acts as a protective layer between the actual
write target (the String in our example) and the XML generation code. Also
note that if we forgot the escape_text
call, the example would not
compile.