diff options
| author | lukeflo | 2025-10-08 14:39:46 +0200 |
|---|---|---|
| committer | lukeflo | 2025-10-08 14:39:46 +0200 |
| commit | 8b858f92da69cfb8fa43ec861cda46eeb6ef4bbe (patch) | |
| tree | 920001c7455e1e2e2ce41324fdaddb1fe5aed920 | |
| parent | a07359a9a1da0c06c040f77158be31b3883b33ac (diff) | |
| download | bibiman-8b858f92da69cfb8fa43ec861cda46eeb6ef4bbe.tar.gz bibiman-8b858f92da69cfb8fa43ec861cda46eeb6ef4bbe.zip | |
case parsing from config, needs to be implemented for citekey struct
| -rw-r--r-- | src/bibiman/citekeys.rs | 95 | ||||
| -rw-r--r-- | src/config.rs | 8 |
2 files changed, 49 insertions, 54 deletions
diff --git a/src/bibiman/citekeys.rs b/src/bibiman/citekeys.rs index a304e92..118ae3e 100644 --- a/src/bibiman/citekeys.rs +++ b/src/bibiman/citekeys.rs @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize}; use crate::{bibiman::sanitize::sanitize_single_string_fully, config::BibiConfig}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub(crate) enum CitekeyCase { +pub enum CitekeyCase { #[serde(alias = "uppercase", alias = "upper")] Upper, #[serde(alias = "lowercase", alias = "lower")] @@ -44,7 +44,11 @@ impl CitekeyFormatting { /// The `Bibliography` inserted will be edited in place with the new citekeys. /// Thus, in the end the `bib_entries` field will hold the updated `Bibliography` pub fn new(cfg: &BibiConfig, bib_entries: Bibliography) -> color_eyre::Result<Self> { - let fields = cfg.citekey_formatter.fields.clone(); + let fields = cfg + .citekey_formatter + .fields + .clone() + .expect("Need to define fields in config to format citekeys"); if fields.is_empty() { return Err(eyre!( "To format all citekeys, you need to provide {} values in the config file", @@ -58,65 +62,54 @@ impl CitekeyFormatting { }) } + /// Process the actual formatting. The citekey of every entry will be updated. pub fn do_formatting(&mut self) { for entry in self.bib_entries.iter_mut() { - let mut new_citekey = String::new(); - for pattern in self.fields.iter() { - let (field, word_count, char_count, inner_delimiter, trailing_delimiter) = - split_formatting_pat(pattern); - let formatted_field_str = { - let mut formatted_str = String::new(); - let field = preformat_field(field, entry); - // let field = if let Ok(val) = entry.get_as::<String>(field) { - // val - // } else { - // eprintln!( - // "Unable to get field {} for entry {}", - // field.bright_red(), - // &entry.key.bold() - // ); - // continue; - // }; - // let field = entry.get_as::<String>(field).expect(&format!( - // "Couldn't find field {}", - // field.bold().bright_red() - // )); - let mut split_field = field.split_whitespace(); - let mut words_passed = 0; - let word_count = if let Some(val) = word_count { - val + entry.key = build_citekey(entry, &self.fields); + } + } +} + +/// Build the citekey from the patterns defined in the config file +fn build_citekey(entry: &Entry, pattern_fields: &[String]) -> String { + let mut new_citekey = String::new(); + for pattern in pattern_fields.iter() { + let (field, word_count, char_count, inner_delimiter, trailing_delimiter) = + split_formatting_pat(pattern); + let formatted_field_str = { + let mut formatted_str = String::new(); + let field = preformat_field(field, entry); + let mut split_field = field.split_whitespace(); + let mut words_passed = 0; + let word_count = if let Some(val) = word_count { + val + } else { + field.split_whitespace().count() + }; + loop { + if let Some(field_slice) = split_field.next() { + formatted_str = formatted_str + format_word(field_slice, char_count); + words_passed += 1; + if word_count == words_passed { + formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); + break; } else { - field.split_whitespace().count() - // split_field.size_hint().0 + 1 - }; - dbg!(word_count); - loop { - if let Some(field_slice) = split_field.next() { - formatted_str = formatted_str + format_word(field_slice, char_count); - words_passed += 1; - // if word_count.is_some_and(|count| count == words_passed) { - if word_count == words_passed { - formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); - break; - } else { - formatted_str = formatted_str + inner_delimiter.unwrap_or("") - } - } else { - formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); - break; - }; + formatted_str = formatted_str + inner_delimiter.unwrap_or("") } - formatted_str + } else { + formatted_str = formatted_str + trailing_delimiter.unwrap_or(""); + break; }; - new_citekey = new_citekey + &formatted_field_str; } - entry.key = new_citekey; - } + formatted_str + }; + new_citekey = new_citekey + &formatted_field_str; } + new_citekey } /// Preformat some fields which are very common to be used in citekeys -fn preformat_field(field: &str, entry: &mut Entry) -> String { +fn preformat_field(field: &str, entry: &Entry) -> String { match field { "title" => { sanitize_single_string_fully(&entry.get_as::<String>(field).unwrap_or("NA".into())) diff --git a/src/config.rs b/src/config.rs index 8a333e4..a5df61c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -149,7 +149,7 @@ pub struct Colors { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct CitekeyFormatter { - pub fields: Vec<String>, + pub fields: Option<Vec<String>>, pub case: Option<CitekeyCase>, } @@ -172,7 +172,7 @@ impl Default for BibiConfig { }, colors: Self::dark_colors(), citekey_formatter: CitekeyFormatter { - fields: Vec::new(), + fields: None, case: None, }, } @@ -202,7 +202,7 @@ impl BibiConfig { Self::dark_colors() }, citekey_formatter: CitekeyFormatter { - fields: Vec::new(), + fields: None, case: None, }, } @@ -400,6 +400,8 @@ mod tests { author_color = "38" title_color = "37" year_color = "135" + + [citekey_formatter] "#, )?; |
