From b5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 18 Oct 2024 17:07:25 +0200 Subject: Handle short and long author fields --- src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 6ef7ee1..b90aeea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,9 +42,9 @@ async fn main() -> Result<()> { std::process::exit(0); } - if !parsed_args.bibfilearg.is_file() { - panic!("No \'.bib\' file passed, aborting") - } + // if !parsed_args.bibfilearg.is_file() { + // panic!("No \'.bib\' file passed, aborting") + // } init_error_hooks()?; -- cgit v1.2.3 From cf6a8d0c25bba1ee767c1dcc945cfbb577dbd13c Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 19 Oct 2024 23:05:40 +0200 Subject: format and clean code --- src/backend/bib.rs | 74 +++++++++++++++++++------------------------------ src/frontend/entries.rs | 6 +--- src/frontend/ui.rs | 5 +--- src/main.rs | 1 - 4 files changed, 31 insertions(+), 55 deletions(-) (limited to 'src/main.rs') diff --git a/src/backend/bib.rs b/src/backend/bib.rs index 29ce22e..a7df951 100644 --- a/src/backend/bib.rs +++ b/src/backend/bib.rs @@ -17,7 +17,6 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; -use hayagriva::io::from_yaml_str; use itertools::Itertools; use std::{fs, path::PathBuf}; @@ -62,7 +61,7 @@ impl BibiMain { 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, &bibfile_format); + let entry_list = Self::create_entry_list(&citekeys, &bibliography); Self { bibfile, bibfile_format, @@ -74,14 +73,6 @@ impl BibiMain { } } - fn parse_bibliography(format: &FileFormat, bibfilestring: &str) -> Bibliography { - if let FileFormat::BibLatex = format { - biblatex::Bibliography::parse(bibfilestring).unwrap() - } else if let FileFormat::Hayagriva = format { - from_yaml_str(&bibfilestring).unwrap() - } - } - // Check which file format the passed file has fn check_file_format(main_bibfile: &PathBuf) -> FileFormat { let extension = main_bibfile.extension().unwrap().to_str(); @@ -95,15 +86,11 @@ impl BibiMain { } } - fn create_entry_list( - citekeys: &[String], - bibliography: &Bibliography, - format: &FileFormat, - ) -> Vec { + fn create_entry_list(citekeys: &[String], bibliography: &Bibliography) -> Vec { citekeys .into_iter() .map(|k| BibiData { - authors: Self::get_authors(&k, &bibliography, format), + authors: Self::get_authors(&k, &bibliography), title: Self::get_title(&k, &bibliography), year: Self::get_year(&k, &bibliography), pubtype: Self::get_pubtype(&k, &bibliography), @@ -158,41 +145,38 @@ impl BibiMain { keyword_list } - pub fn get_authors(citekey: &str, biblio: &Bibliography, format: &FileFormat) -> String { - if let FileFormat::BibLatex = format { - if biblio.get(&citekey).unwrap().author().is_ok() { - let authors = biblio.get(&citekey).unwrap().author().unwrap(); - if authors.len() > 1 { - let all_authors = authors.iter().map(|a| &a.name).join(", "); - all_authors - } else if authors.len() == 1 { - let authors = authors[0].name.to_string(); - authors - } else { - let editors_authors = format!("empty"); - editors_authors - } + pub fn get_authors(citekey: &str, biblio: &Bibliography) -> String { + if biblio.get(&citekey).unwrap().author().is_ok() { + let authors = biblio.get(&citekey).unwrap().author().unwrap(); + if authors.len() > 1 { + let all_authors = authors.iter().map(|a| &a.name).join(", "); + all_authors + } else if authors.len() == 1 { + let authors = authors[0].name.to_string(); + authors } else { - if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() { - let editors = biblio.get(&citekey).unwrap().editors().unwrap(); - if editors[0].0.len() > 1 { - // let editors = format!("{} (ed.) et al.", editors[0].0[0].name); - let mut editors = editors[0].0.iter().map(|e| &e.name).join(", "); - editors.push_str(" (ed.)"); - editors - } else if editors[0].0.len() == 1 { - let editors = format!("{} (ed.)", editors[0].0[0].name); - editors - } else { - let editors_authors = format!("empty"); - editors_authors - } + let editors_authors = format!("empty"); + editors_authors + } + } else { + if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() { + let editors = biblio.get(&citekey).unwrap().editors().unwrap(); + if editors[0].0.len() > 1 { + // let editors = format!("{} (ed.) et al.", editors[0].0[0].name); + let mut editors = editors[0].0.iter().map(|e| &e.name).join(", "); + editors.push_str(" (ed.)"); + editors + } else if editors[0].0.len() == 1 { + let editors = format!("{} (ed.)", editors[0].0[0].name); + editors } else { let editors_authors = format!("empty"); editors_authors } + } else { + let editors_authors = format!("empty"); + editors_authors } - } else if let FileFormat::Hayagriva = format { } } diff --git a/src/frontend/entries.rs b/src/frontend/entries.rs index dea970e..6c227df 100644 --- a/src/frontend/entries.rs +++ b/src/frontend/entries.rs @@ -17,11 +17,7 @@ use super::app::App; use super::tui::Tui; -use crate::backend::{ - bib::{BibiData, BibiMain, FileFormat}, - search::BibiSearch, -}; -use biblatex::Bibliography; +use crate::backend::{bib::BibiData, search::BibiSearch}; use color_eyre::eyre::{Context, Ok, Result}; use core::panic; use editor_command::EditorBuilder; diff --git a/src/frontend/ui.rs b/src/frontend/ui.rs index d94a654..4ea275d 100644 --- a/src/frontend/ui.rs +++ b/src/frontend/ui.rs @@ -28,10 +28,7 @@ use ratatui::{ }, }; -use crate::{ - backend::bib::BibiMain, - frontend::{app::App, keywords::TagListItem}, -}; +use crate::frontend::{app::App, keywords::TagListItem}; use super::app::{CurrentArea, FormerArea}; diff --git a/src/main.rs b/src/main.rs index b90aeea..979c4cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,6 @@ use backend::cliargs::{self, CLIArgs}; use color_eyre::eyre::Result; -use core::panic; use errorsetup::init_error_hooks; use frontend::app::App; -- cgit v1.2.3