Add Utf8Writer

This commit is contained in:
Chris Wong 2015-09-06 12:59:41 +12:00
parent 4c6fe05395
commit cdcacab1ea
2 changed files with 74 additions and 0 deletions
maud/src
maud_macros/tests

View file

@ -5,6 +5,70 @@
//!
//! [book]: http://lfairy.gitbooks.io/maud/content/
use std::fmt;
use std::io;
/// Wraps a `std::io::Write` in a `std::fmt::Write`.
///
/// Most I/O libraries work with binary data (`[u8]`), but Maud outputs
/// Unicode strings (`str`). This adapter links them together by
/// encoding the output as UTF-8.
///
/// # Example
///
/// ```rust,ignore
/// use std::io;
/// let writer = Utf8Writer::new(io::stdout());
/// let _ = html!(writer, p { "Hello, " $name "!" });
/// let result = writer.into_result();
/// result.unwrap();
/// ```
pub struct Utf8Writer<W: io::Write> {
inner: W,
result: io::Result<()>,
}
impl<W: io::Write> Utf8Writer<W> {
/// Creates a `Utf8Writer` from a `std::io::Write`.
pub fn new(inner: W) -> Utf8Writer<W> {
Utf8Writer {
inner: inner,
result: Ok(()),
}
}
pub fn into_inner(self) -> (W, io::Result<()>) {
let Utf8Writer { inner, result } = self;
(inner, result)
}
pub fn into_result(self) -> io::Result<()> {
self.result
}
}
impl<W: io::Write> fmt::Write for Utf8Writer<W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
match io::Write::write_all(&mut self.inner, s.as_bytes()) {
Ok(()) => Ok(()),
Err(e) => {
self.result = Err(e);
Err(fmt::Error)
}
}
}
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
match io::Write::write_fmt(&mut self.inner, args) {
Ok(()) => Ok(()),
Err(e) => {
self.result = Err(e);
Err(fmt::Error)
}
}
}
}
/// Escapes an HTML value.
pub fn escape(s: &str) -> String {
use std::fmt::Write;

View file

@ -248,3 +248,13 @@ mod control {
"</ul>"));
}
}
#[test]
fn utf8_writer() {
use maud::Utf8Writer;
let mut w = Utf8Writer::new(vec![]);
let _ = html!(w, p "hello");
let (buf, r) = w.into_inner();
r.unwrap();
assert_eq!(buf, b"<p>hello</p>");
}