diff options
| author | lukeflo | 2025-01-29 21:04:49 +0100 |
|---|---|---|
| committer | lukeflo | 2025-01-29 21:04:49 +0100 |
| commit | dd108698cfbfda6ba251c75f821f7a4ede9b0608 (patch) | |
| tree | c7ac2d525f478d9f7ead65a48c148b7006603deb | |
| parent | fb34960e523b8f618fc4e024902f43928d8aa716 (diff) | |
| download | bibiman-dd108698cfbfda6ba251c75f821f7a4ede9b0608.tar.gz bibiman-dd108698cfbfda6ba251c75f821f7a4ede9b0608.zip | |
remove EntryTableItem struct, simply use BibiData
| -rw-r--r-- | src/bibiman.rs | 12 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 74 | ||||
| -rw-r--r-- | src/bibiman/entries.rs | 239 | ||||
| -rw-r--r-- | src/bibiman/search.rs | 22 |
4 files changed, 206 insertions, 141 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index 9955896..ee769c4 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -59,7 +59,7 @@ pub enum FormerArea { // Application. #[derive(Debug)] -pub struct Bibiman<'a> { +pub struct Bibiman { // main bib file // pub main_bibfiles: Vec<PathBuf>, // main bibliography @@ -69,7 +69,7 @@ pub struct Bibiman<'a> { // tag list pub tag_list: TagList, // table items - pub entry_table: EntryTable<'a>, + pub entry_table: EntryTable, // scroll state info buffer pub scroll_info: u16, // area @@ -80,14 +80,14 @@ pub struct Bibiman<'a> { pub popup_area: PopupArea, } -impl Bibiman<'_> { +impl Bibiman { // Constructs a new instance of [`App`]. pub fn new(args: &CLIArgs) -> Result<Self> { // let main_bibfiles = args.fileargs.clone(); let main_biblio = BibiSetup::new(&args.files); let tag_list = TagList::new(main_biblio.keyword_list.clone()); let search_struct = BibiSearch::default(); - let entry_table = EntryTable::new(&main_biblio.entry_list); + let entry_table = EntryTable::new(main_biblio.entry_list.clone()); let current_area = CurrentArea::EntryArea; Ok(Self { // main_bibfiles, @@ -131,7 +131,7 @@ impl Bibiman<'_> { pub fn update_lists(&mut self, args: &CLIArgs) { self.main_biblio = BibiSetup::new(&args.files); self.tag_list = TagList::new(self.main_biblio.keyword_list.clone()); - self.entry_table = EntryTable::new(&self.main_biblio.entry_list); + self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone()); } /// Toggle moveable list between entries and tags @@ -155,7 +155,7 @@ impl Bibiman<'_> { } pub fn reset_current_list(&mut self) { - self.entry_table = EntryTable::new(&self.main_biblio.entry_list); + self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone()); self.tag_list = TagList::new(self.main_biblio.keyword_list.clone()); if let CurrentArea::TagArea = self.current_area { self.tag_list.tag_list_state.select(Some(0)) diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 5aa11be..21b3c4b 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -19,7 +19,7 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; use color_eyre::owo_colors::OwoColorize; use itertools::Itertools; -use std::ffi::OsString; +use std::ffi::{OsStr, OsString}; use std::{fs, path::PathBuf}; use crate::cliargs; @@ -36,10 +36,11 @@ pub struct BibiSetup { pub entry_list: Vec<BibiData>, // List of all entries } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct BibiData { pub id: u32, pub authors: String, + pub short_author: String, pub title: String, pub year: String, pub pubtype: String, @@ -51,6 +52,74 @@ pub struct BibiData { pub subtitle: Option<String>, } +impl BibiData { + // This functions decides which fields are rendered in the entry table + // Fields which should be usable but not visible can be left out + pub fn ref_vec(&mut self) -> Vec<&str> { + self.short_author = match self.authors.split_once(",") { + Some((first, _rest)) => { + if self.authors().contains("(ed.)") { + let first_author = format!("{} et al. (ed.)", first); + first_author + } else { + let first_author = format!("{} et al.", first); + first_author + } + } + None => String::from(""), + }; + + vec![ + { + if self.short_author.is_empty() { + self.authors() + } else { + &self.short_author + } + }, + self.title(), + self.year(), + self.pubtype(), + ] + } + + pub fn entry_id(&self) -> &u32 { + &self.id + } + + pub fn authors(&self) -> &str { + &self.authors + } + + pub fn title(&self) -> &str { + &self.title + } + + pub fn year(&self) -> &str { + &self.year + } + + pub fn pubtype(&self) -> &str { + &self.pubtype + } + + pub fn citekey(&self) -> &str { + &self.citekey + } + + pub fn doi_url(&self) -> &str { + self.doi_url.as_ref().unwrap() + } + + pub fn filepath(&self) -> &OsStr { + self.filepath.as_ref().unwrap() + } + + pub fn subtitle(&self) -> &str { + self.subtitle.as_ref().unwrap() + } +} + impl BibiSetup { pub fn new(main_bibfiles: &[PathBuf]) -> Self { // TODO: Needs check for config file path as soon as config file is impl @@ -119,6 +188,7 @@ impl BibiSetup { .map(|(i, k)| BibiData { id: i as u32, authors: Self::get_authors(k, bibliography), + short_author: String::new(), title: Self::get_title(k, bibliography), year: Self::get_year(k, bibliography), pubtype: Self::get_pubtype(k, bibliography), diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index 857b01c..961e66d 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -30,9 +30,9 @@ pub enum EntryTableColumn { // Define list containing entries as table #[derive(Debug, PartialEq, Eq)] -pub struct EntryTable<'a> { - pub entry_table_items: Vec<EntryTableItem<'a>>, - pub entry_table_at_search_start: Vec<EntryTableItem<'a>>, +pub struct EntryTable { + pub entry_table_items: Vec<BibiData>, + pub entry_table_at_search_start: Vec<BibiData>, pub entry_table_selected_column: EntryTableColumn, pub entry_table_sorted_by_col: Option<EntryTableColumn>, pub entry_table_reversed_sort: bool, @@ -42,9 +42,10 @@ pub struct EntryTable<'a> { pub entry_info_scroll_state: ScrollbarState, } -impl<'a> EntryTable<'a> { - pub fn new(entry_list: &'a [BibiData]) -> Self { - let entry_table_items = Self::set_entry_table(entry_list); +impl EntryTable { + pub fn new(entry_list: Vec<BibiData>) -> Self { + // let entry_table_items = Self::set_entry_table(entry_list); + let entry_table_items = entry_list; let entry_table_state = TableState::default() .with_selected(0) .with_selected_column(0) @@ -64,28 +65,28 @@ impl<'a> EntryTable<'a> { } } - pub fn set_entry_table(entry_list: &[BibiData]) -> Vec<EntryTableItem> { - let mut entry_table: Vec<EntryTableItem> = entry_list - .iter() - .map(|e| EntryTableItem { - id: e.id, - authors: &e.authors, - short_author: String::new(), - title: &e.title, - year: &e.year, - pubtype: &e.pubtype, - keywords: &e.keywords, - citekey: &e.citekey, - abstract_text: &e.abstract_text, - doi_url: e.doi_url.as_deref(), - filepath: e.filepath.clone(), - subtitle: e.subtitle.as_deref(), - }) - .collect(); + // pub fn set_entry_table(entry_list: &[BibiData]) -> Vec<EntryTableItem> { + // let mut entry_table: Vec<EntryTableItem> = entry_list + // .iter() + // .map(|e| EntryTableItem { + // id: e.id, + // authors: &e.authors, + // short_author: String::new(), + // title: &e.title, + // year: &e.year, + // pubtype: &e.pubtype, + // keywords: &e.keywords, + // citekey: &e.citekey, + // abstract_text: &e.abstract_text, + // doi_url: e.doi_url.as_deref(), + // filepath: e.filepath.clone(), + // subtitle: e.subtitle.as_deref(), + // }) + // .collect(); - entry_table.sort_by(|a, b| a.authors.to_lowercase().cmp(&b.authors.to_lowercase())); - entry_table - } + // entry_table.sort_by(|a, b| a.authors.to_lowercase().cmp(&b.authors.to_lowercase())); + // entry_table + // } pub fn sort_by_id(&mut self) { if self.entry_table_sorted_by_col.is_some() { @@ -151,94 +152,94 @@ impl<'a> EntryTable<'a> { } } -// Define contents of each entry table row -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct EntryTableItem<'a> { - pub id: u32, - pub authors: &'a str, - pub short_author: String, - pub title: &'a str, - pub year: &'a str, - pub pubtype: &'a str, - pub keywords: &'a str, - pub citekey: &'a str, - pub abstract_text: &'a str, - pub doi_url: Option<&'a str>, - pub filepath: Option<OsString>, - pub subtitle: Option<&'a str>, -} +// // Define contents of each entry table row +// #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +// pub struct EntryTableItem { +// pub id: u32, +// pub authors: String, +// pub short_author: String, +// pub title: String, +// pub year: String, +// pub pubtype: String, +// pub keywords: String, +// pub citekey: String, +// pub abstract_text: String, +// pub doi_url: Option<String>, +// pub filepath: Option<OsString>, +// pub subtitle: Option<String>, +// } -impl<'a> EntryTableItem<'a> { - // This functions decides which fields are rendered in the entry table - // Fields which should be usable but not visible can be left out - pub fn ref_vec(&mut self) -> Vec<&str> { - self.short_author = match self.authors.split_once(",") { - Some((first, _rest)) => { - if self.authors().contains("(ed.)") { - let first_author = format!("{} et al. (ed.)", first); - first_author - } else { - let first_author = format!("{} et al.", first); - first_author - } - } - None => String::from(""), - }; +// impl EntryTableItem { +// // This functions decides which fields are rendered in the entry table +// // Fields which should be usable but not visible can be left out +// pub fn ref_vec(&mut self) -> Vec<&str> { +// self.short_author = match self.authors.split_once(",") { +// Some((first, _rest)) => { +// if self.authors().contains("(ed.)") { +// let first_author = format!("{} et al. (ed.)", first); +// first_author +// } else { +// let first_author = format!("{} et al.", first); +// first_author +// } +// } +// None => String::from(""), +// }; - vec![ - { - if self.short_author.is_empty() { - self.authors() - } else { - &self.short_author - } - }, - self.title(), - self.year(), - self.pubtype(), - ] - } +// vec![ +// { +// if self.short_author.is_empty() { +// self.authors() +// } else { +// &self.short_author +// } +// }, +// self.title(), +// self.year(), +// self.pubtype(), +// ] +// } - pub fn entry_id(&self) -> &u32 { - &self.id - } +// pub fn entry_id(&self) -> &u32 { +// &self.id +// } - pub fn authors(&self) -> &str { - &self.authors - } +// pub fn authors(&self) -> &str { +// &self.authors +// } - pub fn title(&self) -> &str { - &self.title - } +// pub fn title(&self) -> &str { +// &self.title +// } - pub fn year(&self) -> &str { - &self.year - } +// pub fn year(&self) -> &str { +// &self.year +// } - pub fn pubtype(&self) -> &str { - &self.pubtype - } +// pub fn pubtype(&self) -> &str { +// &self.pubtype +// } - pub fn citekey(&self) -> &str { - &self.citekey - } +// pub fn citekey(&self) -> &str { +// &self.citekey +// } - pub fn doi_url(&self) -> &str { - self.doi_url.as_ref().unwrap() - } +// pub fn doi_url(&self) -> &str { +// self.doi_url.as_ref().unwrap() +// } - pub fn filepath(&self) -> &OsStr { - self.filepath.as_ref().unwrap() - } +// pub fn filepath(&self) -> &OsStr { +// self.filepath.as_ref().unwrap() +// } - pub fn subtitle(&self) -> &str { - self.subtitle.as_ref().unwrap() - } -} +// pub fn subtitle(&self) -> &str { +// self.subtitle.as_ref().unwrap() +// } +// } #[cfg(test)] mod tests { - use super::EntryTableItem; + use crate::bibiman::BibiData; #[test] fn check_os() { @@ -253,39 +254,39 @@ mod tests { #[test] fn shorten_authors() { - let mut entry: EntryTableItem = EntryTableItem { + let mut entry: BibiData = BibiData { id: 1, - authors: "Miller, Schmitz, Bernard", + authors: "Miller, Schmitz, Bernard".to_string(), short_author: String::new(), - title: "A title", - year: "2000", - pubtype: "article", - keywords: "key1, key2", - citekey: "miller_2000", - abstract_text: "An abstract", + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), doi_url: None, filepath: None, subtitle: None, }; - let entry_vec = EntryTableItem::ref_vec(&mut entry); + let entry_vec = BibiData::ref_vec(&mut entry); - let mut entry_editors: EntryTableItem = EntryTableItem { + let mut entry_editors: BibiData = BibiData { id: 2, - authors: "Miller, Schmitz, Bernard (ed.)", + authors: "Miller, Schmitz, Bernard (ed.)".to_string(), short_author: String::new(), - title: "A title", - year: "2000", - pubtype: "article", - keywords: "key1, key2", - citekey: "miller_2000", - abstract_text: "An abstract", + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), doi_url: None, filepath: None, subtitle: None, }; - let entry_vec_editors = EntryTableItem::ref_vec(&mut entry_editors); + let entry_vec_editors = BibiData::ref_vec(&mut entry_editors); assert_eq!( entry_vec, diff --git a/src/bibiman/search.rs b/src/bibiman/search.rs index dff835d..1855092 100644 --- a/src/bibiman/search.rs +++ b/src/bibiman/search.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// -use super::entries::EntryTableItem; +use super::BibiData; use nucleo_matcher::{ pattern::{CaseMatching, Normalization, Pattern}, Config, Matcher, @@ -31,7 +31,7 @@ pub struct BibiSearch { impl BibiSearch { // Stringify EntryTableItem by joining/concat - fn convert_to_string(inner_vec: &EntryTableItem) -> String { + fn convert_to_string(inner_vec: &BibiData) -> String { format!( "{} {} {} {} {} {}", &inner_vec.authors, @@ -44,12 +44,9 @@ impl BibiSearch { } // Return a filtered entry list - pub fn search_entry_list( - search_pattern: &str, - orig_list: Vec<EntryTableItem>, - ) -> Vec<EntryTableItem> { + pub fn search_entry_list(search_pattern: &str, orig_list: Vec<BibiData>) -> Vec<BibiData> { // Create a hashmap to connect stingified entry with entry vec - let mut entry_string_hm: HashMap<String, EntryTableItem> = HashMap::new(); + let mut entry_string_hm: HashMap<String, BibiData> = HashMap::new(); // Convert all entries to string and insert them into the hashmap // next to the original inner Vec<String> of the entry list @@ -70,7 +67,7 @@ impl BibiSearch { // Create filtered entry list and push the inner entry vec's to it // Use the filtered stringified hm-key as index - let mut filtered_list: Vec<EntryTableItem> = Vec::new(); + let mut filtered_list: Vec<BibiData> = Vec::new(); for m in filtered_matches { filtered_list.push(entry_string_hm[&m].to_owned()); } @@ -92,11 +89,8 @@ impl BibiSearch { filtered_matches } - pub fn filter_entries_by_tag( - keyword: &str, - orig_list: &Vec<EntryTableItem>, - ) -> Vec<EntryTableItem> { - let mut filtered_list: Vec<EntryTableItem> = Vec::new(); + pub fn filter_entries_by_tag(keyword: &str, orig_list: &Vec<BibiData>) -> Vec<BibiData> { + let mut filtered_list: Vec<BibiData> = Vec::new(); // Loop over the whole given entry table // Check if the selected keyword is present in the current entry @@ -127,7 +121,7 @@ mod tests { #[test] fn test_vector_join() { - let bibvec: EntryTableItem = EntryTableItem { + let bibvec: BibiData = BibiData { id: 3, authors: "Author".to_string(), short_author: "".to_string(), |
