|
| 1 | +use std::collections::BTreeSet; |
| 2 | + |
| 3 | +use indexmap::IndexMap; |
| 4 | + |
| 5 | +use crate::{config::Configuration, goal::TeamAsk, team::TeamName, util}; |
| 6 | + |
| 7 | +/// Format a set of team asks into a table, with asks separated by team and grouped by kind. |
| 8 | +/// |
| 9 | +/// output looks like |
| 10 | +/// |
| 11 | +/// ```ignore |
| 12 | +/// | Goal | [DMS](#discussion-and-moral-support) | [SR](#standard reviews) | |
| 13 | +/// | :--- | :-- | :-- | |
| 14 | +/// | Foo | ✅ | ✅ (notes) | |
| 15 | +/// | Bar (Baz) | ✅ | ✅ (\*1) | |
| 16 | +/// |
| 17 | +/// \*1: ... longer notes that would not fit ... |
| 18 | +/// ``` |
| 19 | +pub fn format_team_asks(asks_of_any_team: &[&TeamAsk]) -> anyhow::Result<String> { |
| 20 | + use std::fmt::Write; |
| 21 | + |
| 22 | + const CHECK: &str = "✅"; |
| 23 | + |
| 24 | + /// Arbitrary: max length of text before we insert a footnote |
| 25 | + const FOOTNOTE_LEN: usize = 22; |
| 26 | + |
| 27 | + let mut output = String::new(); |
| 28 | + |
| 29 | + let all_teams: BTreeSet<&TeamName> = asks_of_any_team |
| 30 | + .iter() |
| 31 | + .flat_map(|a| &a.teams) |
| 32 | + .copied() |
| 33 | + .collect(); |
| 34 | + |
| 35 | + // The set of configured team asks |
| 36 | + let config = Configuration::get(); |
| 37 | + |
| 38 | + for team_name in all_teams { |
| 39 | + let asks_of_this_team: Vec<_> = asks_of_any_team |
| 40 | + .iter() |
| 41 | + .filter(|a| a.teams.contains(&team_name)) |
| 42 | + .collect(); |
| 43 | + |
| 44 | + let team_data = team_name.data(); |
| 45 | + write!(output, "\n### {} team\n", team_data.name)?; |
| 46 | + |
| 47 | + // We will accumulate footnotes when we encounter comments that are too long. |
| 48 | + let mut footnotes = vec![]; |
| 49 | + |
| 50 | + // These are things like "discussion and moral support". They are extracted from |
| 51 | + // the configuration. We prune out the ones that do not appear in the asks for a particular team. |
| 52 | + let ask_headings = config |
| 53 | + .team_asks |
| 54 | + .keys() |
| 55 | + .filter(|&ask_kind| { |
| 56 | + asks_of_this_team |
| 57 | + .iter() |
| 58 | + .any(|a| &a.ask_description == ask_kind) |
| 59 | + }) |
| 60 | + .collect::<Vec<_>>(); |
| 61 | + let empty_row = || { |
| 62 | + (0..ask_headings.len()) |
| 63 | + .map(|_| "".to_string()) |
| 64 | + .collect::<Vec<_>>() |
| 65 | + }; |
| 66 | + |
| 67 | + // Collect the asks by goal. The `rows` map goes from goal title to a row with entries |
| 68 | + let mut goal_rows: IndexMap<String, Vec<String>> = IndexMap::default(); |
| 69 | + for ask in &asks_of_this_team { |
| 70 | + let link = format!("{}", ask.link_path.display()); |
| 71 | + |
| 72 | + let goal_title = match &ask.goal_titles[..] { |
| 73 | + [goal_title] => format!("[{goal_title}]({link}#ownership-and-team-asks)"), |
| 74 | + [goal_title, subgoal_title] => { |
| 75 | + format!("[{subgoal_title}]({link}#ownership-and-team-asks) (part of [{goal_title}]({link}))") |
| 76 | + } |
| 77 | + _ => anyhow::bail!( |
| 78 | + "expected either 1 or 2 goal titles, not {:?}", |
| 79 | + ask.goal_titles |
| 80 | + ), |
| 81 | + }; |
| 82 | + |
| 83 | + let row = goal_rows.entry(goal_title).or_insert_with(empty_row); |
| 84 | + |
| 85 | + let index = ask_headings |
| 86 | + .iter() |
| 87 | + .position(|&h| h == &ask.ask_description) |
| 88 | + .unwrap(); |
| 89 | + |
| 90 | + let text = if !ask.notes.is_empty() { |
| 91 | + &ask.notes |
| 92 | + } else { |
| 93 | + CHECK |
| 94 | + }; |
| 95 | + |
| 96 | + let mut maybe_footnote = |text: &str| -> String { |
| 97 | + if text.len() > FOOTNOTE_LEN { |
| 98 | + let footnote_index = footnotes.len() + 1; |
| 99 | + footnotes.push(format!("\\*{footnote_index}: {text} ([from here]({link}))", link = ask.link_path.display())); |
| 100 | + format!("\\*{footnote_index}") |
| 101 | + } else { |
| 102 | + text.to_string() |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + if !row[index].is_empty() { |
| 107 | + row[index] = format!("{} {}", row[index], maybe_footnote(text)); |
| 108 | + } else { |
| 109 | + row[index] = maybe_footnote(text); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Sort the goal rows by name (ignoring case). |
| 114 | + goal_rows.sort_by_cached_key(|ask_names, _ask_rows| { |
| 115 | + ask_names.clone().to_uppercase() |
| 116 | + }); |
| 117 | + |
| 118 | + // Create the table itself. |
| 119 | + let table = { |
| 120 | + let headings = std::iter::once("Goal".to_string()) |
| 121 | + .chain( |
| 122 | + ask_headings |
| 123 | + .iter() |
| 124 | + .map(|&ask_kind| format!( |
| 125 | + "[{team_ask_short}][valid_team_asks]", // HACK: This should not be hardcoded in the code. |
| 126 | + team_ask_short = config.team_asks[ask_kind].short, |
| 127 | + )) |
| 128 | + ) // e.g. "discussion and moral support" |
| 129 | + .collect::<Vec<String>>(); |
| 130 | + |
| 131 | + let rows = goal_rows.into_iter().map(|(goal_title, goal_columns)| { |
| 132 | + std::iter::once(goal_title) |
| 133 | + .chain(goal_columns) |
| 134 | + .collect::<Vec<String>>() |
| 135 | + }); |
| 136 | + |
| 137 | + std::iter::once(headings).chain(rows).collect::<Vec<_>>() |
| 138 | + }; |
| 139 | + |
| 140 | + write!(output, "{}", util::format_table(&table))?; |
| 141 | + |
| 142 | + for footnote in footnotes { |
| 143 | + write!(output, "\n\n{}\n", footnote)?; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + Ok(output) |
| 148 | +} |
0 commit comments