parent
7925fbad43
commit
c57ee6e683
3 changed files with 4 additions and 50 deletions
|
@ -85,41 +85,6 @@ impl Render for str {
|
|||
}
|
||||
}
|
||||
|
||||
/// Represents a type that can be rendered as HTML, where the rendering
|
||||
/// operation consumes the value.
|
||||
///
|
||||
/// See the [`Render`](trait.Render.html) documentation for advice on
|
||||
/// how to use this trait.
|
||||
pub trait RenderOnce: Sized {
|
||||
/// Renders `self` as a block of `Markup`, consuming it in the
|
||||
/// process.
|
||||
fn render_once(self) -> Markup {
|
||||
let mut buffer = String::new();
|
||||
self.render_once_to(&mut buffer);
|
||||
PreEscaped(buffer)
|
||||
}
|
||||
|
||||
/// Appends a representation of `self` to the given string,
|
||||
/// consuming `self` in the process.
|
||||
///
|
||||
/// Its default implementation just calls `.render_once()`, but you
|
||||
/// may override it with something more efficient.
|
||||
///
|
||||
/// Note that no further escaping is performed on data written to
|
||||
/// the buffer. If you override this method, you must make sure that
|
||||
/// any data written is properly escaped, whether by hand or using
|
||||
/// the [`Escaper`](struct.Escaper.html) wrapper struct.
|
||||
fn render_once_to(self, buffer: &mut String) {
|
||||
buffer.push_str(&self.render_once().into_string());
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Render + ?Sized> RenderOnce for &'a T {
|
||||
fn render_once_to(self, w: &mut String) {
|
||||
self.render_to(w);
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper that renders the inner value without escaping.
|
||||
#[derive(Debug)]
|
||||
pub struct PreEscaped<T: AsRef<str>>(pub T);
|
||||
|
|
|
@ -89,7 +89,10 @@ impl<'cx, 'a> Renderer<'cx, 'a> {
|
|||
/// Appends the result of an expression.
|
||||
pub fn splice(&mut self, expr: P<Expr>) {
|
||||
let w = self.writer;
|
||||
let expr = quote_expr!(self.cx, { use ::maud::RenderOnce; $expr.render_once_to(&mut $w) });
|
||||
let expr = quote_expr!(self.cx, {
|
||||
use ::maud::Render as __maud_Render;
|
||||
$expr.render_to(&mut $w);
|
||||
});
|
||||
let stmt = self.wrap_stmt(expr);
|
||||
self.push(stmt);
|
||||
}
|
||||
|
|
|
@ -66,17 +66,3 @@ fn render_impl() {
|
|||
assert_eq!(s1, "pinkie");
|
||||
assert_eq!(s2, "pinkie");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_once_impl() {
|
||||
struct Once(String);
|
||||
impl maud::RenderOnce for Once {
|
||||
fn render_once_to(self, w: &mut String) {
|
||||
w.push_str(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
let once = Once(String::from("pinkie"));
|
||||
let s = html!((once)).into_string();
|
||||
assert_eq!(s, "pinkie");
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue