From da0b6ead0e75082bfffa24f8529d1f83961ba45c Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Fri, 14 Jan 2022 15:32:53 +0100 Subject: Parse tuple expressions Askama understands how to destructure tuples in let and match statements, but it does not understand how to build a tuple. This PR fixes this shortcoming. --- testing/tests/tuple.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 testing/tests/tuple.rs (limited to 'testing/tests') diff --git a/testing/tests/tuple.rs b/testing/tests/tuple.rs new file mode 100644 index 0000000..37d313c --- /dev/null +++ b/testing/tests/tuple.rs @@ -0,0 +1,82 @@ +use askama::Template; + +struct Post { + id: u32, +} + +struct Client<'a> { + can_post_ids: &'a [u32], + can_update_ids: &'a [u32], +} + +impl Client<'_> { + fn can_post(&self, post: &Post) -> bool { + self.can_post_ids.contains(&post.id) + } + + fn can_update(&self, post: &Post) -> bool { + self.can_update_ids.contains(&post.id) + } +} + +#[derive(Template)] +#[template( + source = r#" +{%- match (client.can_post(post), client.can_update(post)) -%} + {%- when (false, false) -%} + No! + {%- when (can_post, can_update) -%} + +{%- endmatch -%} +"#, + ext = "txt" +)] +struct TupleTemplate<'a> { + client: &'a Client<'a>, + post: &'a Post, +} + +#[test] +fn test_tuple() { + let template = TupleTemplate { + client: &Client { + can_post_ids: &[1, 2], + can_update_ids: &[2, 3], + }, + post: &Post { id: 1 }, + }; + assert_eq!(template.render().unwrap(), ""); + + let template = TupleTemplate { + client: &Client { + can_post_ids: &[1, 2], + can_update_ids: &[2, 3], + }, + post: &Post { id: 2 }, + }; + assert_eq!( + template.render().unwrap(), + "" + ); + + let template = TupleTemplate { + client: &Client { + can_post_ids: &[1, 2], + can_update_ids: &[2, 3], + }, + post: &Post { id: 3 }, + }; + assert_eq!(template.render().unwrap(), ""); + + let template = TupleTemplate { + client: &Client { + can_post_ids: &[1, 2], + can_update_ids: &[2, 3], + }, + post: &Post { id: 4 }, + }; + assert_eq!(template.render().unwrap(), "No!"); +} -- cgit