Skip to content

Commit 5d5aac5

Browse files
General refactoring for safety & performance (#22)
1 parent 213cdca commit 5d5aac5

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/lib.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Config {
116116
}
117117

118118
fn get_feature(line: &str) -> Option<&str> {
119-
let re = Regex::new("^\\s*### .*$").unwrap();
119+
let re = Regex::new("^\\s*### .*$").expect("fixed regex always valid");
120120
if re.is_match(line) {
121121
Some(line.trim()[3..].trim())
122122
} else {
@@ -244,14 +244,24 @@ pub fn template(
244244
mode: Mode,
245245
ignore: Vec<&str>,
246246
) -> Result<(), Box<dyn Error>> {
247-
for entry in WalkDir::new(source_dir).into_iter().filter_entry(|entry| {
248-
let ignore_list: Vec<&Path> = ignore.iter().map(|fname| Path::new(fname)).collect();
249-
250-
!ignore_list.contains(&entry.path().strip_prefix(&source_dir).unwrap())
251-
}) {
247+
let ignore_list: Vec<&Path> = ignore.iter().map(|fname| Path::new(fname)).collect();
248+
249+
let filtered_paths = WalkDir::new(source_dir).into_iter().filter_entry(|entry| {
250+
!ignore_list.contains(
251+
&entry
252+
.path()
253+
.strip_prefix(&source_dir)
254+
.expect("entry should always have source_dir prefix"),
255+
)
256+
});
257+
258+
for entry in filtered_paths {
252259
let source_file = entry?;
253260
let source_file = source_file.path();
254-
let dest_file = source_file.to_str().unwrap().replace(source_dir, dest_dir);
261+
let dest_file = source_file
262+
.to_str()
263+
.expect("expected UTF-8 path")
264+
.replace(source_dir, dest_dir);
255265
let dest_file = Path::new(&dest_file);
256266

257267
match mode {

0 commit comments

Comments
 (0)