diff options
| author | lukeflo | 2024-09-26 22:20:21 +0200 |
|---|---|---|
| committer | lukeflo | 2024-09-26 22:20:21 +0200 |
| commit | 576e7a39312f26ff3df24cf2eb279e6fdabbe903 (patch) | |
| tree | 26cda065ccf821c9b26036046854f3b768828be1 /src | |
| parent | 81cd14a7603ce4a9b312f3a5a62eb8e9ad7e3e2f (diff) | |
| download | bibiman-576e7a39312f26ff3df24cf2eb279e6fdabbe903.tar.gz bibiman-576e7a39312f26ff3df24cf2eb279e6fdabbe903.zip | |
refinement of bib backend 2.0
Diffstat (limited to 'src')
| -rw-r--r-- | src/backend/bib.rs | 109 | ||||
| -rw-r--r-- | src/backend/cliargs.rs | 4 | ||||
| -rw-r--r-- | src/frontend/app.rs | 159 | ||||
| -rw-r--r-- | src/main.rs | 12 |
4 files changed, 96 insertions, 188 deletions
diff --git a/src/backend/bib.rs b/src/backend/bib.rs index d0658fd..c520395 100644 --- a/src/backend/bib.rs +++ b/src/backend/bib.rs @@ -18,7 +18,6 @@ use super::cliargs::PosArgs; use std::{fs, path::PathBuf}; -use biblatex::PermissiveType; use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; @@ -29,6 +28,7 @@ pub struct BibiMain { pub bibfilestring: String, // content of bibfile as string pub bibliography: Bibliography, // parsed bibliography pub citekeys: Vec<String>, // list of all citekeys + // pub bibentries: BibiDataSets, } impl BibiMain { @@ -38,13 +38,11 @@ impl BibiMain { let bibfilestring = fs::read_to_string(&bibfile).unwrap(); let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap(); let citekeys = Self::get_citekeys(&bibliography); - // let bibentries = BibiDataSets::from_iter(citekeys.clone()); Self { bibfile, bibfilestring, bibliography, citekeys, - // bibentries, } } @@ -65,10 +63,15 @@ pub struct BibiData { } impl BibiData { - pub fn new() -> Self { - let citekeys = BibiMain::new().citekeys; + pub fn new(biblio: &Bibliography, citekeys: &Vec<String>) -> Self { Self { - entry_list: BibiDataSets::from_iter(citekeys), + entry_list: { + let bibentries = citekeys + .into_iter() + .map(|citekey| BibiEntry::new(&citekey, &biblio)) + .collect(); + BibiDataSets { bibentries } + }, } } } @@ -77,17 +80,7 @@ impl BibiData { // Necessary for implementing FromIterator #[derive(Debug)] pub struct BibiDataSets { - pub bibentries: Vec<BibiEntry>, -} - -impl FromIterator<String> for BibiDataSets { - fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self { - let bibentries = iter - .into_iter() - .map(|citekey| BibiEntry::new(&citekey)) - .collect(); - Self { bibentries } - } + pub bibentries: Vec<Vec<String>>, } // Struct which has to be created for every entry of bibdatabase @@ -101,22 +94,18 @@ pub struct BibiEntry { pub citekey: String, } -// INFO & TODO: Iterator needs to process all citekeys (Vec<String>) and should output another vector which holds every single entry as BibiEntry struct (Vec<BibiEntry>). Maybe the BibiEntry struct has to be wrapped inside a larger BibiEntryVec/BibiDataSets struct or similar. -> Iterator for BibiDataSets! - impl BibiEntry { - pub fn new(citekey: &str) -> Self { - Self { - authors: Self::get_authors(citekey), - title: Self::get_title(citekey), - year: Self::get_year(citekey), - pubtype: Self::get_pubtype(citekey), - // keywords: Self::get_keywords(citekey), - citekey: citekey.to_string(), - } + pub fn new(citekey: &str, biblio: &Bibliography) -> Vec<String> { + vec![ + Self::get_authors(&citekey, &biblio), + Self::get_title(&citekey, &biblio), + Self::get_year(&citekey, &biblio), + Self::get_pubtype(&citekey, &biblio), + citekey.to_string(), + ] } - fn get_authors(citekey: &str) -> String { - let biblio = BibiMain::new().bibliography; + fn get_authors(citekey: &str, biblio: &Bibliography) -> String { let authors = { if biblio.get(&citekey).unwrap().author().is_ok() { let authors = biblio.get(&citekey).unwrap().author().unwrap(); @@ -148,37 +137,29 @@ impl BibiEntry { editors_authors } } - - // if biblio.get(&citekey).unwrap().editors().is_ok() { - // let editors = biblio.get(&citekey).unwrap().editors().unwrap(); - // if editors.len() > 1 { - // let editors = format!("{} (ed.) et al.", editors[0].0[0].name); - // editors - // } else { - // let editors = editors[0].0[0].name.to_string(); - // editors - // } - // } else { - // let editors_authors = format!("empty"); - // editors_authors - // } }; authors } - fn get_title(citekey: &str) -> String { - let biblio = BibiMain::new().bibliography; - let title = biblio - .get(&citekey) - .unwrap() - .title() - .unwrap() - .format_verbatim(); + fn get_title(citekey: &str, biblio: &Bibliography) -> String { + let title = { + if biblio.get(&citekey).unwrap().title().is_ok() { + let title = biblio + .get(&citekey) + .unwrap() + .title() + .unwrap() + .format_verbatim(); + title + } else { + let title = format!("no title"); + title + } + }; title } - fn get_year(citekey: &str) -> String { - let biblio = BibiMain::new().bibliography; + fn get_year(citekey: &str, biblio: &Bibliography) -> String { let year = biblio.get(&citekey).unwrap(); let year = { if year.date().is_ok() { @@ -186,33 +167,19 @@ impl BibiEntry { let year = year[..4].to_string(); year } else { - let year = format!("emtpy"); + let year = format!("n.d."); year } }; year - - // let year = &year[..4].to_string(); - // let year = { - // if year.is_empty() { - // let year = format!("empty"); - // year - // } else { - // let year = year.to_string(); - // year - // } - // }; - // year } - fn get_pubtype(citekey: &str) -> String { - let biblio = BibiMain::new().bibliography; + fn get_pubtype(citekey: &str, biblio: &Bibliography) -> String { let pubtype = biblio.get(&citekey).unwrap().entry_type.to_string(); pubtype } - fn get_keywords(citekey: &str) -> String { - let biblio = BibiMain::new().bibliography; + fn get_keywords(citekey: &str, biblio: &Bibliography) -> String { let keywords = biblio .get(&citekey) .unwrap() diff --git a/src/backend/cliargs.rs b/src/backend/cliargs.rs index f3e9fcf..32972e0 100644 --- a/src/backend/cliargs.rs +++ b/src/backend/cliargs.rs @@ -29,10 +29,6 @@ sarge! { // Show version and exit. TODO: Write version... 'v' version: bool, - - // Option for file: -b - short option; --bibfile - long option - // #ok makes it optional - #ok 'b' bibfile: String, } // struct for CLIArgs diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 51a5aff..d61d094 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -93,33 +93,16 @@ impl FromIterator<String> for TagList { // Iterate over vector fields with entries data to create TableItems // Number of Strings has to match number of fields -impl FromIterator<[String; 5]> for EntryTable { - fn from_iter<T: IntoIterator<Item = [String; 5]>>(iter: T) -> Self { - // Has to be Vev<EntryTableItem> - let entry_table_items = iter - .into_iter() - // fields in map must not be named specific - .map(|[authors, title, year, pubtype, citekey]| { - EntryTableItem::new(&authors, &title, &year, &pubtype, &citekey) - }) - .sorted_by(|a, b| a.authors.cmp(&b.authors)) - .collect(); - let entry_table_state = TableState::default().with_selected(0); - Self { - entry_table_items, - entry_table_state, - } - } -} - -// // Also possible with vector. TODO: Decide whats better -// impl FromIterator<Vec<String>> for EntryTable { -// fn from_iter<T: IntoIterator<Item = Vec<String>>>(iter: T) -> Self { +// impl FromIterator<[String; 5]> for EntryTable { +// fn from_iter<T: IntoIterator<Item = [String; 5]>>(iter: T) -> Self { // // Has to be Vev<EntryTableItem> // let entry_table_items = iter // .into_iter() -// .sorted() -// .map(|i| EntryTableItem::new(&i[0], &i[1], &i[2], &i[3], &i[4])) +// // fields in map must not be named specific +// .map(|[authors, title, year, pubtype, citekey]| { +// EntryTableItem::new(&authors, &title, &year, &pubtype, &citekey) +// }) +// .sorted_by(|a, b| a.authors.cmp(&b.authors)) // .collect(); // let entry_table_state = TableState::default().with_selected(0); // Self { @@ -129,6 +112,23 @@ impl FromIterator<[String; 5]> for EntryTable { // } // } +// // Also possible with vector. TODO: Decide whats better +impl FromIterator<Vec<String>> for EntryTable { + fn from_iter<T: IntoIterator<Item = Vec<String>>>(iter: T) -> Self { + // Has to be Vev<EntryTableItem> + let entry_table_items = iter + .into_iter() + .sorted() + .map(|i| EntryTableItem::new(&i[0], &i[1], &i[2], &i[3], &i[4])) + .collect(); + let entry_table_state = TableState::default().with_selected(0); + Self { + entry_table_items, + entry_table_state, + } + } +} + // Define list containing entries as table #[derive(Debug)] pub struct EntryTable { @@ -185,102 +185,39 @@ impl EntryTableItem { } } -impl Default for App { - fn default() -> Self { - // TEST: read file - let lines = BibiMain::new().citekeys; - let iter = vec![ - [ - "Mrs. Doubtfire".to_string(), - "A great book of great length".to_string(), - "2003".to_string(), - "book".to_string(), - "doubtfire_2003".to_string(), - ], - [ - "Veye Tatah".to_string(), - "Modern economy".to_string(), - 1995.to_string(), - "article".to_string(), - "tatah_1995".to_string(), - ], - [ - "Joseph Conrad".to_string(), - "Heart of Darkness".to_string(), - 1899.to_string(), - "book".to_string(), - "conrad_1899".to_string(), - ], - [ - "Michelle-Rolpg Trouillot".to_string(), - "Silencing the Past".to_string(), - "1995".to_string(), - "book".to_string(), - "trouillot_1995".to_string(), - ], - [ - "Zora Neale Hurston".to_string(), - "Barracoon".to_string(), - "1919".to_string(), - "book".to_string(), - "hurston_1919".to_string(), - ], - ]; - // let iter = vec![ - // vec![ - // "Mrs. Doubtfire".to_string(), - // "A great book of great length".to_string(), - // "2003".to_string(), - // "book".to_string(), - // "doubtfire_2003".to_string(), - // ], - // vec![ - // "Veye Tatah".to_string(), - // "Modern economy".to_string(), - // 1995.to_string(), - // "article".to_string(), - // "tatah_1995".to_string(), - // ], - // vec![ - // "Joseph Conrad".to_string(), - // "Heart of Darkness".to_string(), - // 1899.to_string(), - // "book".to_string(), - // "conrad_1899".to_string(), - // ], - // vec![ - // "Michelle-Rolpg Trouillot".to_string(), - // "Silencing the Past".to_string(), - // "1995".to_string(), - // "book".to_string(), - // "trouillot_1995".to_string(), - // ], - // vec![ - // "Zora Neale Hurston".to_string(), - // "Barracoon".to_string(), - // "1919".to_string(), - // "book".to_string(), - // "hurston_1919".to_string(), - // ], - // ]; +// impl Default for App { +// fn default(bib_main: &BibiMain, bib_data: &BibiData) -> Self { +// // TEST: read file +// let keyword_list = BibiMain::new().citekeys; +// let entry_vec = BibiData::new().entry_list.bibentries; +// Self { +// running: true, +// tag_list: TagList::from_iter(keyword_list), +// entry_table: EntryTable::from_iter(entry_vec), +// current_area: CurrentArea::EntryArea, +// } +// } +// } + +impl App { + // Constructs a new instance of [`App`]. + pub fn new(bib_main: &BibiMain, bib_data: &BibiData) -> Self { Self { running: true, - tag_list: TagList::from_iter(lines), - entry_table: EntryTable::from_iter(iter), + tag_list: TagList::from_iter(bib_main.citekeys.clone()), + entry_table: EntryTable::from_iter(bib_data.entry_list.bibentries.clone()), current_area: CurrentArea::EntryArea, } } -} - -impl App { - // Constructs a new instance of [`App`]. - pub fn new() -> Self { - Self::default() - } // Handles the tick event of the terminal. pub fn tick(&self) {} + // TODO: Create process to do something with selected entry + // The logic getting e.g. the citekey of the selected entry is: + // let idx = self.entry_table.entry_table_state.selected().unwrap(); + // println!("{:#?}", self.entry_table.entry_table_items[*idx].citekey); + // Set running to false to quit the application. pub fn quit(&mut self) { self.running = false; diff --git a/src/main.rs b/src/main.rs index a9011f6..a07b75a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,10 @@ use std::io; -use backend::cliargs::{self, CLIArgs}; +use backend::{ + bib::{BibiData, BibiMain}, + cliargs::{self, CLIArgs}, +}; use ratatui::{backend::CrosstermBackend, Terminal}; use crate::{ @@ -50,8 +53,11 @@ async fn main() -> AppResult<()> { // TODO: Implement logic for CLI arguments/options which need to be handled // before the TUI is started + let mut bib_main = BibiMain::new(); + let mut bib_data = BibiData::new(&bib_main.bibliography, &bib_main.citekeys); + // Create an application. - let mut app = App::new(); + let mut app = App::new(&mut bib_main, &mut bib_data); // Initialize the terminal user interface. let backend = CrosstermBackend::new(io::stdout()); @@ -75,5 +81,7 @@ async fn main() -> AppResult<()> { // Exit the user interface. tui.exit()?; + // let idx = &app.entry_table.entry_table_state.selected().unwrap(); + // println!("{:#?}", &app.entry_table.entry_table_items[*idx].citekey); Ok(()) } |
