Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the signature of templates cleaner. #107

Merged
merged 1 commit into from Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,8 @@ project adheres to

## Unreleased

* Breaking change: The generated template functions have a simpler
signature.
* Improve error handling in optional warp support, PR #109.
* Current stable rust is 1.57, MSRV is now 1.46.0.
* Update nom dependency to 7.1.0.
Expand Down
6 changes: 2 additions & 4 deletions examples/actix/src/actix_ructe.rs
@@ -1,5 +1,3 @@
use std::io::Write;

macro_rules! render {
($template:path) => (Render(|o| $template(o)));
($template:path, $($arg:expr),*) => {{
Expand All @@ -12,9 +10,9 @@ macro_rules! render {
}};
}

pub struct Render<T: FnOnce(&mut dyn Write) -> std::io::Result<()>>(pub T);
pub struct Render<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()>>(pub T);

impl<T: FnOnce(&mut dyn Write) -> std::io::Result<()>> From<Render<T>>
impl<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()>> From<Render<T>>
for actix_web::body::Body
{
fn from(t: Render<T>) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/actix/src/main.rs
Expand Up @@ -107,7 +107,7 @@ impl actix_web::error::ResponseError for ExampleAppError {

/// This method can be used as a "template tag", i.e. a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
2 changes: 1 addition & 1 deletion examples/gotham/src/main.rs
Expand Up @@ -41,7 +41,7 @@ fn homepage(state: State) -> (State, Response<Body>) {

/// This method can be used as a "template tag", that is a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
6 changes: 3 additions & 3 deletions examples/gotham/src/ructe_response.rs
Expand Up @@ -2,18 +2,18 @@ use gotham::hyper::http::header::CONTENT_TYPE;
use gotham::hyper::{Body, Response, StatusCode};
use gotham::state::State;
use mime::TEXT_HTML_UTF_8;
use std::io::{self, Write};
use std::io;

pub trait RucteResponse: Sized {
fn html<F>(self, do_render: F) -> (Self, Response<Body>)
where
F: FnOnce(&mut dyn Write) -> io::Result<()>;
F: FnOnce(&mut Vec<u8>) -> io::Result<()>;
}

impl RucteResponse for State {
fn html<F>(self, do_render: F) -> (Self, Response<Body>)
where
F: FnOnce(&mut dyn Write) -> io::Result<()>,
F: FnOnce(&mut Vec<u8>) -> io::Result<()>,
{
let mut buf = Vec::new();
let res = match do_render(&mut buf) {
Expand Down
2 changes: 1 addition & 1 deletion examples/iron/src/main.rs
Expand Up @@ -36,7 +36,7 @@ fn frontpage(_: &mut Request) -> IronResult<Response> {

/// This method can be used as a "template tag", that is a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
5 changes: 3 additions & 2 deletions examples/nickel/src/main.rs
Expand Up @@ -3,6 +3,7 @@ extern crate nickel;
extern crate time;

use nickel::hyper::header::{ContentType, Expires, HttpDate};
use nickel::hyper::server::Streaming;
use nickel::status::StatusCode;
use nickel::{Halt, HttpRouter, MiddlewareResult, Nickel, Request, Response};
use std::io::{self, Write};
Expand Down Expand Up @@ -50,7 +51,7 @@ fn page<'mw>(

fn render<F>(res: Response, do_render: F) -> MiddlewareResult
where
F: FnOnce(&mut dyn Write) -> io::Result<()>,
F: FnOnce(&mut Response<(), Streaming>) -> io::Result<()>,
{
let mut stream = res.start()?;
match do_render(&mut stream) {
Expand All @@ -61,7 +62,7 @@ where

/// This method can be used as a "template tag", that is a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/src/main.rs
@@ -1,6 +1,6 @@
#![allow(dead_code)] // Most templates here are only used in tests.

use std::io::{self, Write};
use std::io;

include!(concat!(env!("OUT_DIR"), "/templates.rs"));
use crate::templates::*;
Expand All @@ -11,7 +11,7 @@ fn main() {

fn r2s<Call>(call: Call) -> String
where
Call: FnOnce(&mut dyn Write) -> io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/templates/issue_66.rs.html
Expand Up @@ -7,5 +7,5 @@
text.push('C');
}

return text;
text
})
4 changes: 2 additions & 2 deletions examples/static-sass/src/main.rs
Expand Up @@ -20,7 +20,7 @@ fn main() {
mod test {
use crate::templates::statics::{StaticFile, STATICS};
use crate::templates::*;
use std::io::{self, Write};
use std::io;

#[test]
fn page_w_static() {
Expand Down Expand Up @@ -74,7 +74,7 @@ mod test {

fn r2s<Call>(call: Call) -> String
where
Call: FnOnce(&mut dyn Write) -> io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/statics/src/main.rs
Expand Up @@ -66,7 +66,7 @@ fn test_all_statics_known() {
#[cfg(test)]
fn r2s<Call>(call: Call) -> String
where
Call: FnOnce(&mut dyn Write) -> io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/tide/src/main.rs
Expand Up @@ -44,7 +44,7 @@ async fn frontpage(_req: Request<()>) -> Result<Response, Error> {
/// interface to get a file by url path.
async fn static_file(req: Request<()>) -> Result<Response, Error> {
let path = req.param("path")?;
StaticFile::get(&path)
StaticFile::get(path)
.ok_or_else(|| Error::from_str(StatusCode::NotFound, "not found"))
.map(static_response)
}
Expand All @@ -70,7 +70,7 @@ const DAY: Duration = Duration::from_secs(24 * 60 * 60);

/// This method can be used as a "template tag", i.e. a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
16 changes: 8 additions & 8 deletions examples/tide/src/ructe_tide.rs
Expand Up @@ -34,7 +34,7 @@ pub trait Render {
/// ```
fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>;

/// Render a template to the html body of self.
///
Expand All @@ -44,13 +44,13 @@ pub trait Render {
/// [`HTML`]: ../../tide/http/mime/constant.HTML.html
fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>;
}

impl Render for tide::Response {
fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf)?;
Expand All @@ -60,7 +60,7 @@ impl Render for tide::Response {

fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
{
self.render(call)?;
self.set_content_type(tide::http::mime::HTML);
Expand Down Expand Up @@ -97,7 +97,7 @@ pub trait RenderBuilder {
/// ```
fn render<Call>(self, call: Call) -> tide::ResponseBuilder
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>;

/// Render a template to the html body of self.
///
Expand All @@ -107,13 +107,13 @@ pub trait RenderBuilder {
/// [`HTML`]: ../../tide/http/mime/constant.HTML.html
fn render_html<Call>(self, call: Call) -> tide::ResponseBuilder
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>;
}

impl RenderBuilder for tide::ResponseBuilder {
fn render<Call>(self, call: Call) -> tide::ResponseBuilder
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
{
let mut buf = Vec::new();
match call(&mut buf) {
Expand All @@ -131,7 +131,7 @@ impl RenderBuilder for tide::ResponseBuilder {

fn render_html<Call>(self, call: Call) -> tide::ResponseBuilder
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
Call: FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
{
self.content_type(tide::http::mime::HTML).render(call)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/warp03/src/main.rs
Expand Up @@ -73,7 +73,7 @@ async fn arg_handler(what: String) -> Result<Response> {

/// This method can be used as a "template tag", i.e. a method that
/// can be called directly from a template.
fn footer(out: &mut dyn Write) -> io::Result<()> {
fn footer(out: &mut impl Write) -> io::Result<()> {
templates::footer(
out,
&[
Expand Down
2 changes: 1 addition & 1 deletion src/template.rs
Expand Up @@ -40,7 +40,7 @@ impl Template {
writeln!(
out,
"\n\
pub fn {name}<W>(mut _ructe_out_: &mut W{args}) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write {{\n\
pub fn {name}<W>(_ructe_out_: &mut W{args}) -> io::Result<()> where W: Write {{\n\
{body}\
Ok(())\n\
}}",
Expand Down
4 changes: 2 additions & 2 deletions src/templateexpression.rs
Expand Up @@ -82,7 +82,7 @@ impl TemplateExpression {
format!("_ructe_out_.write_all({:?}.as_bytes())?;\n", text)
}
TemplateExpression::Expression { ref expr } => {
format!("{}.to_html(&mut _ructe_out_)?;\n", expr)
format!("{}.to_html(_ructe_out_)?;\n", expr)
}
TemplateExpression::ForLoop {
ref name,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl TemplateExpression {
),
TemplateExpression::CallTemplate { ref name, ref args } => {
format!(
"{}(&mut _ructe_out_{})?;\n",
"{}(_ructe_out_{})?;\n",
name,
args.iter().format_with("", |arg, f| f(&format_args!(
", {}",
Expand Down