use crate::model::{NomCode, ParsedName, Rank};
use crate::token::Token;
#[allow(dead_code)]
pub(crate) struct ParseContext {
pub original: String,
pub authorship_input: Option<String>,
pub requested_rank: Option<Rank>,
pub requested_code: Option<NomCode>,
pub working: String,
pub tokens: Vec<Token>,
pub name: ParsedName,
pub pending_unparsed: Option<String>,
pub pending_unparsed_trailing_tag: bool,
pub page_strip_verbatim: Option<String>,
pub aggregate: bool,
pub viral_shape: bool,
pub pending_year: Option<String>,
pub pending_year_from_publication: bool,
pub quoted_monomial: Option<String>,
pub mid_author_from: i32,
pub mid_author_to: i32,
pub pending_imprint_year: Option<String>,
pub pending_specific_author: Option<String>,
pub pending_generic_author: Option<String>,
pub preflight_complete: bool,
}
impl ParseContext {
pub(crate) fn new(
original: String,
authorship_input: Option<String>,
requested_rank: Option<Rank>,
requested_code: Option<NomCode>,
) -> Self {
let name = ParsedName {
rank: requested_rank.unwrap_or(Rank::Unranked),
code: requested_code,
..ParsedName::default()
};
ParseContext {
working: original.clone(),
original,
authorship_input,
requested_rank,
requested_code,
tokens: Vec::new(),
name,
pending_unparsed: None,
pending_unparsed_trailing_tag: false,
page_strip_verbatim: None,
aggregate: false,
viral_shape: false,
pending_year: None,
pending_year_from_publication: false,
quoted_monomial: None,
mid_author_from: -1,
mid_author_to: -1,
pending_imprint_year: None,
pending_specific_author: None,
pending_generic_author: None,
preflight_complete: false,
}
}
pub(crate) fn set_pending_imprint_year(&mut self, year: &str) {
if self.pending_imprint_year.is_none() {
self.pending_imprint_year = Some(year.to_string());
}
}
pub(crate) fn set_pending_unparsed(&mut self, remainder: &str) {
if self.pending_unparsed.is_none()
&& !remainder.chars().all(crate::token::is_whitespace_java)
{
self.pending_unparsed = Some(remainder.to_string());
}
}
pub(crate) fn set_pending_publication_year(&mut self, year: &str) {
if self.pending_year.is_none() {
self.pending_year = Some(year.to_string());
self.pending_year_from_publication = true;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{NameType, State};
#[test]
fn constructor_seeds_name_rank_code_type_and_state() {
let ctx = ParseContext::new(
"Abies alba".to_string(),
None,
Some(Rank::Species),
Some(NomCode::Botanical),
);
assert_eq!(ctx.name.rank, Rank::Species);
assert_eq!(ctx.name.code, Some(NomCode::Botanical));
assert_eq!(ctx.name.type_, NameType::Scientific);
assert_eq!(ctx.name.state, State::Complete);
assert_eq!(ctx.working, "Abies alba");
assert_eq!(ctx.original, "Abies alba");
}
#[test]
fn constructor_folds_missing_rank_to_unranked_but_leaves_code_unset() {
let ctx = ParseContext::new("Abies".to_string(), None, None, None);
assert_eq!(ctx.name.rank, Rank::Unranked);
assert_eq!(ctx.name.code, None);
}
#[test]
fn mid_author_range_defaults_to_minus_one() {
let ctx = ParseContext::new("Abies".to_string(), None, None, None);
assert_eq!(ctx.mid_author_from, -1);
assert_eq!(ctx.mid_author_to, -1);
}
#[test]
fn authorship_input_is_threaded_through() {
let ctx = ParseContext::new(
"Abies alba".to_string(),
Some("Mill.".to_string()),
None,
None,
);
assert_eq!(ctx.authorship_input, Some("Mill.".to_string()));
}
#[test]
fn set_pending_imprint_year_is_first_writer_wins() {
let mut ctx = ParseContext::new("Abies alba".to_string(), None, None, None);
ctx.set_pending_imprint_year("1969");
assert_eq!(ctx.pending_imprint_year, Some("1969".to_string()));
ctx.set_pending_imprint_year("1875");
assert_eq!(
ctx.pending_imprint_year,
Some("1969".to_string()),
"the first-written year must win, matching Java's null-guarded setter"
);
}
#[test]
fn set_pending_unparsed_is_first_writer_wins_and_ignores_blank() {
let mut ctx = ParseContext::new("Abies alba".to_string(), None, None, None);
ctx.set_pending_unparsed(" ");
assert_eq!(
ctx.pending_unparsed, None,
"a blank remainder must be ignored, matching Java's isBlank() guard"
);
ctx.set_pending_unparsed("XXZ_21243");
assert_eq!(ctx.pending_unparsed, Some("XXZ_21243".to_string()));
ctx.set_pending_unparsed("(sic,foo)");
assert_eq!(
ctx.pending_unparsed,
Some("XXZ_21243".to_string()),
"the first non-blank write must win"
);
}
#[test]
fn set_pending_publication_year_is_first_writer_wins_and_marks_code_neutral() {
let mut ctx = ParseContext::new("Abies alba".to_string(), None, None, None);
assert_eq!(ctx.pending_year, None);
assert!(!ctx.pending_year_from_publication);
ctx.set_pending_publication_year("1988");
assert_eq!(ctx.pending_year, Some("1988".to_string()));
assert!(
ctx.pending_year_from_publication,
"a publication-derived year must be marked code-neutral"
);
ctx.set_pending_publication_year("1900");
assert_eq!(
ctx.pending_year,
Some("1988".to_string()),
"the first-written year must win, matching Java's null-guarded setter"
);
}
}