Skip to content

Commit

Permalink
Fix id_by_filename_format unwrap None captures
Browse files Browse the repository at this point in the history
Replace `id_by_filename_format` and `date_by_filename_format` with `dir_entry_to_id_date`
  • Loading branch information
NRngnl committed Dec 24, 2022
1 parent 859b3f3 commit ff89f70
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/message/saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ impl<'b, C: SHNClient> Saver<'b, C> {
}

fn id_dates(&self, dir_buf: &PathBuf) -> Vec<IdDate> {
WalkDir::new(dir_buf)
.sort_by(move |a, b| {
id_by_filename_format(&a).cmp(&id_by_filename_format(&b))
})
let mut result = WalkDir::new(dir_buf)
.into_iter()
.filter(|r| !r.as_ref().unwrap().path().is_dir())
.map(|r| {
let dir_entry = r.unwrap();
IdDate { id: id_by_filename_format(&dir_entry), date: date_by_filename_format(&dir_entry) }
dir_entry_to_id_date(&dir_entry)
})
.collect::<Vec<_>>()
.flatten()
.collect::<Vec<_>>();
result.sort_by(|a, b| a.id.cmp(&b.id));
result
}

fn latest_date(&self, id_dates: &Vec<IdDate>) -> Result<String> {
Expand Down Expand Up @@ -214,14 +214,19 @@ struct IdDate {
date: String,
}

fn id_by_filename_format(filename: &DirEntry) -> u32 {
let re = Regex::new(r"(?x)(?P<id>\d+)_*").unwrap();
let caps = &re.captures(filename.file_name().to_str().unwrap()).unwrap();
caps["id"].parse::<u32>().unwrap()
}
fn dir_entry_to_id_date(filename: &DirEntry) -> Option<IdDate> {
let re = Regex::new(r"(?x)(?P<id>\d+)_\d_(?P<date>\d+)").unwrap();
let caps = &re.captures(filename.file_name().to_str().unwrap());

fn date_by_filename_format(filename: &DirEntry) -> String {
let re = Regex::new(r"(?x)\d+_\d_(?P<date>\d+)").unwrap();
let caps = &re.captures(filename.file_name().to_str().unwrap()).unwrap();
caps["date"].to_string()
match caps {
None => {
None
}
Some(cap) => {
Some(IdDate {
id: cap["id"].parse::<u32>().unwrap(),
date: cap["date"].to_string()
})
}
}
}

0 comments on commit ff89f70

Please sign in to comment.