maud/benchmarks/benches/complicated_maud.rs
Eshin Kunishima 3a3e55429a Update dependencies for benchmarks ()
* Update dependencies for benchmarks

Template engines
- maud 0.17.1 (local)
- handlebars 0.29.1
- horrorshow 0.6.2
- liquid 0.10.1
- tera 0.10.10

* Update benchmark dependencies

* Remove unused feature

* Revert indentation

* Fix unused Result
2017-09-30 17:16:21 +13:00

100 lines
2.4 KiB
Rust

#![feature(proc_macro, test)]
extern crate maud;
extern crate test;
use maud::{Markup, html};
#[derive(Debug)]
struct Entry {
name: &'static str,
score: u32,
}
mod btn {
use maud::{Markup, Render, html};
#[derive(Copy, Clone)]
pub enum RequestMethod {
Get,
Post
}
#[derive(Copy, Clone)]
pub struct Button<'a> {
label: &'a str,
path: &'a str,
req_meth: RequestMethod,
}
impl<'a> Button<'a> {
pub fn new(label: &'a str, path: &'a str) -> Button<'a> {
Button {
label: label,
path: path,
req_meth: RequestMethod::Get,
}
}
pub fn with_method(mut self, meth: RequestMethod) -> Button<'a> {
self.req_meth = meth;
self
}
}
impl<'a> Render for Button<'a> {
fn render(&self) -> Markup {
match self.req_meth {
RequestMethod::Get => {
html! { a.btn href=(self.path) (self.label) }
}
RequestMethod::Post => {
html! {
form method="POST" action=(self.path) {
input.btn type="submit" value=(self.label) /
}
}
}
}
}
}
}
fn layout<S: AsRef<str>>(title: S, inner: Markup) -> Markup {
html! {
html {
head {
title (title.as_ref())
}
body {
(inner)
}
}
}
}
#[bench]
fn render_complicated_template(b: &mut test::Bencher) {
let year = test::black_box("2015");
let teams = test::black_box(vec![
Entry { name: "Jiangsu", score: 43 },
Entry { name: "Beijing", score: 27 },
Entry { name: "Guangzhou", score: 22 },
Entry { name: "Shandong", score: 12 },
]);
b.iter(|| {
use btn::{Button, RequestMethod};
layout(format!("Homepage of {}", year), html! {
h1 { "Hello there!" }
@for entry in &teams {
div {
strong (entry.name)
(Button::new("Edit", "edit"))
(Button::new("Delete", "edit")
.with_method(RequestMethod::Post))
}
}
})
});
}