Skip to content

Commit

Permalink
PR #110: Allow lifetime annotations in type_expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Feb 6, 2022
2 parents 42ac2f8 + 9b2c730 commit 674cbf5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,13 +11,16 @@ project adheres to

* Breaking change: The generated template functions have a simpler
signature.
* 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.
* Update optional rsass to 0.23.0.
* 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

Expand Down
18 changes: 18 additions & 0 deletions examples/simple/src/main.rs
Expand Up @@ -320,3 +320,21 @@ 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 <p>foo</p>\n\n <p>bar</p>\n\n",
);
}

/// [Issue #106](https://github.com/kaj/ructe/issues/106)
#[test]
fn lifetimes2() {
assert_eq!(
r2s(|o| with_lifetime2(o, &["foo", "bar"])),
"\n <p>foo</p>\n\n <p>bar</p>\n\n",
);
}
6 changes: 6 additions & 0 deletions 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 {
<p>@x</p>
}
6 changes: 6 additions & 0 deletions 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 {
<p>@x</p>
}
34 changes: 29 additions & 5 deletions src/template.rs
Expand Up @@ -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<String>,
type_args: String,
args: Vec<String>,
body: Vec<TemplateExpression>,
}
Expand All @@ -40,11 +41,14 @@ impl Template {
writeln!(
out,
"\n\
pub fn {name}<W>(_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!(
", {}",
Expand All @@ -70,10 +74,28 @@ pub fn template(input: &[u8]) -> PResult<Template> {
),
String::from,
)),
context("expected '@('...')' template declaration.", tag("@")),
opt(delimited(
terminated(tag("<"), multispace0),
context(
"expected type argument or '>'",
map_res(
recognize(separated_list1(
terminated(tag(","), multispace0),
context(
"expected lifetime declaration",
preceded(tag("'"), rust_name),
),
)),
input_to_str,
),
),
tag(">"),
)),
delimited(
context(
"expected '@('...')' template declaration.",
terminated(tag("@("), multispace0),
"expected '('...')' template arguments declaration.",
terminated(tag("("), multispace0),
),
separated_list0(
terminated(tag(","), multispace0),
Expand All @@ -95,8 +117,9 @@ pub fn template(input: &[u8]) -> PResult<Template> {
end_of_file,
),
)),
|((), preamble, args, body)| Template {
|((), preamble, _, type_args, args, body)| Template {
preamble,
type_args: type_args.map(String::from).unwrap_or_default(),
args,
body: body.0,
},
Expand Down Expand Up @@ -131,6 +154,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(""))),
Expand Down

0 comments on commit 674cbf5

Please sign in to comment.