From 9bce9fdc82cbcb4bdc84d3490d7cc755aae935f3 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 8 Nov 2024 15:36:56 +0100 Subject: simplify bibdata sourcing --- src/bibiman.rs | 10 +++++++--- src/bibiman/bibisetup.rs | 37 +++++++++++++++++-------------------- src/bibiman/entries.rs | 16 ++++++++-------- src/tui/ui.rs | 6 +++--- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/bibiman.rs b/src/bibiman.rs index b07512d..a573ee7 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -375,7 +375,7 @@ impl Bibiman { // Pass filepath as argument, pipe stdout and stderr to /dev/null // to keep the TUI clean (where is it piped on Windows???) let _ = Command::new(&cmd) - .arg(&filepath) + .arg(&filepath.as_ref().unwrap()) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() @@ -386,7 +386,11 @@ impl Bibiman { pub fn open_doi_url(&mut self) -> Result<()> { let idx = self.entry_table.entry_table_state.selected().unwrap(); - let web_adress = self.entry_table.entry_table_items[idx].doi_url.clone(); + let web_adress = self.entry_table.entry_table_items[idx] + .doi_url + .as_ref() + .unwrap(); + // .clone(); // Resolve strings using the resolving function of dx.doi.org, so the // terminal is not blocked by the resolving process @@ -397,7 +401,7 @@ impl Bibiman { let prefix = "https://".to_string(); prefix + &web_adress } else { - web_adress + web_adress.to_string() }; // Build command to execute browser. 'xdg-open' is Linux standard diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index e17997d..027e217 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -51,8 +51,8 @@ pub struct BibiData { pub keywords: String, pub citekey: String, pub abstract_text: String, - pub doi_url: String, - pub filepath: String, + pub doi_url: Option, + pub filepath: Option, pub subtitle: Option, } @@ -219,10 +219,11 @@ impl BibiSetup { } pub fn get_year(citekey: &str, biblio: &Bibliography) -> String { - let year = biblio.get(&citekey).unwrap(); + let bib = biblio.get(&citekey).unwrap(); + // let year = biblio.get(&citekey).unwrap(); let year = { - if year.date().is_ok() { - let year = year.date().unwrap().to_chunks().format_verbatim(); + if bib.date().is_ok() { + let year = bib.date().unwrap().to_chunks().format_verbatim(); let year = year[..4].to_string(); year } else { @@ -274,26 +275,22 @@ impl BibiSetup { text } - pub fn get_weblink(citekey: &str, biblio: &Bibliography) -> String { - if let true = biblio.get(&citekey).unwrap().doi().is_ok() { - let url = biblio.get(&citekey).unwrap().doi().unwrap(); - url - } else if let true = biblio.get(&citekey).unwrap().url().is_ok() { - let url = biblio.get(&citekey).unwrap().url().unwrap(); - url + pub fn get_weblink(citekey: &str, biblio: &Bibliography) -> Option { + let bib = biblio.get(&citekey).unwrap(); + if bib.doi().is_ok() { + Some(bib.doi().unwrap()) + } else if bib.url().is_ok() { + Some(bib.url().unwrap()) } else { - let url = "".to_string(); - url + None } } - pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> String { - if let true = biblio.get(&citekey).unwrap().file().is_ok() { - let file = biblio.get(&citekey).unwrap().file().unwrap(); - file + pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> Option { + if biblio.get(&citekey).unwrap().file().is_ok() { + Some(biblio.get(&citekey).unwrap().file().unwrap()) } else { - let file = "".to_string(); - file + None } } diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index 025b696..d8fadcc 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -139,8 +139,8 @@ pub struct EntryTableItem { pub keywords: String, pub citekey: String, pub abstract_text: String, - pub doi_url: String, - pub filepath: String, + pub doi_url: Option, + pub filepath: Option, pub subtitle: Option, } @@ -196,11 +196,11 @@ impl EntryTableItem { } pub fn doi_url(&self) -> &str { - &self.doi_url + &self.doi_url.as_ref().unwrap() } pub fn filepath(&self) -> &str { - &self.filepath + &self.filepath.as_ref().unwrap() } pub fn subtitle(&self) -> &str { @@ -234,8 +234,8 @@ mod tests { keywords: "key1, key2".to_string(), citekey: "miller_2000".to_string(), abstract_text: "An abstract".to_string(), - doi_url: "www.text.org".to_string(), - filepath: "/home/test".to_string(), + doi_url: None, + filepath: None, subtitle: None, }; @@ -250,8 +250,8 @@ mod tests { keywords: "key1, key2".to_string(), citekey: "miller_2000".to_string(), abstract_text: "An abstract".to_string(), - doi_url: "www.text.org".to_string(), - filepath: "/home/test".to_string(), + doi_url: None, + filepath: None, subtitle: None, }; diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 79d1ecc..f2091a5 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -610,16 +610,16 @@ pub fn render_selected_item(app: &mut App, frame: &mut Frame, rect: Rect) { } lines.push(Line::from(content)) } - if !cur_entry.doi_url.is_empty() || !cur_entry.filepath.is_empty() { + if cur_entry.doi_url.is_some() || cur_entry.filepath.is_some() { lines.push(Line::raw("")); } - if !cur_entry.doi_url.is_empty() { + if cur_entry.doi_url.is_some() { lines.push(Line::from(vec![ Span::styled("DOI/URL: ", style_value), Span::styled(cur_entry.doi_url(), INFO_STYLE_DOI.underlined()), ])); } - if !cur_entry.filepath.is_empty() { + if cur_entry.filepath.is_some() { lines.push(Line::from(vec![ Span::styled("File: ", style_value), Span::styled(cur_entry.filepath(), INFO_STYLE_FILE), -- cgit v1.2.3