diff --git a/CHANGELOG.md b/CHANGELOG.md index fec9be3750..b63c25a110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). * Timestamp objects in templates now have `after(date) -> Boolean` and `before(date) -> Boolean` methods for comparing timestamps to other dates. +* Signature objects in templates now have a `email_domain() -> String` method. + * New template functions `pad_start()`, `pad_end()`, `truncate_start()`, and `truncate_end()` are added. diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index bd7c2ea4f0..462d800f02 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -787,6 +787,17 @@ fn builtin_signature_methods<'a, L: TemplateLanguage<'a> + ?Sized>( Ok(L::wrap_string(out_property)) }, ); + map.insert( + "email_domain", + |_language, _diagnostics, _build_ctx, self_property, function| { + function.expect_no_arguments()?; + let out_property = self_property.map(|signature| { + let (_, domain) = text_util::split_email(&signature.email); + domain.unwrap_or_default().to_owned() + }); + Ok(L::wrap_string(out_property)) + }, + ); map.insert( "timestamp", |_language, _diagnostics, _build_ctx, self_property, function| { @@ -2156,6 +2167,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"Test User"); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user@example.com"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"example.com"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature( @@ -2167,6 +2179,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"Another Test User"); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user@example.com"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"example.com"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature( @@ -2178,6 +2191,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"Test User"); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user@invalid@example.com"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"invalid@example.com"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature("Test User", "test.user"))) @@ -2185,6 +2199,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User "); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @""); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature( @@ -2195,6 +2210,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User "); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user+tag@example.com"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user+tag"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"example.com"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature("Test User", "x@y"))) @@ -2202,6 +2218,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User "); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"x@y"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"x"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"y"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature("", "test.user@example.com"))) @@ -2210,6 +2227,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @""); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user@example.com"); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user"); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"example.com"); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature("Test User", ""))) @@ -2218,6 +2236,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"Test User"); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @""); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @""); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @""); env.add_keyword("author", || { L::wrap_signature(Literal(new_signature("", ""))) @@ -2226,6 +2245,7 @@ mod tests { insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @""); insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @""); insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @""); + insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @""); } #[test] diff --git a/docs/templates.md b/docs/templates.md index 76862c0836..ed17d7ef1c 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -210,6 +210,8 @@ The following methods are defined. * `.name() -> String` * `.email() -> String` * `.username() -> String` +* `.email_domain() -> String`: The part of the email after the `@`. May be empty + and may contain `@` for abnormal emails. * `.timestamp() -> Timestamp` ### SizeHint type