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

Add basic handling for generics in derive macro #41

Merged
merged 2 commits into from
Oct 20, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ramhorns-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn content_derive(input: TokenStream) -> TokenStream {

let name = &item.ident;
let generics = &item.generics;
let type_params = item.generics.type_params();
let unit_fields = UnitFields::new();

let mut errors = Vec::new();
Expand Down Expand Up @@ -222,9 +223,12 @@ pub fn content_derive(input: TokenStream) -> TokenStream {
let flatten = &*flatten;
let fields = fields.iter().map(|Field { field, .. }| field);

let where_clause = type_params.map(|param| quote!(#param: ::ramhorns::Content)).collect::<Vec<_>>();
let where_clause = if where_clause.len() > 0 { quote!(where #(#where_clause),*) } else { quote!() };

// FIXME: decouple lifetimes from actual generics with trait boundaries
let tokens = quote! {
impl#generics ramhorns::Content for #name#generics {
impl#generics ramhorns::Content for #name#generics #where_clause {
#[inline]
fn capacity_hint(&self, tpl: &ramhorns::Template) -> usize {
tpl.capacity_hint() #( + self.#fields.capacity_hint(tpl) )*
Expand Down
85 changes: 81 additions & 4 deletions tests/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,80 @@ fn can_render_lists_from_vecs() {
);
}

#[test]
fn can_render_nested_generic_types() {
#[derive(Content)]
struct Article {
title: String,
body: String,
}

#[derive(Content)]
struct Page<T> {
contents: T,
last_updated: String,
}

let tpl = Template::new(
"\
{{#contents}}\
<h1>{{title}}</h1>\
<article>{{body}}</article>\
{{/contents}}\
<p>{{last_updated}}</p>",
)
.unwrap();

let html = tpl.render(&Page {
last_updated: "yesterday".into(),
contents: Article {
title: "Jam".into(),
body: "This is an article body".into(),
},
});

assert_eq!(
html,
"<h1>Jam</h1><article>This is an article body</article><p>yesterday</p>"
)
}

#[test]
fn can_render_flattened_generic_types() {
#[derive(Content)]
struct Article {
title: String,
body: String,
}

#[derive(Content)]
struct Page<T> {
#[ramhorns(flatten)]
contents: T,
last_updated: String,
}

let tpl = Template::new(
"<h1>{{title}}</h1>\
<article>{{body}}</article>\
<p>{{last_updated}}</p>",
)
.unwrap();

let html = tpl.render(&Page {
last_updated: "yesterday".into(),
contents: Article {
title: "Jam".into(),
body: "This is an article body".into(),
},
});

assert_eq!(
html,
"<h1>Jam</h1><article>This is an article body</article><p>yesterday</p>"
)
}

#[test]
fn can_render_markdown() {
#[derive(Content)]
Expand Down Expand Up @@ -554,13 +628,13 @@ fn struct_with_many_sections() {
}

let tpl = Template::new("<h1>{{#d}}{{#0}}{{#c}}{{0}}{{/c}}{{/0}}{{/d}} world!</h1>").unwrap();

let rendered = tpl.render(&Page {
a: A(1),
b: B(2.0),
c: C("Hello"),
d: D(true),
other: &[]
other: &[],
});

assert_eq!(rendered, "<h1>Hello world!</h1>");
Expand Down Expand Up @@ -606,10 +680,13 @@ fn derive_flatten() {
title: "This is the title",
child: Child {
body: "This is the body",
}
},
});

assert_eq!(html, "<h1>This is the title</h1><head>This is the body</head>");
assert_eq!(
html,
"<h1>This is the title</h1><head>This is the body</head>"
);
}

#[test]
Expand Down