Use an opaque Markup type instead of a bare closure

This commit is contained in:
Chris Wong 2015-01-12 15:52:11 +13:00
parent f8b37e5496
commit 663247aef3
3 changed files with 62 additions and 29 deletions
maud/src
maud_macros

View file

@ -4,12 +4,52 @@
use std::fmt; use std::fmt;
use std::fmt::Writer as FmtWriter; use std::fmt::Writer as FmtWriter;
use std::io::{IoError, IoErrorKind, IoResult};
pub type FmtResult<T> = Result<T, fmt::Error>; pub type FmtResult<T> = Result<T, fmt::Error>;
/// Escape an HTML value. /// Escape an HTML value.
pub fn escape(s: &str) -> String { pub fn escape(s: &str) -> String {
render(|w| rt::escape(w, |w| w.write_str(s))) let mut buf = String::new();
rt::escape(&mut buf, |w| w.write_str(s)).unwrap();
buf
}
/// A block of HTML markup, as returned by the `html!` macro.
pub struct Markup<'a, 'b: 'a> {
callback: &'a (Fn(&mut FmtWriter) -> FmtResult<()> + 'b),
}
impl<'a, 'b> Markup<'a, 'b> {
/// Render the markup to a `String`.
pub fn render(&self) -> String {
let mut buf = String::new();
self.render_fmt(&mut buf).unwrap();
buf
}
/// Render the markup to a `std::io::Writer`.
pub fn render_to(&self, w: &mut Writer) -> IoResult<()> {
struct WriterWrapper<'a, 'b: 'a> {
inner: &'a mut (Writer + 'b),
}
impl<'a, 'b> FmtWriter for WriterWrapper<'a, 'b> {
fn write_str(&mut self, s: &str) -> FmtResult<()> {
self.inner.write_str(s).map_err(|_| fmt::Error)
}
}
self.render_fmt(&mut WriterWrapper { inner: w })
.map_err(|_| IoError {
kind: IoErrorKind::OtherIoError,
desc: "formatting error",
detail: None,
})
}
/// Render the markup to a `std::fmt::Writer`.
pub fn render_fmt(&self, w: &mut FmtWriter) -> FmtResult<()> {
(self.callback)(w)
}
} }
/// Internal functions used by the `maud_macros` package. You should /// Internal functions used by the `maud_macros` package. You should
@ -18,7 +58,12 @@ pub fn escape(s: &str) -> String {
Use the macros in `maud_macros` instead."] Use the macros in `maud_macros` instead."]
pub mod rt { pub mod rt {
use std::fmt::Writer as FmtWriter; use std::fmt::Writer as FmtWriter;
use super::FmtResult; use super::{FmtResult, Markup};
#[inline]
pub fn make_markup<'a, 'b>(f: &'a (Fn(&mut FmtWriter) -> FmtResult<()> + 'b)) -> Markup<'a, 'b> {
Markup { callback: f }
}
struct Escaper<'a, 'b: 'a> { struct Escaper<'a, 'b: 'a> {
inner: &'a mut (FmtWriter + 'b), inner: &'a mut (FmtWriter + 'b),
@ -47,12 +92,3 @@ pub mod rt {
f(&mut Escaper { inner: w }) f(&mut Escaper { inner: w })
} }
} }
/// Render a template into a `String`.
pub fn render<F>(template: F) -> String where
F: FnOnce(&mut FmtWriter) -> FmtResult<()>
{
let mut buf = String::new();
template(&mut buf).unwrap();
buf
}

View file

@ -33,10 +33,11 @@ impl<'cx, 's: 'cx, 'o> Renderer<'cx, 's, 'o> {
f(&mut render); f(&mut render);
render.cx render.cx
}; };
quote_expr!(cx, |&: $w: &mut ::std::fmt::Writer| -> ::std::result::Result<(), ::std::fmt::Error> { quote_expr!(cx,
$stmts ::maud::rt::make_markup(&|&: $w: &mut ::std::fmt::Writer| -> Result<(), ::std::fmt::Error> {
Ok(()) $stmts
}) Ok(())
}))
} }
/// Append a literal pre-escaped string. /// Append a literal pre-escaped string.

View file

@ -6,49 +6,45 @@ extern crate maud;
#[test] #[test]
fn it_works() { fn it_works() {
let template = html!("du\tcks" -23 3.14 '\n' "geese"); let s = html!("du\tcks" -23 3.14 '\n' "geese").render();
let s = maud::render(template);
assert_eq!(s, "du\tcks-233.14\ngeese"); assert_eq!(s, "du\tcks-233.14\ngeese");
} }
#[test] #[test]
fn escaping() { fn escaping() {
let template = html!("<flim&flam>"); let s = html!("<flim&flam>").render();
let s = maud::render(template);
assert_eq!(s, "&lt;flim&amp;flam&gt;"); assert_eq!(s, "&lt;flim&amp;flam&gt;");
} }
#[test] #[test]
fn blocks() { fn blocks() {
let s = maud::render(html! { let s = html! {
"hello" "hello"
{ {
" ducks"; " ducks";
" geese"; " geese";
} }
" swans" " swans"
}); }.render();
assert_eq!(s, "hello ducks geese swans"); assert_eq!(s, "hello ducks geese swans");
} }
mod splice { mod splice {
use super::maud; // lol
#[test] #[test]
fn literal() { fn literal() {
let s = maud::render(html! { $"<pinkie>" }); let s = html! { $"<pinkie>" }.render();
assert_eq!(s, "&lt;pinkie&gt;"); assert_eq!(s, "&lt;pinkie&gt;");
} }
#[test] #[test]
fn raw_literal() { fn raw_literal() {
let s = maud::render(html! { $$"<pinkie>" }); let s = html! { $$"<pinkie>" }.render();
assert_eq!(s, "<pinkie>"); assert_eq!(s, "<pinkie>");
} }
#[test] #[test]
fn block() { fn block() {
let s = maud::render(html! { let s = html! {
${ ${
let mut result = 1i32; let mut result = 1i32;
for i in range(2, 11) { for i in range(2, 11) {
@ -56,7 +52,7 @@ mod splice {
} }
result result
} }
}); }.render();
assert_eq!(s, "3628800"); assert_eq!(s, "3628800");
} }
@ -64,7 +60,7 @@ mod splice {
#[test] #[test]
fn statics() { fn statics() {
let s = maud::render(html! { $BEST_PONY }); let s = html! { $BEST_PONY }.render();
assert_eq!(s, "Pinkie Pie"); assert_eq!(s, "Pinkie Pie");
} }
@ -74,7 +70,7 @@ mod splice {
#[test] #[test]
fn closure() { fn closure() {
let best_pony = "Pinkie Pie"; let best_pony = "Pinkie Pie";
let s = maud::render(html! { $best_pony }); let s = html! { $best_pony }.render();
assert_eq!(s, "Pinkie Pie"); assert_eq!(s, "Pinkie Pie");
} }
*/ */