From c555c8666268efdff4d7107d5a527b899679559e Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 14 Oct 2024 22:04:26 +0200 Subject: check file format, use new keys() method of biblatex crate --- src/backend/bib.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/backend') 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, // 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 { - let mut citekeys: Vec = - bibstring.iter().map(|entry| entry.to_owned().key).collect(); - citekeys.sort_by_key(|name| name.to_lowercase()); + let citekeys: Vec = bibstring.keys().map(|k| k.to_owned()).collect(); + // let mut citekeys: Vec = + // bibstring.iter().map(|entry| entry.to_owned().key).collect(); + // citekeys.sort_by_key(|name| name.to_lowercase()); citekeys } -- cgit v1.2.3