From 9b2c730c6502369f959fc1f60d88d1cb0c4481c6 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Sun, 6 Feb 2022 14:39:21 +0100 Subject: [PATCH] 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