Skip to content

Commit bf42ec6

Browse files
committed
lazy static regex
1 parent 3bffb27 commit bf42ec6

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fast-glob = "0.4.0"
1414
ignore = "0.4.23"
1515
itertools = "0.13.0"
1616
jwalk = "0.8.1"
17+
lazy_static = "1.5.0"
1718
path-clean = "1.0.1"
1819
rayon = "1.10.0"
1920
regex = "1.11.1"

src/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct VendoredGem {
2323
pub name: String,
2424
}
2525

26-
#[derive(Debug)]
26+
#[derive(Debug, Clone)]
2727
pub struct ProjectFile {
2828
pub owner: Option<String>,
2929
pub path: PathBuf,

src/project_builder.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::{
22
fs::File,
3-
io::BufRead,
43
path::{Path, PathBuf},
54
};
65

76
use error_stack::{Result, ResultExt};
87
use fast_glob::glob_match;
98
use ignore::WalkBuilder;
109
use jwalk::{DirEntry, WalkDir};
10+
use lazy_static::lazy_static;
1111
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1212
use regex::Regex;
13-
use tracing::{info, instrument};
13+
use tracing::instrument;
1414

1515
use crate::{
1616
config::Config,
@@ -350,41 +350,35 @@ fn javascript_package_owner(path: &Path) -> Result<Option<String>, Error> {
350350
Ok(deserializer.metadata.and_then(|metadata| metadata.owner))
351351
}
352352

353+
lazy_static! {
354+
static ref TEAM_REGEX: Regex = Regex::new(r#"^(?:#|//) @team (.*)$"#).expect("error compiling regular expression");
355+
}
356+
353357
#[instrument(level = "debug", skip_all)]
354358
fn owned_files(owned_file_paths: Vec<PathBuf>) -> Vec<ProjectFile> {
355-
let regexp = Regex::new(r#"^(?:#|//) @team (.*)$"#).expect("error compiling regular expression");
356-
357-
info!("opening files to read ownership annotation");
358-
359-
owned_file_paths
360-
.into_par_iter()
361-
.map(|path| {
362-
let file = File::open(&path).unwrap_or_else(|_| panic!("Couldn't open {}", path.to_string_lossy()));
363-
let first_line = std::io::BufReader::new(file).lines().next().transpose();
364-
let first_line = first_line.expect("error reading first line");
359+
owned_file_paths.into_par_iter().map(build_project_file).collect()
360+
}
365361

366-
if first_line.is_none() {
367-
return ProjectFile { path, owner: None };
368-
}
362+
fn build_project_file(path: PathBuf) -> ProjectFile {
363+
let content = match std::fs::read_to_string(&path) {
364+
Ok(content) => content,
365+
Err(_) => return ProjectFile { path, owner: None },
366+
};
369367

370-
if let Some(first_line) = first_line {
371-
let capture = regexp.captures(&first_line);
368+
let first_line = content.lines().next();
372369

373-
if let Some(capture) = capture {
374-
let first_capture = capture.get(1);
370+
// Early return if empty file
371+
let Some(first_line) = first_line else {
372+
return ProjectFile { path, owner: None };
373+
};
375374

376-
if let Some(first_capture) = first_capture {
377-
return ProjectFile {
378-
path,
379-
owner: Some(first_capture.as_str().to_string()),
380-
};
381-
}
382-
}
383-
}
375+
// Use TEAM_REGEX instead of regexp
376+
let owner = TEAM_REGEX
377+
.captures(first_line)
378+
.and_then(|cap| cap.get(1))
379+
.map(|m| m.as_str().to_string());
384380

385-
ProjectFile { path, owner: None }
386-
})
387-
.collect()
381+
ProjectFile { path, owner }
388382
}
389383

390384
#[cfg(test)]

0 commit comments

Comments
 (0)