// bibiman - a TUI for managing BibLaTeX databases
// Copyright (C) 2024 lukeflo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/////
use biblatex::{self, Bibliography};
use biblatex::{ChunksExt, Type};
use itertools::Itertools;
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
pub keyword_list: Vec<String>, // list of all available keywords
pub entry_list: Vec<BibiData>, // List of all entries
}
#[derive(Debug, Clone)]
pub struct BibiData {
pub authors: String,
pub title: String,
pub year: String,
pub pubtype: String,
pub keywords: String,
pub citekey: String,
pub abstract_text: String,
pub doi_url: String,
pub filepath: String,
}
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();
let citekeys = Self::get_citekeys(&bibliography);
let keyword_list = Self::collect_tag_list(&citekeys, &bibliography);
let entry_list = Self::create_entry_list(&citekeys, &bibliography);
Self {
bibfile,
bibfile_format,
bibfilestring,
bibliography,
citekeys,
keyword_list,
entry_list,
}
}
// 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),
}
}
fn create_entry_list(citekeys: &[String], bibliography: &Bibliography) -> Vec<BibiData> {
citekeys
.into_iter()
.map(|k| BibiData {
authors: Self::get_authors(&k, &bibliography),
title: Self::get_title(&k, &bibliography),
year: Self::get_year(&k, &bibliography),
pubtype: Self::get_pubtype(&k, &bibliography),
keywords: Self::get_keywords(&k, &bibliography),
citekey: k.to_owned(),
abstract_text: Self::get_abstract(&k, &bibliography