From fdfbf550144ef69ecc6003760fbb39466ec08637 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 29 Jun 2025 12:26:27 +0200 Subject: Some basic function rewriting for matching different file types --- src/bibiman/bibisetup.rs | 137 +++++++++++++++++++++++++++-------------------- src/config.rs | 12 +++++ 2 files changed, 91 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index bf5baf5..8d8de53 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -55,6 +55,7 @@ pub struct BibiData { pub filepath: Option>, pub file_field: bool, pub subtitle: Option, + pub notes: Option>, } impl BibiData { @@ -200,7 +201,7 @@ impl BibiSetup { cfg: &BibiConfig, ) -> Vec { let mut pdf_files = if cfg.general.pdf_path.is_some() { - collect_pdf_file_paths(cfg.general.pdf_path.as_ref().unwrap()) + collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), Some(vec!["pdf"])) } else { None }; @@ -225,6 +226,7 @@ impl BibiSetup { filepath: filepaths.0, file_field: filepaths.1, subtitle: Self::get_subtitle(k, bibliography), + notes: None, } }) .collect() @@ -373,12 +375,21 @@ impl BibiSetup { true, ) } else if pdf_files.is_some() { - (Self::merge_filepath_or_none(&citekey, pdf_files), false) + ( + Self::merge_filepath_or_none_two(&citekey, pdf_files, vec!["pdf"]), + false, + ) } else { (None, false) } } + // pub fn get_notepath( + // citekey: &str, + // note_files: &mut Option>>, + // ) -> Option> { + // } + pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option { if biblio.get(citekey).unwrap().subtitle().is_ok() { Some( @@ -399,53 +410,7 @@ impl BibiSetup { pdf_files: &mut Option>>, ) -> Option> { let pdf_file = { - // let mut idx = 0; let citekey = citekey.to_owned().to_ascii_lowercase() + ".pdf"; - // let filename = citekey.to_owned() + ".pdf"; - // for f in args.pdf_files.unwrap().iter() { - // if f.file_name().unwrap().to_str().unwrap() == &filename { - // break f; - // } - // } - - // loop { - // if idx + 1 > pdf_files.as_ref().unwrap().len() { - // break None; - // } - // let cur_entry = pdf_files.as_ref().unwrap()[idx].clone(); - // if cur_entry.is_file() - // && cur_entry - // .file_name() - // .unwrap() - // .to_ascii_lowercase() - // .to_str() - // .unwrap() - // == citekey - // { - // let path = cur_entry.to_owned().into_os_string(); - // pdf_files.as_mut().unwrap().swap_remove(idx); - // break Some(path); - // } else { - // idx += 1 - // } - // } - - // for file in pdf_files.as_ref().unwrap().iter() { - // let filename = file.file_name().unwrap().to_ascii_lowercase(); - // if filename.to_str().unwrap() == citekey { - // break; - // } else if pdf_files.as_ref().unwrap().len() > idx { - // break; - // } else { - // idx += 1; - // } - // } - - // if pdf_files.as_ref().unwrap()[idx].is_file() { - // Some(pdf_files.as_ref().unwrap()[idx].to_owned().into_os_string()) - // } else { - // None - // } if pdf_files.as_ref().unwrap().contains_key(&citekey) { let path_vec = pdf_files @@ -462,31 +427,74 @@ impl BibiSetup { pdf_file } + + fn merge_filepath_or_none_two( + citekey: &str, + files: &mut Option>>, + extensions: Vec<&str>, + ) -> Option> { + let mut file = Vec::new(); + + for e in extensions.iter() { + let basename = citekey.to_owned().to_ascii_lowercase() + "." + e; + if files.as_ref().unwrap().contains_key(&basename) { + let _ = files + .as_ref() + .unwrap() + .get(&basename) + .unwrap() + .to_owned() + .into_iter() + .for_each(|p| file.push(p.into_os_string())); + } + } + + if file.is_empty() { + None + } else { + Some(file) + } + } } -/// This function walks the given dir and collects all pdf files into a `HashMap` -/// of the format `[String, Vec]`, where `String` represents the basename -/// of the file and the `Vec` holds all filepaths ending with this basename. +/// This function walks the given dir and collects all files matching one of the +/// passed extensions into a `HashMap` of the format `[String, Vec]`, +/// where `String` represents the basename of the file and the `Vec` holds +/// all filepaths ending with this basename. /// /// In most cases the latter is only a single path, but there might be some concepts /// with subdirs were some entries have multiple files associated with them. -pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option>> { +/// +/// Passing [`None`] as argument for extensions will result in collecting all files +/// from the given directory and its subdirectories! +pub fn collect_file_paths( + file_dir: &PathBuf, + extensions: Option>, +) -> Option>> { let mut files: HashMap> = HashMap::new(); // Expand tilde to /home/user - let pdf_dir = if pdf_dir.starts_with("~") { - &app::expand_home(&pdf_dir) + let file_dir = if file_dir.starts_with("~") { + &app::expand_home(&file_dir) } else { - pdf_dir + file_dir }; // Walk the passed dir and collect all pdf files into hashmap - if pdf_dir.is_dir() { - for file in WalkDir::new(pdf_dir) { + if file_dir.is_dir() { + for file in WalkDir::new(file_dir) { let f = file.unwrap().into_path(); if f.is_file() && f.extension().is_some() - && f.extension().unwrap_or_default().to_ascii_lowercase() == "pdf" + && extensions.as_ref().is_some_and(|v| { + v.contains( + &f.extension() + .unwrap_or_default() + .to_ascii_lowercase() + .to_str() + .unwrap_or_default(), + ) + }) { let filename = f .file_name() @@ -495,6 +503,19 @@ pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option, pub pdf_path: Option, + pub note_path: Option, + pub note_extensions: Option>, } /// Substruct [colors] in config.toml @@ -117,6 +125,8 @@ impl Default for BibiConfig { url_opener: select_opener(), file_prefix: None, pdf_path: None, + note_path: None, + note_extensions: None, }, colors: Self::dark_colors(), } @@ -133,6 +143,8 @@ impl BibiConfig { url_opener: select_opener(), file_prefix: None, pdf_path: None, + note_path: None, + note_extensions: None, }, colors: if args.light_theme { Self::light_colors() -- cgit v1.2.3