|
1 | 1 | use crate::ownership::{FileGenerator, TeamOwnership}; |
2 | 2 | use fast_glob::glob_match; |
3 | | -use std::{error::Error, path::Path}; |
4 | | - |
5 | | -pub fn team_name_from_file_path(file_path: &Path, codeowners_file: &str) -> Option<String> { |
6 | | - let stripped_lines = stripped_lines_by_priority(codeowners_file); |
7 | | - let slash_prefixed = if file_path.starts_with("/") { |
8 | | - file_path.to_str().unwrap().to_string() |
| 3 | +use std::{ |
| 4 | + error::Error, |
| 5 | + fs, |
| 6 | + io::Error as IoError, |
| 7 | + path::{Path, PathBuf}, |
| 8 | +}; |
| 9 | + |
| 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() |
9 | 16 | } else { |
10 | | - format!("/{}", file_path.to_str()?) |
| 17 | + format!("/{}", file_path_str) |
11 | 18 | }; |
12 | | - for line in stripped_lines { |
13 | | - let (glob, team_name) = line.split_once(' ')?; |
| 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"))?; |
14 | 26 | if glob_match(glob, &slash_prefixed) { |
15 | | - return Some(team_name.to_string()); |
| 27 | + return Ok(Some(team_name.to_string())); |
16 | 28 | } |
17 | 29 | } |
18 | 30 |
|
19 | | - None |
| 31 | + Ok(None) |
| 32 | +} |
| 33 | + |
| 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) |
20 | 38 | } |
21 | 39 |
|
22 | 40 | fn stripped_lines_by_priority(codeowners_file: &str) -> Vec<String> { |
|
0 commit comments