use std::collections::HashMap;
use std::sync::LazyLock;
use crate::model::Rank;
pub const LETTER_SUBDIVISION: &str = "infrasubdivision";
static INFRASPECIFIC: LazyLock<HashMap<&'static str, Rank>> = LazyLock::new(|| {
HashMap::from([
(LETTER_SUBDIVISION, Rank::Other),
("subsp", Rank::Subspecies),
("ssp", Rank::Subspecies),
("var", Rank::Variety),
("subvar", Rank::Subvariety),
("subv", Rank::Subvariety),
("f", Rank::Form),
("forma", Rank::Form),
("form", Rank::Form),
("fo", Rank::Form),
("subf", Rank::Subform),
("subforma", Rank::Subform),
("pv", Rank::Pathovar),
("pathovar", Rank::Pathovar),
("bv", Rank::Biovar),
("biovar", Rank::Biovar),
("ct", Rank::Chemoform),
("chemoform", Rank::Chemoform),
("sv", Rank::Serovar),
("serovar", Rank::Serovar),
("morph", Rank::Morph),
("morphovar", Rank::Morphovar),
("phagovar", Rank::Phagovar),
("nat", Rank::Natio),
("natio", Rank::Natio),
("mut", Rank::Mutatio),
("mutatio", Rank::Mutatio),
("agamosp", Rank::Species),
("agamossp", Rank::Subspecies),
("agamovar", Rank::Variety),
("conv", Rank::Convariety),
("convar", Rank::Convariety),
("subspec", Rank::Subspecies),
("variety", Rank::Variety),
("fm", Rank::Form),
("fma", Rank::Form),
("prol", Rank::Proles),
("proles", Rank::Proles),
("ab", Rank::Aberration),
("aberration", Rank::Aberration),
("strain", Rank::Strain),
("str", Rank::Strain),
("sssp", Rank::Subspecies),
("lusus", Rank::Lusus),
("lus", Rank::Lusus),
("microg", Rank::InfraspecificName),
("st", Rank::InfraspecificName),
("*", Rank::InfraspecificName),
])
});
static INFRAGENERIC: LazyLock<HashMap<&'static str, Rank>> = LazyLock::new(|| {
HashMap::from([
("div", Rank::DivisionBotany),
("divisio", Rank::DivisionBotany),
("subg", Rank::Subgenus),
("subgen", Rank::Subgenus),
("sect", Rank::SectionBotany),
("subsect", Rank::SubsectionBotany),
("supersect", Rank::SupersectionBotany),
("suprasect", Rank::SupersectionBotany),
("ser", Rank::SeriesBotany),
("subser", Rank::SubseriesBotany),
("tr", Rank::InfragenericName),
("subtr", Rank::InfragenericName),
("unr", Rank::InfragenericName),
])
});
pub fn match_infraspecific(word: &str) -> Option<Rank> {
INFRASPECIFIC.get(word.to_lowercase().as_str()).copied()
}
pub fn match_infrageneric(word: &str) -> Option<Rank> {
INFRAGENERIC.get(word.to_lowercase().as_str()).copied()
}
pub fn match_infrageneric_allow_notho(word: &str) -> Option<(Rank, bool)> {
let w = word.to_lowercase();
if let Some(rest) = w.strip_prefix("notho") {
if let Some(&r) = INFRAGENERIC.get(rest) {
return Some((r, true));
}
}
INFRAGENERIC.get(w.as_str()).map(|&r| (r, false))
}
pub fn match_infraspecific_allow_notho(word: &str) -> Option<(Rank, bool)> {
let w = word.to_lowercase();
if let Some(rest) = w.strip_prefix("notho") {
if let Some(&r) = INFRASPECIFIC.get(rest) {
return Some((r, true));
}
}
if w.len() > 1 && w.starts_with('n') {
if let Some(&r) = INFRASPECIFIC.get(&w[1..]) {
return Some((r, true));
}
}
INFRASPECIFIC.get(w.as_str()).map(|&r| (r, false))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn subsp_matches_subspecies() {
assert_eq!(match_infraspecific("subsp"), Some(Rank::Subspecies));
assert_eq!(
match_infraspecific_allow_notho("subsp"),
Some((Rank::Subspecies, false))
);
}
#[test]
fn var_matches_variety() {
assert_eq!(match_infraspecific("var"), Some(Rank::Variety));
}
#[test]
fn f_and_forma_both_match_form_java_has_no_forma_rank_constant() {
assert_eq!(match_infraspecific("f"), Some(Rank::Form));
assert_eq!(match_infraspecific("forma"), Some(Rank::Form));
assert_eq!(match_infraspecific("form"), Some(Rank::Form));
assert_eq!(match_infraspecific("fo"), Some(Rank::Form));
}
#[test]
fn sect_matches_section_botany() {
assert_eq!(match_infrageneric("sect"), Some(Rank::SectionBotany));
}
#[test]
fn subg_and_subgen_both_match_subgenus() {
assert_eq!(match_infrageneric("subg"), Some(Rank::Subgenus));
assert_eq!(match_infrageneric("subgen"), Some(Rank::Subgenus));
}
#[test]
fn nothovar_strips_the_notho_prefix_and_flags_it() {
assert_eq!(
match_infraspecific_allow_notho("nothovar"),
Some((Rank::Variety, true))
);
}
#[test]
fn notho_prefix_also_recognised_for_infrageneric_markers() {
assert_eq!(
match_infrageneric_allow_notho("nothosect"),
Some((Rank::SectionBotany, true))
);
assert_eq!(match_infrageneric_allow_notho("nothoxyz"), None);
}
#[test]
fn short_n_prefix_is_infraspecific_only_and_notho_flagged() {
assert_eq!(
match_infraspecific_allow_notho("nvar"),
Some((Rank::Variety, true))
);
assert_eq!(
match_infraspecific_allow_notho("nf"),
Some((Rank::Form, true))
);
assert_eq!(match_infrageneric_allow_notho("nsect"), None);
}
#[test]
fn case_insensitive_matching() {
assert_eq!(match_infraspecific("SUBSP"), Some(Rank::Subspecies));
assert_eq!(match_infrageneric("Sect"), Some(Rank::SectionBotany));
assert_eq!(
match_infraspecific_allow_notho("NOTHOVAR"),
Some((Rank::Variety, true))
);
}
#[test]
fn unrecognised_marker_returns_none() {
assert_eq!(match_infraspecific("xyz"), None);
assert_eq!(match_infrageneric("xyz"), None);
assert_eq!(match_infraspecific_allow_notho("xyz"), None);
assert_eq!(match_infrageneric_allow_notho("xyz"), None);
}
#[test]
fn trailing_dot_is_not_stripped_by_this_module_the_caller_must_do_it() {
assert_eq!(match_infraspecific("subsp."), None);
assert_eq!(match_infraspecific_allow_notho("var."), None);
assert_eq!(match_infrageneric("sect."), None);
}
#[test]
fn letter_subdivision_synthetic_marker_maps_to_rank_other() {
assert_eq!(match_infraspecific(LETTER_SUBDIVISION), Some(Rank::Other));
}
#[test]
fn star_is_the_old_infraspecific_separator() {
assert_eq!(match_infraspecific("*"), Some(Rank::InfraspecificName));
}
#[test]
fn division_botany_marker_and_its_suprasect_alias() {
assert_eq!(match_infrageneric("div"), Some(Rank::DivisionBotany));
assert_eq!(match_infrageneric("divisio"), Some(Rank::DivisionBotany));
assert_eq!(
match_infrageneric("supersect"),
Some(Rank::SupersectionBotany)
);
assert_eq!(
match_infrageneric("suprasect"),
Some(Rank::SupersectionBotany)
);
}
#[test]
fn microbial_and_legacy_infraspecific_markers() {
assert_eq!(match_infraspecific("pv"), Some(Rank::Pathovar));
assert_eq!(match_infraspecific("biovar"), Some(Rank::Biovar));
assert_eq!(match_infraspecific("ct"), Some(Rank::Chemoform));
assert_eq!(match_infraspecific("sv"), Some(Rank::Serovar));
assert_eq!(match_infraspecific("agamosp"), Some(Rank::Species));
assert_eq!(match_infraspecific("agamossp"), Some(Rank::Subspecies));
assert_eq!(match_infraspecific("agamovar"), Some(Rank::Variety));
assert_eq!(match_infraspecific("strain"), Some(Rank::Strain));
assert_eq!(match_infraspecific("str"), Some(Rank::Strain));
assert_eq!(match_infraspecific("st"), Some(Rank::InfraspecificName));
}
}