diff --git a/Cargo.lock b/Cargo.lock index ccc395ec..d1de248d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1495,7 +1495,7 @@ dependencies = [ [[package]] name = "rust_team_data" version = "1.0.0" -source = "git+https://github.com/rust-lang/team#3e3704e7655c881533306ef09f077786e47eeda4" +source = "git+https://github.com/rust-lang/team#ae0fe5cb83bbb24c9533c6829f2ca11667e8ab2d" dependencies = [ "indexmap 2.1.0", "serde", diff --git a/src/i18n.rs b/src/i18n.rs index 91610dcf..f2153372 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -107,6 +107,45 @@ impl Default for TeamHelper { } } +enum TeamHelperParam { + /// `{{team-text team name}}` + Name, + + /// `{{team-text team description}}` + Description, + + /// `{{team-text team role (lookup member.roles 0)}}` + Role(String), +} + +impl TeamHelperParam { + fn fluent_id(&self, team_name: &str) -> String { + match self { + TeamHelperParam::Name => format!("governance-team-{team_name}-name"), + TeamHelperParam::Description => format!("governance-team-{team_name}-description"), + TeamHelperParam::Role(role_id) => format!("governance-role-{role_id}"), + } + } + + fn english<'a>(&'a self, team: &'a serde_json::Value) -> &'a str { + match self { + TeamHelperParam::Name => team["website_data"]["name"].as_str().unwrap(), + TeamHelperParam::Description => team["website_data"]["description"].as_str().unwrap(), + TeamHelperParam::Role(role_id) => { + for role in team["roles"].as_array().unwrap() { + if role["id"] == *role_id { + return role["description"].as_str().unwrap(); + } + } + // This should never happen. The `validate_member_roles` test in + // the team repo enforces that `.members.*.roles.*` lines up + // with exactly one `.roles.*.id`. + role_id + } + } + } +} + impl HelperDef for TeamHelper { fn call<'reg: 'rc, 'rc>( &self, @@ -137,6 +176,25 @@ impl HelperDef for TeamHelper { "{{team-text}} takes only identifier parameters", )); }; + + let param = match id.as_str() { + "name" => TeamHelperParam::Name, + "description" => TeamHelperParam::Description, + "role" => { + let Some(role_id) = h.param(2) else { + return Err(RenderError::new( + "{{team-text}} requires a third parameter for the role id", + )); + }; + TeamHelperParam::Role(role_id.value().as_str().unwrap().to_owned()) + } + unrecognized => { + return Err(RenderError::new(format!( + "unrecognized {{{{team-text}}}} param {unrecognized:?}", + ))); + } + }; + let team = rcx .evaluate(context, name) .map_err(|e| RenderError::from_error(&format!("Cannot find team {}", name), e))?; @@ -148,22 +206,20 @@ impl HelperDef for TeamHelper { .expect("Language must be string"); let team_name = team.as_json()["name"].as_str().unwrap(); - let fluent_id = format!("governance-team-{}-{}", team_name, id); - // English uses the team data directly, so that it gets autoupdated if lang == "en-US" { - let english = team.as_json()["website_data"][id].as_str().unwrap(); + let english = param.english(team.as_json()); out.write(english) .map_err(|e| RenderError::from_error("failed to render English team data", e))?; } else if let Some(value) = self.i18n.lookup_no_default_fallback( &lang.parse().expect("language must be valid"), - &fluent_id, + ¶m.fluent_id(team_name), None, ) { out.write(&value) .map_err(|e| RenderError::from_error("failed to render translated team data", e))?; } else { - let english = team.as_json()["website_data"][id].as_str().unwrap(); + let english = param.english(team.as_json()); out.write(english) .map_err(|e| RenderError::from_error("failed to render", e))?; } diff --git a/src/main.rs b/src/main.rs index 9f960997..5be444f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,6 @@ use sass_rs::{compile_file, Options}; use category::Category; use caching::CachedNamedFile; -use handlebars::handlebars_helper; use handlebars_fluent::{loader::Loader, FluentHelper}; use i18n::{create_loader, LocaleInfo, SupportedLocale, TeamHelper, EXPLICIT_LOCALE_INFO}; @@ -492,11 +491,6 @@ async fn rocket() -> _ { engine .handlebars .register_helper("encode-zulip-stream", Box::new(encode_zulip_stream)); - - handlebars_helper!(concat: |x: String, y: String| x + &y); - engine - .handlebars - .register_helper("concat", Box::new(concat)); }); let rust_version = RustVersion::fetch().await.unwrap_or_default(); diff --git a/src/teams.rs b/src/teams.rs index 30ba774d..12d050f9 100644 --- a/src/teams.rs +++ b/src/teams.rs @@ -334,6 +334,7 @@ mod tests { zulip_stream: None, weight: 0, }), + roles: Vec::new(), github: None, discord: vec![], } diff --git a/templates/governance/group-team.html.hbs b/templates/governance/group-team.html.hbs index 257ebed3..5fd32b72 100644 --- a/templates/governance/group-team.html.hbs +++ b/templates/governance/group-team.html.hbs @@ -62,7 +62,7 @@
{{fluent "governance-user-team-leader"}}
{{else}} {{#if member.roles}} -
{{fluent (concat "governance-role-" (lookup member.roles 0))}}
+
{{team-text team role (lookup member.roles 0)}}
{{/if}} {{/if}}