From afa7a504ecd7af62ad59107b9910050c57c757b6 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Sun, 6 Feb 2022 13:32:37 +0100 Subject: [PATCH 1/2] Allow lifetime annotations in type_expression. Fixes #106. --- CHANGELOG.md | 3 +++ examples/simple/src/main.rs | 9 +++++++++ examples/simple/templates/with_lifetime.rs.html | 6 ++++++ src/template.rs | 1 + 4 files changed, 19 insertions(+) create mode 100644 examples/simple/templates/with_lifetime.rs.html diff --git a/CHANGELOG.md b/CHANGELOG.md index fb5cf5e..1192e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ project adheres to * Breaking change: The generated template functions have a simpler signature. +* Allow litetimes in template argument types. Issue #106. * 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. @@ -18,6 +19,8 @@ project adheres to * Update env_logger to 0.9 and gotham to 0.7.1 in examples * Dropped support for warp 0.2 (the warp02 feature and example). +Thanks to @JojiiOfficial for reporting #106. + ## Release 0.13.4 - 2021-06-25 diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 8cc1e70..943edac 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -320,3 +320,12 @@ fn test_issue_68() { "Hello!\n\nThe 0 number.\n\nThe 1 number.\n\nThe 2 number.\n\nGood bye!\n", ); } + +/// [Issue #106](https://github.com/kaj/ructe/issues/106) +#[test] +fn lifetimes() { + assert_eq!( + r2s(|o| with_lifetime(o, &["foo", "bar"])), + "\n

foo

\n\n

bar

\n\n", + ); +} diff --git a/examples/simple/templates/with_lifetime.rs.html b/examples/simple/templates/with_lifetime.rs.html new file mode 100644 index 0000000..b727d5f --- /dev/null +++ b/examples/simple/templates/with_lifetime.rs.html @@ -0,0 +1,6 @@ +@* There can be lifetime annotations in argument types. *@ +@(arg: &[&'static str]) + +@for x in arg { +

@x

+} diff --git a/src/template.rs b/src/template.rs index 01a6bbc..f2afdaf 100644 --- a/src/template.rs +++ b/src/template.rs @@ -131,6 +131,7 @@ fn type_expression(input: &[u8]) -> PResult<()> { map( tuple(( alt((tag("&"), tag(""))), + opt(delimited(spacelike, tag("'"), rust_name)), delimited( spacelike, alt((tag("impl"), tag("dyn"), tag(""))), From 9b2c730c6502369f959fc1f60d88d1cb0c4481c6 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Sun, 6 Feb 2022 14:39:21 +0100 Subject: [PATCH 2/2] Allow declaring named lifetimes. --- CHANGELOG.md | 2 +- examples/simple/src/main.rs | 9 +++++ .../simple/templates/with_lifetime2.rs.html | 6 ++++ src/template.rs | 33 ++++++++++++++++--- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 examples/simple/templates/with_lifetime2.rs.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 1192e40..a9f235a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ project adheres to * Breaking change: The generated template functions have a simpler signature. -* Allow litetimes in template argument types. Issue #106. +* Allow litetimes in template argument types. Issue #106, PR #110. * 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. diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 943edac..67c69ce 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -329,3 +329,12 @@ fn lifetimes() { "\n

foo

\n\n

bar

\n\n", ); } + +/// [Issue #106](https://github.com/kaj/ructe/issues/106) +#[test] +fn lifetimes2() { + assert_eq!( + r2s(|o| with_lifetime2(o, &["foo", "bar"])), + "\n

foo

\n\n

bar

\n\n", + ); +} diff --git a/examples/simple/templates/with_lifetime2.rs.html b/examples/simple/templates/with_lifetime2.rs.html new file mode 100644 index 0000000..cda2784 --- /dev/null +++ b/examples/simple/templates/with_lifetime2.rs.html @@ -0,0 +1,6 @@ +@* There can be lifetime annotations in argument types. *@ +@<'a>(arg: &[&'a str]) + +@for x in arg { +

@x

+} diff --git a/src/template.rs b/src/template.rs index f2afdaf..d4f5a6a 100644 --- a/src/template.rs +++ b/src/template.rs @@ -9,13 +9,14 @@ use nom::bytes::complete::tag; use nom::character::complete::{char, multispace0}; use nom::combinator::{map, map_res, opt, recognize}; use nom::error::context; -use nom::multi::{many0, many_till, separated_list0}; +use nom::multi::{many0, many_till, separated_list0, separated_list1}; use nom::sequence::{delimited, preceded, terminated, tuple}; use std::io::{self, Write}; #[derive(Debug, PartialEq, Eq)] pub struct Template { preamble: Vec, + type_args: String, args: Vec, body: Vec, } @@ -40,11 +41,14 @@ impl Template { writeln!( out, "\n\ - pub fn {name}(_ructe_out_: &mut W{args}) -> io::Result<()> where W: Write {{\n\ + pub fn {name}<{ta}{ta_sep}W>(_ructe_out_: &mut W{args}) -> io::Result<()>\n\ + where W: Write {{\n\ {body}\ Ok(())\n\ }}", name = name, + ta = self.type_args, + ta_sep = if self.type_args.is_empty() { "" } else { ", " }, args = self.args.iter().format_with("", |arg, f| f(&format_args!( ", {}", @@ -70,10 +74,28 @@ pub fn template(input: &[u8]) -> PResult