Add #call instruction

This commit is contained in:
Chris Wong 2015-09-23 14:29:45 +12:00
parent 344df28f90
commit 7d124e616e
3 changed files with 35 additions and 0 deletions

View file

@ -152,6 +152,12 @@ impl<'cx, 'i> Parser<'cx, 'i> {
self.shift(2);
try!(self.for_expr(sp));
},
// Call
[pound!(), ident!(sp, name), ..] if name.name == "call" => {
self.shift(2);
let func = try!(self.splice(sp));
self.render.emit_call(func);
},
// Splice
[ref tt @ dollar!(), dollar!(), ..] => {
self.shift(2);

View file

@ -190,6 +190,13 @@ impl<'cx> Renderer<'cx> {
let stmt = quote_stmt!(self.cx, for $pattern in $iterable { $body }).unwrap();
self.push(stmt);
}
pub fn emit_call(&mut self, func: P<Expr>) {
let w = self.writer;
let expr = quote_expr!(self.cx, ($func)(&mut *$w));
let stmt = self.wrap_try(expr);
self.push(stmt);
}
}
fn html_escape(s: &str) -> String {

View file

@ -3,6 +3,8 @@
extern crate maud;
use std::fmt;
#[test]
fn literals() {
let mut s = String::new();
@ -271,3 +273,23 @@ mod issue_10 {
assert_eq!(s, r#"<this sentence-is="false" of-course></this>"#);
}
}
#[test]
fn call() {
fn ducks(w: &mut fmt::Write) -> fmt::Result {
write!(w, "Ducks")
}
let mut s = String::new();
let swans = |yes|
if yes {
|w: &mut fmt::Write| write!(w, "Swans")
} else {
panic!("oh noes")
};
html!(s, {
#call ducks
#call (|w: &mut fmt::Write| write!(w, "Geese"))
#call swans(true)
}).unwrap();
assert_eq!(s, "DucksGeeseSwans");
}