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

templates: add .email_domain() method to Signatures #4687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
20 changes: 20 additions & 0 deletions cli/src/template_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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()"#), @"[email protected]");
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(
Expand All @@ -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()"#), @"[email protected]");
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(
Expand All @@ -2178,13 +2191,15 @@ mod tests {
insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"Test User");
insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"test.user@[email protected]");
insta::assert_snapshot!(env.render_ok(r#"author.username()"#), @"test.user");
insta::assert_snapshot!(env.render_ok(r#"author.email_domain()"#), @"[email protected]");

env.add_keyword("author", || {
L::wrap_signature(Literal(new_signature("Test User", "test.user")))
});
insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User <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(
Expand All @@ -2195,13 +2210,15 @@ mod tests {
insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User <[email protected]>");
insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"[email protected]");
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")))
});
insta::assert_snapshot!(env.render_ok(r#"author"#), @"Test User <x@y>");
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("", "[email protected]")))
Expand All @@ -2210,6 +2227,7 @@ mod tests {
insta::assert_snapshot!(env.render_ok(r#"author.name()"#), @"");
insta::assert_snapshot!(env.render_ok(r#"author.email()"#), @"[email protected]");
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", "")))
Expand All @@ -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("", "")))
Expand All @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down