pub(crate) mod assemble;
pub(crate) mod authorship_parser;
pub(crate) mod authorship_split;
pub(crate) mod blacklisted_epithets;
pub(crate) mod code_inference;
pub(crate) mod context;
pub(crate) mod culture_collections;
pub(crate) mod name_tokens;
pub(crate) mod preflight;
pub(crate) mod rank_markers;
pub(crate) mod stripandstash;
pub(crate) use context::ParseContext;
use std::sync::LazyLock;
use regex::Regex;
use crate::model::{
warnings, CombinedAuthorship, NameType, NomCode, ParseError, ParsedName, Rank, State,
};
use crate::pipeline::authorship_parser::AuthState;
use crate::token::tokenize;
use crate::unicode::{java_trim, normalize_quotes};
const MAX_LENGTH: usize = 1000;
const LONG_NAME_LENGTH: usize = 250;
static GLUED_PHRASE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([\p{Lu}][\p{Ll}]+)([\p{Lu}]{2,}[\p{Lu}\d_]*)$").unwrap());
pub fn run(
name: &str,
authorship: Option<&str>,
rank: Option<Rank>,
code: Option<NomCode>,
) -> Result<ParsedName, ParseError> {
let trimmed = java_trim(name);
if trimmed.is_empty() {
return Err(ParseError::new(NameType::Other, None, name));
}
if trimmed.chars().count() > MAX_LENGTH {
return Err(ParseError::new(NameType::Other, None, name));
}
if let Some(a) = authorship {
if java_trim(a).chars().count() > MAX_LENGTH {
return Err(ParseError::new(NameType::Other, None, name));
}
}
let trimmed = normalize_quotes(trimmed);
let authorship = authorship.map(normalize_quotes);
let mut ctx = ParseContext::new(trimmed.clone(), authorship, rank, code);
if trimmed.chars().count() > LONG_NAME_LENGTH {
ctx.name.add_warning(warnings::LONG_NAME);
}
split_glued_phrase_name(&mut ctx);
preflight::run(name, &mut ctx)?;
if ctx.preflight_complete {
return Ok(ctx.name);
}
stripandstash::run(&mut ctx);
if !has_letter(&ctx.working) {
return Err(ParseError::new(NameType::Other, None, name));
}
ctx.tokens = tokenize(&ctx.working);
let boundary = authorship_split::find_boundary(&ctx.tokens, &ctx);
name_tokens::classify(&mut ctx, boundary);
let mut auth_state: Option<AuthState> = None;
if boundary < ctx.tokens.len() {
let st = authorship_parser::parse(&ctx.tokens, boundary);
apply_authorship(&mut ctx.name, &st);
if st.unparsed_from >= 0 {
ctx.name.state = State::Partial;
ctx.name.unparsed = st.unparsed_text.clone();
}
auth_state = Some(st);
}
let mut autonym_state: Option<AuthState> = None;
if ctx.mid_author_from >= 0 && ctx.name.is_autonym() && !ctx.name.has_authorship() {
let from = ctx.mid_author_from as usize;
let to = ctx.mid_author_to as usize;
let st = authorship_parser::parse(&ctx.tokens[from..to], 0);
apply_authorship(&mut ctx.name, &st);
autonym_state = Some(st);
}
let mut extra_state: Option<AuthState> = None;
if let Some(authorship) = ctx.authorship_input.clone() {
if !authorship.chars().all(crate::token::is_whitespace_java) {
let auth_clean = stripandstash::strip_authorship_markers(&authorship, &mut ctx.name);
let aux = tokenize(&auth_clean);
let st = authorship_parser::parse(&aux, 0);
apply_authorship(&mut ctx.name, &st);
if st.sanctioning_author.is_some() {
ctx.name.sanctioning_author = st.sanctioning_author.clone();
}
extra_state = Some(st);
}
}
if let Some(st) = auth_state.as_ref() {
if st.sanctioning_author.is_some() {
ctx.name.sanctioning_author = st.sanctioning_author.clone();
}
}
let mut code_state: Option<&AuthState> = auth_state.as_ref();
if code_state_needs_fallback(code_state)
&& extra_state.as_ref().is_some_and(|st| {
st.basionym_present && (st.basionym.year.is_some() || st.combination.exists())
})
{
code_state = extra_state.as_ref();
}
if code_state_needs_fallback(code_state) && autonym_state.is_some() {
code_state = autonym_state.as_ref();
}
if ctx.pending_year.is_some()
&& !ctx.pending_year_from_publication
&& ctx.name.combination_authorship.exists()
&& ctx.name.combination_authorship.year.is_none()
{
ctx.name.combination_authorship.year = ctx.pending_year.clone();
}
assemble::finish(&mut ctx, code_state);
if ctx.pending_year.is_some()
&& ctx.pending_year_from_publication
&& ctx.name.combination_authorship.exists()
&& ctx.name.combination_authorship.year.is_none()
{
ctx.name.combination_authorship.year = ctx.pending_year.clone();
}
if ctx.pending_imprint_year.is_some() && ctx.name.combination_authorship.imprint_year.is_none()
{
ctx.name.combination_authorship.imprint_year = ctx.pending_imprint_year.clone();
}
if let Some(specific_author) = ctx.pending_specific_author.clone() {
let already = ctx
.name
.specific_authorship
.as_ref()
.is_some_and(CombinedAuthorship::has_authorship);
if !already {
if let Some(ca) = parse_combined_authorship(&specific_author) {
ctx.name.specific_authorship = Some(ca);
}
}
}
if let Some(generic_author) = ctx.pending_generic_author.clone() {
let already = ctx
.name
.generic_authorship
.as_ref()
.is_some_and(CombinedAuthorship::has_authorship);
if !already {
if let Some(ca) = parse_combined_authorship(&generic_author) {
ctx.name.generic_authorship = Some(ca);
}
}
}
Ok(ctx.name)
}
fn apply_authorship(name: &mut ParsedName, st: &AuthState) {
if st.combination.exists() {
name.combination_authorship = st.combination.clone();
}
if st.basionym.exists() {
name.basionym_authorship = st.basionym.clone();
}
}
fn code_state_needs_fallback(state: Option<&AuthState>) -> bool {
match state {
None => true,
Some(s) => !s.combination.exists() && !s.basionym_present,
}
}
fn parse_combined_authorship(authors: &str) -> Option<CombinedAuthorship> {
let tokens = tokenize(authors);
let st = authorship_parser::parse(&tokens, 0);
let mut ca = CombinedAuthorship::default();
if st.combination.exists() {
ca.combination_authorship = st.combination;
}
if st.basionym.exists() {
ca.basionym_authorship = st.basionym;
}
ca.has_authorship().then_some(ca)
}
fn has_letter(s: &str) -> bool {
s.chars().any(|c| c.is_alphabetic())
}
fn split_glued_phrase_name(ctx: &mut ParseContext) {
if ctx.working.contains(' ') {
return;
}
let Some(caps) = GLUED_PHRASE.captures(&ctx.working) else {
return;
};
let prefix = caps.get(1).unwrap().as_str().to_string();
let suffix = caps.get(2).unwrap().as_str().to_string();
ctx.name.phrase = Some(suffix);
ctx.name.type_ = NameType::Informal;
ctx.working = prefix;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_input_is_rejected_as_other() {
let err = run("", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, "");
}
#[test]
fn whitespace_only_input_is_rejected_as_other() {
let err = run(" ", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
}
#[test]
fn name_over_max_length_is_rejected_as_other() {
let long = "a".repeat(1001);
let err = run(&long, None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, long);
}
#[test]
fn name_at_max_length_is_not_rejected_by_the_length_guard() {
let at_limit = "a".repeat(1000);
assert!(run(&at_limit, None, None, None).is_ok());
}
#[test]
fn authorship_over_max_length_is_rejected_as_other_with_name_field() {
let long_authorship = "a".repeat(1001);
let err = run("Abies alba", Some(&long_authorship), None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.name, "Abies alba");
}
#[test]
fn normal_binomial_returns_ok_with_a_seeded_name() {
let pn = run("Abies alba", None, None, None).expect("should parse");
assert_eq!(pn.genus, Some("Abies".to_string()));
assert_eq!(pn.specific_epithet, Some("alba".to_string()));
assert_eq!(pn.rank, Rank::Species);
assert_eq!(pn.code, None);
assert_eq!(pn.type_, NameType::Scientific);
assert_eq!(pn.state, crate::model::State::Complete);
assert!(pn.warnings.is_empty());
}
#[test]
fn requested_rank_and_code_are_seeded_onto_the_returned_name() {
let pn = run(
"Abies alba",
None,
Some(Rank::Species),
Some(NomCode::Botanical),
)
.expect("should parse");
assert_eq!(pn.rank, Rank::Species);
assert_eq!(pn.code, Some(NomCode::Botanical));
}
#[test]
fn long_name_over_250_chars_gets_the_long_name_warning() {
let long = "Abies ".repeat(42);
let pn = run(long.trim(), None, None, None).expect("should parse");
assert!(pn.warnings.contains(&warnings::LONG_NAME.to_string()));
}
#[test]
fn short_name_does_not_get_the_long_name_warning() {
let pn = run("Abies alba", None, None, None).expect("should parse");
assert!(!pn.warnings.contains(&warnings::LONG_NAME.to_string()));
}
#[test]
fn quotes_are_normalized_before_preflight_and_storage() {
assert!(run("Ceylonesmus vector Cham\u{2019}s, 1941", None, None, None).is_ok());
}
#[test]
fn glued_phrase_name_is_split_into_prefix_and_phrase() {
let pn = run("OdontellidaeGEN", None, None, None).expect("should parse");
assert_eq!(pn.phrase, Some("GEN".to_string()));
assert_eq!(pn.type_, NameType::Informal);
}
#[test]
fn glued_phrase_pattern_does_not_fire_when_working_has_a_space() {
let pn = run("Odontellidae GEN", None, None, None).expect("should parse");
assert_eq!(pn.phrase, None);
assert_eq!(pn.type_, NameType::Scientific);
}
#[test]
fn input_with_no_letters_at_all_is_rejected_as_other() {
let err = run("-,.#", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, "-,.#");
}
#[test]
fn single_letter_abbreviation_survives_the_has_letter_guard() {
assert!(run("B.", None, None, None).is_ok());
}
#[test]
fn embedded_authorship_is_applied_to_combination_authorship() {
let pn = run("Abies alba Mill.", None, None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
}
#[test]
fn separately_supplied_authorship_is_applied_to_combination_authorship() {
let pn = run("Abies alba", Some("Mill."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
assert_eq!(pn.genus, Some("Abies".to_string()));
assert_eq!(pn.specific_epithet, Some("alba".to_string()));
}
#[test]
fn separately_supplied_authorship_overwrites_embedded_authorship_when_both_are_given() {
let pn = run("Abies alba Mill.", Some("L."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["L.".to_string()]);
}
#[test]
fn separately_supplied_authorship_runs_through_strip_authorship_markers_first() {
let pn = run("Abies alba", Some("Mill. nom. illeg."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
assert_eq!(pn.nomenclatural_note, Some("nom. illeg.".to_string()));
}
#[test]
fn standalone_manuscript_marker_as_the_whole_separate_authorship_sets_manuscript() {
let pn = run("Abies alba", Some("ined."), None, None).expect("should parse");
assert!(pn.manuscript);
assert!(!pn.combination_authorship.exists());
}
#[test]
fn sanctioning_author_is_last_write_wins_embedded_over_separately_supplied() {
let pn =
run("Boletus versicolor L. : Fr.", Some("X. : Y."), None, None).expect("should parse");
assert_eq!(pn.sanctioning_author, Some("Fr.".to_string()));
}
#[test]
fn code_state_falls_back_to_separately_supplied_authorship_when_embedded_has_none() {
let pn = run("Heptacyclus", Some("(Vasileyev, 1939)"), None, None).expect("should parse");
assert_eq!(pn.code, Some(NomCode::Zoological));
assert_eq!(
pn.basionym_authorship.authors,
vec!["Vasileyev".to_string()]
);
assert_eq!(pn.basionym_authorship.year, Some("1939".to_string()));
}
#[test]
fn autonym_species_author_drives_code_inference_when_no_other_authorship() {
let pn = run(
"Trimezia spathata (Klatt) Baker subsp. spathata",
None,
None,
None,
)
.expect("should parse");
assert!(pn.is_autonym());
assert_eq!(pn.code, Some(NomCode::Botanical));
assert_eq!(pn.combination_authorship.authors, vec!["Baker".to_string()]);
assert_eq!(pn.basionym_authorship.authors, vec!["Klatt".to_string()]);
}
}