Skip to content

Commit

Permalink
templates: add .email_domain() method to signatures TODO: test
Browse files Browse the repository at this point in the history
This is slightly less pretty than making an Email type,
but I think is much easier.

I would use instead of the last argument of:

```
  concat(
    label("email", signature.username()),
    "@",
    label("email",
      signature.email().remove_prefix(
        signature.username() ++ "@"
      )
    )
  )
```
  • Loading branch information
ilyagr committed Oct 22, 2024
1 parent a493913 commit 23faa20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ The following methods are defined.
* `.name() -> String`
* `.email() -> String`
* `.username() -> String`
* `.email_domain() -> String`: The part of the email after the `@`.
* `.timestamp() -> Timestamp`

### SizeHint type
Expand Down

0 comments on commit 23faa20

Please sign in to comment.