Skip to main content

nameparser/model/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2pub mod enums;
3pub use enums::*;
4pub mod name;
5pub use name::*;
6
7/// Java `Enum.name()` — the exact SCREAMING_SNAKE_CASE identifier (e.g. `OTHER`), as opposed to
8/// Rust's `{:?}` (e.g. `Other`). Reuses each wire enum's own `Serialize` impl — which already
9/// carries `#[serde(rename_all = "SCREAMING_SNAKE_CASE")]` — rather than a hand-maintained match,
10/// so the two representations cannot drift apart.
11fn java_name<T: serde::Serialize>(value: &T) -> String {
12    serde_json::to_string(value)
13        .expect("wire enums always serialize to a JSON string")
14        .trim_matches('"')
15        .to_string()
16}
17
18/// Rust equivalent of Java UnparsableNameException (type/code/name/message).
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct ParseError {
21    pub type_: NameType,
22    pub code: Option<NomCode>,
23    pub name: String,
24    pub message: String,
25}
26impl ParseError {
27    pub fn new(type_: NameType, code: Option<NomCode>, name: impl Into<String>) -> Self {
28        let name = name.into();
29        // Java: `"Unparsable " + type + " name: " + name`, where `type` is a NameType and string
30        // concatenation calls its (un-overridden) `Enum.toString()`, i.e. `.name()` — UPPERCASE.
31        let message = format!("Unparsable {} name: {name}", java_name(&type_));
32        Self {
33            type_,
34            code,
35            name,
36            message,
37        }
38    }
39
40    /// Clamp a parsable [`NameType`] to [`NameType::Other`] so this error is legal as a 5.0.0
41    /// `Unparsable` result. The core's error path can still tag an unrepresentable-but-informal
42    /// grouping (`"Bartonella group"`, `"Amauropeltoid clade"`) as `INFORMAL`, but the 5.0.0
43    /// `ParseResult::Unparsable` variant — mirroring Java's, whose record rejects `isParsable()`
44    /// types — may only carry a non-parsable type. Applied ONLY at the 5.0.0 boundaries
45    /// ([`crate::parse`] and the FFI); [`crate::parse_name`] (the raw path, cross-validated
46    /// byte-for-byte against Java 4.2.0) keeps the original `INFORMAL`, so the golden oracles are
47    /// untouched. Rebuilds the message via [`Self::new`] so it names the clamped type.
48    pub fn clamped_to_unparsable(self) -> ParseError {
49        if self.type_.is_parsable() {
50            ParseError::new(NameType::Other, self.code, self.name)
51        } else {
52            self
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn parse_error_message_matches_java_default() {
63        let err = ParseError::new(NameType::Other, None, "BOLD:ACW2100");
64        assert_eq!(err.message, "Unparsable OTHER name: BOLD:ACW2100");
65    }
66
67    /// `model::{NameType, NomCode, NamePart, State, Rank}` and `model::warnings` are the
68    /// documented public interface — reached via the `pub use enums::*;` re-export above, not
69    /// only via `model::enums::…`. Exercised here through fully crate-qualified paths so a future
70    /// refactor of the re-export can't silently narrow the interface without a test failure.
71    #[test]
72    fn wire_enums_and_warnings_are_reachable_at_model_top_level() {
73        assert_eq!(crate::model::warnings::LONG_NAME, "unusually long name");
74        let _: crate::model::NameType = crate::model::NameType::Scientific;
75        let _: crate::model::NomCode = crate::model::NomCode::Zoological;
76        let _: crate::model::NamePart = crate::model::NamePart::Generic;
77        let _: crate::model::State = crate::model::State::Complete;
78        let _: crate::model::Rank = crate::model::Rank::Unranked;
79    }
80
81    /// `model::{Authorship, CombinedAuthorship, ParsedName}` are likewise reached directly
82    /// under `model::`, via the `pub use name::*;` re-export above, not only via
83    /// `model::name::…`.
84    #[test]
85    fn wire_name_structs_are_reachable_at_model_top_level() {
86        let _: crate::model::Authorship = crate::model::Authorship::default();
87        let _: crate::model::CombinedAuthorship = crate::model::CombinedAuthorship::default();
88        let _: crate::model::ParsedName = crate::model::ParsedName::default();
89    }
90}