diff options
| author | lukeflo | 2024-10-14 22:04:26 +0200 |
|---|---|---|
| committer | lukeflo | 2024-10-14 22:04:26 +0200 |
| commit | c555c8666268efdff4d7107d5a527b899679559e (patch) | |
| tree | cf63b48c6034e8952a35197716338bfe73f84ee8 /src/backend | |
| parent | 5114820b9733bfb4daeedaef4f892ba9f5f023a0 (diff) | |
| download | bibiman-c555c8666268efdff4d7107d5a527b899679559e.tar.gz bibiman-c555c8666268efdff4d7107d5a527b899679559e.zip | |
check file format, use new keys() method of biblatex crate
Diffstat (limited to 'src/backend')
| -rw-r--r-- | src/backend/bib.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/backend/bib.rs b/src/backend/bib.rs index bfc959d..fecb548 100644 --- a/src/backend/bib.rs +++ b/src/backend/bib.rs @@ -19,11 +19,18 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; use std::{fs, path::PathBuf}; +#[derive(Debug)] +pub enum FileFormat { + BibLatex, + Hayagriva, +} + // Set necessary fields // TODO: can surely be made more efficient/simpler #[derive(Debug)] pub struct BibiMain { pub bibfile: PathBuf, // path to bibfile + pub bibfile_format: FileFormat, // Format of passed file pub bibfilestring: String, // content of bibfile as string pub bibliography: Bibliography, // parsed bibliography pub citekeys: Vec<String>, // list of all citekeys @@ -33,6 +40,7 @@ pub struct BibiMain { impl BibiMain { pub fn new(main_bibfile: PathBuf) -> Self { // TODO: Needs check for config file path as soon as config file is impl + let bibfile_format = Self::check_file_format(&main_bibfile); let bibfile = main_bibfile; let bibfilestring = fs::read_to_string(&bibfile).unwrap(); let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap(); @@ -40,6 +48,7 @@ impl BibiMain { let keyword_list = Self::collect_tag_list(&citekeys, &bibliography); Self { bibfile, + bibfile_format, bibfilestring, bibliography, citekeys, @@ -47,13 +56,27 @@ impl BibiMain { } } + // Check which file format the passed file has + fn check_file_format(main_bibfile: &PathBuf) -> FileFormat { + let extension = main_bibfile.extension().unwrap().to_str(); + + match extension { + Some("yml") => FileFormat::Hayagriva, + Some("yaml") => FileFormat::Hayagriva, + Some("bib") => FileFormat::BibLatex, + Some(_) => panic!("The extension {:?} is no valid bibfile", extension.unwrap()), + None => panic!("The given path {:?} holds no valid file", main_bibfile), + } + } + // get list of citekeys from the given bibfile // this list is the base for further operations on the bibentries // since it is the entry point of the biblatex crate. pub fn get_citekeys(bibstring: &Bibliography) -> Vec<String> { - let mut citekeys: Vec<String> = - bibstring.iter().map(|entry| entry.to_owned().key).collect(); - citekeys.sort_by_key(|name| name.to_lowercase()); + let citekeys: Vec<String> = bibstring.keys().map(|k| k.to_owned()).collect(); + // let mut citekeys: Vec<String> = + // bibstring.iter().map(|entry| entry.to_owned().key).collect(); + // citekeys.sort_by_key(|name| name.to_lowercase()); citekeys } |
