|
1 |
| -use crate::ownership::{FileGenerator, TeamOwnership}; |
| 1 | +use crate::{ |
| 2 | + ownership::{FileGenerator, TeamOwnership}, |
| 3 | + project::Team, |
| 4 | +}; |
2 | 5 | use fast_glob::glob_match;
|
| 6 | +use memoize::memoize; |
3 | 7 | use std::{
|
| 8 | + collections::HashMap, |
4 | 9 | error::Error,
|
5 | 10 | fs,
|
6 | 11 | io::Error as IoError,
|
7 | 12 | path::{Path, PathBuf},
|
8 | 13 | };
|
9 | 14 |
|
10 |
| -pub fn team_name_from_file_path(file_path: &Path, codeowners_file_path: &PathBuf) -> Result<Option<String>, Box<dyn Error>> { |
11 |
| - let file_path_str = file_path |
12 |
| - .to_str() |
13 |
| - .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?; |
14 |
| - let slash_prefixed = if file_path_str.starts_with("/") { |
15 |
| - file_path_str.to_string() |
16 |
| - } else { |
17 |
| - format!("/{}", file_path_str) |
18 |
| - }; |
19 |
| - |
20 |
| - let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(codeowners_file_path)?; |
21 |
| - |
22 |
| - for line in codeowners_lines_in_priorty { |
23 |
| - let (glob, team_name) = line |
24 |
| - .split_once(' ') |
25 |
| - .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))?; |
26 |
| - if glob_match(glob, &slash_prefixed) { |
27 |
| - return Ok(Some(team_name.to_string())); |
| 15 | +pub struct Parser { |
| 16 | + pub project_root: PathBuf, |
| 17 | + pub codeowners_file_path: PathBuf, |
| 18 | +} |
| 19 | + |
| 20 | +impl Parser { |
| 21 | + pub fn team_from_file_path(&self, file_path: &Path) -> Result<Option<Team>, Box<dyn Error>> { |
| 22 | + let file_path_str = file_path |
| 23 | + .to_str() |
| 24 | + .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid file path"))?; |
| 25 | + let slash_prefixed = if file_path_str.starts_with("/") { |
| 26 | + file_path_str.to_string() |
| 27 | + } else { |
| 28 | + format!("/{}", file_path_str) |
| 29 | + }; |
| 30 | + |
| 31 | + let codeowners_lines_in_priorty = build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned()); |
| 32 | + for line in codeowners_lines_in_priorty { |
| 33 | + let (glob, team_name) = line |
| 34 | + .split_once(' ') |
| 35 | + .ok_or(IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))?; |
| 36 | + if glob_match(glob, &slash_prefixed) { |
| 37 | + let tbn = teams_by_name(self.absolute_team_files_glob()); |
| 38 | + let team: Option<Team> = tbn.get(team_name.to_string().as_str()).cloned(); |
| 39 | + return Ok(team); |
| 40 | + } |
28 | 41 | }
|
| 42 | + |
| 43 | + Ok(None) |
| 44 | + } |
| 45 | + |
| 46 | + fn absolute_team_files_glob(&self) -> String { |
| 47 | + self.project_root |
| 48 | + .join(self.codeowners_file_path.to_string_lossy().into_owned()) |
| 49 | + .to_string_lossy() |
| 50 | + .into_owned() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[memoize] |
| 55 | +fn teams_by_name(team_file_glob: String) -> HashMap<String, Team> { |
| 56 | + let mut teams = HashMap::new(); |
| 57 | + |
| 58 | + // Get all files matching the glob pattern |
| 59 | + let paths = glob::glob(&team_file_glob) |
| 60 | + .expect("Failed to read glob pattern") |
| 61 | + .filter_map(Result::ok); |
| 62 | + |
| 63 | + // Process each matching file |
| 64 | + for path in paths { |
| 65 | + let team = Team::from_team_file_path(path).unwrap(); |
| 66 | + teams.insert(team.name.clone(), team); |
29 | 67 | }
|
30 | 68 |
|
31 |
| - Ok(None) |
| 69 | + teams |
32 | 70 | }
|
33 | 71 |
|
34 |
| -fn build_codeowners_lines_in_priority(codeowners_file_path: &PathBuf) -> Result<Vec<String>, Box<dyn Error>> { |
35 |
| - let codeowners_file = fs::read_to_string(codeowners_file_path)?; |
36 |
| - let stripped_lines = stripped_lines_by_priority(&codeowners_file); |
37 |
| - Ok(stripped_lines) |
| 72 | +#[memoize] |
| 73 | +fn build_codeowners_lines_in_priority(codeowners_file_path: String) -> Vec<String> { |
| 74 | + let codeowners_file = fs::read_to_string(codeowners_file_path).unwrap(); |
| 75 | + stripped_lines_by_priority(&codeowners_file) |
38 | 76 | }
|
39 | 77 |
|
40 | 78 | fn stripped_lines_by_priority(codeowners_file: &str) -> Vec<String> {
|
|
0 commit comments