Skip to content

Commit 762f4cb

Browse files
committed
Add auto-completion for team URLs
1 parent 4c0e843 commit 762f4cb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

crates/generate_blog/src/main.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::{error::Error, fmt::Display, fs, path::PathBuf, process::Command};
33
use front_matter::FrontMatter;
44
use inquire::{Confirm, Select, Text, validator::Validation, Autocomplete, CustomUserError};
55
use inquire::autocompletion::Replacement;
6-
use rust_team_data::v1::Teams;
6+
use rust_team_data::v1::{Team, Teams};
7+
8+
const BASE_TEAM_WEBSITE_URL: &str = "https://www.rust-lang.org/governance/teams/";
79

810
fn main() -> Result<(), Box<dyn Error>> {
911
println!("\nHi, thanks for writing a post for the Rust blog!\n");
@@ -91,8 +93,12 @@ fn main() -> Result<(), Box<dyn Error>> {
9193
}
9294

9395
let team = team_prompt.prompt()?;
96+
97+
let prefilled_url = team_data.as_ref().and_then(|teams| find_team_url(teams, &team))
98+
.unwrap_or_else(|| BASE_TEAM_WEBSITE_URL.to_string());
99+
94100
let url = Text::new("At what URL can people find the team?")
95-
.with_initial_value("https://www.rust-lang.org/governance/teams/")
101+
.with_initial_value(&prefilled_url)
96102
.prompt()?;
97103
(Some(team), Some(url))
98104
};
@@ -179,6 +185,24 @@ fn load_teams() -> Result<Teams, String> {
179185
Ok(teams)
180186
}
181187

188+
fn find_team_url(teams: &Teams, team_name: &str) -> Option<String> {
189+
let Some(team) = teams.teams.get(team_name)?;
190+
let top_level_team = find_top_level_team(teams, team);
191+
192+
// E.g. <BASE>compiler#team-miri
193+
Some(format!("{}{}#team-{team_name}", BASE_TEAM_WEBSITE_URL, top_level_team.name))
194+
}
195+
196+
fn find_top_level_team<'a>(teams: &'a Teams, team: &'a Team) -> &'a Team {
197+
if team.top_level.unwrap_or(false) {
198+
return team;
199+
}
200+
if let Some(parent) = team.subteam_of.as_ref().and_then(|t| teams.teams.get(t)) {
201+
return find_top_level_team(teams, parent);
202+
}
203+
team
204+
}
205+
182206
#[derive(Clone)]
183207
struct TeamNames(Vec<String>);
184208

0 commit comments

Comments
 (0)