Skip to content

Commit f708aa9

Browse files
committed
from_team_file_path for reuse
1 parent 9d68536 commit f708aa9

File tree

6 files changed

+58
-39
lines changed

6 files changed

+58
-39
lines changed

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::Deserialize;
22

3-
#[derive(Deserialize, Debug)]
3+
#[derive(Deserialize, Debug, Clone)]
44
pub struct Config {
55
pub owned_globs: Vec<String>,
66

src/ownership.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use mapper::{OwnerMatcher, Source, TeamName};
44
use std::{
55
error::Error,
66
fmt::{self, Display},
7-
fs,
87
path::{Path, PathBuf},
98
sync::Arc,
109
};
@@ -180,9 +179,7 @@ impl Ownership {
180179
}
181180

182181
pub fn fast_team_name_from_file_path(file_path: &str, code_owners_file_path: &PathBuf) -> Result<Option<String>, Box<dyn Error>> {
183-
let code_owners = fs::read_to_string(code_owners_file_path)?;
184-
let team_name = parser::team_name_from_file_path(Path::new(file_path), &code_owners);
185-
Ok(team_name)
182+
parser::team_name_from_file_path(Path::new(file_path), code_owners_file_path)
186183
}
187184

188185
#[cfg(test)]

src/ownership/parser.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
use crate::ownership::{FileGenerator, TeamOwnership};
22
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()
916
} else {
10-
format!("/{}", file_path.to_str()?)
17+
format!("/{}", file_path_str)
1118
};
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"))?;
1426
if glob_match(glob, &slash_prefixed) {
15-
return Some(team_name.to_string());
27+
return Ok(Some(team_name.to_string()));
1628
}
1729
}
1830

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)
2038
}
2139

2240
fn stripped_lines_by_priority(codeowners_file: &str) -> Vec<String> {

src/project.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::fmt;
22
use std::{
33
collections::HashMap,
44
fmt::Display,
5+
fs::File,
56
path::{Path, PathBuf},
67
};
78

@@ -40,6 +41,21 @@ pub struct Team {
4041
pub avoid_ownership: bool,
4142
}
4243

44+
impl Team {
45+
pub fn from_team_file_path(absolute_path: PathBuf) -> Result<Self, Error> {
46+
let file = File::open(&absolute_path).change_context(Error::Io)?;
47+
let deserializer: deserializers::Team = serde_yaml::from_reader(file).change_context(Error::SerdeYaml)?;
48+
Ok(Self {
49+
path: absolute_path.to_owned(),
50+
name: deserializer.name,
51+
github_team: deserializer.github.team,
52+
owned_globs: deserializer.owned_globs,
53+
owned_gems: deserializer.ruby.map(|ruby| ruby.owned_gems).unwrap_or_default(),
54+
avoid_ownership: deserializer.github.do_not_add_to_codeowners_file,
55+
})
56+
}
57+
}
58+
4359
#[derive(Clone, Debug)]
4460
pub struct Package {
4561
pub path: PathBuf,

src/project_builder.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,8 @@ impl<'a> ProjectBuilder<'a> {
150150
});
151151
}
152152
EntryType::TeamFile(absolute_path, _relative_path) => {
153-
let file = File::open(&absolute_path).unwrap();
154-
let deserializer: deserializers::Team = serde_yaml::from_reader(file).unwrap();
155-
team_files.push(Team {
156-
path: absolute_path.to_owned(),
157-
name: deserializer.name,
158-
github_team: deserializer.github.team,
159-
owned_globs: deserializer.owned_globs,
160-
owned_gems: deserializer.ruby.map(|ruby| ruby.owned_gems).unwrap_or_default(),
161-
avoid_ownership: deserializer.github.do_not_add_to_codeowners_file,
162-
});
153+
let team = Team::from_team_file_path(absolute_path).unwrap();
154+
team_files.push(team);
163155
}
164156
EntryType::NullEntry() => {}
165157
}
@@ -177,8 +169,7 @@ impl<'a> ProjectBuilder<'a> {
177169
acc
178170
},
179171
);
180-
let teams_by_name = teams.iter()
181-
.map(|team| (team.name.clone(), team.clone())).collect();
172+
let teams_by_name = teams.iter().map(|team| (team.name.clone(), team.clone())).collect();
182173

183174
Ok(Project {
184175
base_path: self.base_path.to_owned(),

src/runner.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub struct Runner {
3232
}
3333

3434
pub fn for_file(run_config: &RunConfig, file_path: &str, verbose: bool) -> RunResult {
35-
dbg!(verbose);
3635
if verbose {
3736
run_with_runner(run_config, |runner| runner.for_file(file_path))
3837
} else {
@@ -114,19 +113,17 @@ impl fmt::Display for Error {
114113
}
115114
}
116115

116+
fn config_from_path(path: &PathBuf) -> Result<Config, Error> {
117+
let config_file = File::open(path)
118+
.change_context(Error::Io(format!("Can't open config file: {}", &path.to_string_lossy())))
119+
.attach_printable(format!("Can't open config file: {}", &path.to_string_lossy()))?;
120+
121+
serde_yaml::from_reader(config_file).change_context(Error::Io(format!("Can't parse config file: {}", &path.to_string_lossy())))
122+
}
117123
impl Runner {
118124
pub fn new(run_config: &RunConfig) -> Result<Self, Error> {
119-
let config_file = File::open(&run_config.config_path)
120-
.change_context(Error::Io(format!(
121-
"Can't open config file: {}",
122-
&run_config.config_path.to_string_lossy()
123-
)))
124-
.attach_printable(format!("Can't open config file: {}", &run_config.config_path.to_string_lossy()))?;
125+
let config = config_from_path(&run_config.config_path)?;
125126

126-
let config: Config = serde_yaml::from_reader(config_file).change_context(Error::Io(format!(
127-
"Can't parse config file: {}",
128-
&run_config.config_path.to_string_lossy()
129-
)))?;
130127
let cache: Cache = if run_config.no_cache {
131128
NoopCache::default().into()
132129
} else {

0 commit comments

Comments
 (0)