Skip to main content

hpr_core/
tool.rs

1//! The program every file and document FusionSpace HPR writes names, as a drawing's title block names
2//! the tool that made it: its name, its version and its designation in the FusionSpace product
3//! system ([ADR-164][adr-164], which adopts that system; its `data.md`: "Every export carries the
4//! designation and version of the tool that made it").
5//!
6//! [adr-164]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0164-the-fusionspace-product-system.md
7//!
8//! The writers put these three in whatever place their format keeps for it: a `.eng` file's
9//! comment, an XML comment or attribute, a JSON object, Parquet's key-value metadata.
10
11/// The program's name: `FusionSpace HPR`, the suite's public name ([ADR-199][adr-199] §4).
12///
13/// [adr-199]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0199-the-2026-10-07-fusionspace-hpr.md
14pub const NAME: &str = "FusionSpace HPR";
15
16/// The name earlier builds wrote, which readers still recognize: `hpr-sim`, written by every
17/// build until [ADR-199, the public name][adr-199], renamed the program, with either designation
18/// (builds after [ADR-198, project Eridanus][adr-198], wrote the new designation under the old
19/// name). Nothing writes it.
20///
21/// [adr-198]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0198-the-2026-10-07-project-eridanus.md
22/// [adr-199]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0199-the-2026-10-07-fusionspace-hpr.md
23pub const EARLIER_NAME: &str = "hpr-sim";
24
25/// Its version, the workspace's: `0.1.0` until the first release changes it.
26pub const VERSION: &str = env!("CARGO_PKG_VERSION");
27
28/// Its designation in the FusionSpace product system, `<code> · <tag> · <kind> <number>` in the
29/// system's grammar: the simulator's internal name, Achernar (`FS-ACHERNAR`, a star of project
30/// Eridanus), then software tool 1 ([ADR-198][adr-198] §4).
31///
32/// [adr-198]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0198-the-2026-10-07-project-eridanus.md
33pub const DESIGNATION: &str = "FS-ACHERNAR · SW · TOOL 001";
34
35/// The designations earlier builds wrote, which readers still recognize: `FS · SW · TOOL 005`,
36/// written by every build from the command line's restyle until [ADR-198][adr-198] retired it.
37/// Nothing writes these.
38///
39/// [adr-198]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0198-the-2026-10-07-project-eridanus.md
40pub const EARLIER_DESIGNATIONS: [&str; 1] = ["FS · SW · TOOL 005"];
41
42/// The three on one line, `FusionSpace HPR 0.1.0 · FS-ACHERNAR · SW · TOOL 001`, for a format that
43/// keeps one line of text for it: the external name first, as the system's naming rule asks.
44pub fn stamp() -> String {
45    format!("{NAME} {VERSION} · {DESIGNATION}")
46}
47
48/// Whether `text` is a stamp this program wrote, at this version or any other: a name, a space,
49/// and a version (starting with a digit, as every Cargo version does), then ` · ` and a
50/// designation at the end. Surrounding white space is ignored. The pairs recognized are the ones
51/// some build wrote: [`NAME`] with [`DESIGNATION`], and [`EARLIER_NAME`] with [`DESIGNATION`] or
52/// an [earlier designation](EARLIER_DESIGNATIONS).
53#[must_use]
54pub fn is_stamp(text: &str) -> bool {
55    let text = text.trim();
56    let written: [(&str, &[&str]); 2] = [
57        (NAME, &[DESIGNATION]),
58        (EARLIER_NAME, &[DESIGNATION, EARLIER_DESIGNATIONS[0]]),
59    ];
60    written.iter().any(|(name, designations)| {
61        let Some(rest) = text
62            .strip_prefix(name)
63            .and_then(|rest| rest.strip_prefix(' '))
64        else {
65            return false;
66        };
67        designations.iter().any(|designation| {
68            rest.strip_suffix(designation)
69                .and_then(|version| version.strip_suffix(" · "))
70                .is_some_and(|version| {
71                    version.starts_with(|c: char| c.is_ascii_digit())
72                        && !version.contains(char::is_whitespace)
73                })
74        })
75    })
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    /// The line names all three, in the order a title block reads, the external name first.
83    #[test]
84    fn the_stamp_reads_name_version_designation() {
85        assert_eq!(
86            stamp(),
87            format!(
88                "FusionSpace HPR {} · FS-ACHERNAR · SW · TOOL 001",
89                env!("CARGO_PKG_VERSION")
90            )
91        );
92        assert!(is_stamp(&stamp()));
93        assert!(!stamp().contains("hpr-sim"));
94    }
95
96    /// ADR-199 §4: a stamp under the old name, `hpr-sim`, with either designation, is still
97    /// recognized, as is the new name with the current designation; the new name with the
98    /// retired designation was never written, so it isn't one.
99    #[test]
100    fn stamps_under_the_earlier_name_are_recognized() {
101        assert!(is_stamp("hpr-sim 0.1.0 · FS-ACHERNAR · SW · TOOL 001"));
102        assert!(is_stamp("hpr-sim 0.1.0 · FS · SW · TOOL 005"));
103        assert!(is_stamp(
104            "FusionSpace HPR 0.1.0 · FS-ACHERNAR · SW · TOOL 001"
105        ));
106        assert!(is_stamp(
107            " FusionSpace HPR 2.0.0-rc.1 · FS-ACHERNAR · SW · TOOL 001 "
108        ));
109        assert_ne!(NAME, EARLIER_NAME);
110        for text in [
111            "FusionSpace HPR 0.1.0 · FS · SW · TOOL 005",
112            "FusionSpace HPR · FS-ACHERNAR · SW · TOOL 001",
113            "FusionSpace  HPR 0.1.0 · FS-ACHERNAR · SW · TOOL 001",
114            "FusionSpace HPR0.1.0 · FS-ACHERNAR · SW · TOOL 001",
115            "fusionspace-hpr 0.1.0 · FS-ACHERNAR · SW · TOOL 001",
116            "FusionSpace 0.1.0 · FS-ACHERNAR · SW · TOOL 001",
117            "HPR 0.1.0 · FS-ACHERNAR · SW · TOOL 001",
118            "FusionSpace HPR x · FS-ACHERNAR · SW · TOOL 001",
119            "FusionSpace HPR 0.1.0 · FS-ACHERNAR · SW · TOOL 001, edited",
120        ] {
121            assert!(!is_stamp(text), "{text}");
122        }
123    }
124
125    /// A stamp with this designation or the retired one, from any version, is recognized; the
126    /// retired designation is never written; and a text that only looks like a stamp isn't one.
127    #[test]
128    fn stamps_with_the_earlier_designation_are_recognized() {
129        assert!(is_stamp("hpr-sim 0.1.0 · FS · SW · TOOL 005"));
130        assert!(is_stamp("  hpr-sim 0.0.9 · FS · SW · TOOL 005 "));
131        assert!(is_stamp("hpr-sim 9.9 · FS-ACHERNAR · SW · TOOL 001"));
132        assert!(!stamp().contains("TOOL 005"));
133        assert!(!EARLIER_DESIGNATIONS.contains(&DESIGNATION));
134        for text in [
135            "FS · SW · TOOL 005",
136            "FS-ACHERNAR · SW · TOOL 001",
137            "hpr-sim · FS · SW · TOOL 005",
138            "hpr-sim  · FS-ACHERNAR · SW · TOOL 001",
139            "hpr-simulator 0.1.0 · FS · SW · TOOL 005",
140            "hpr-sim 0.1.0 FS · SW · TOOL 005",
141            "hpr-sim 2 · 3 s burn · FS · SW · TOOL 005",
142            "hpr-sim 0.1.0 · FS · SW · TOOL 006",
143            "hpr-sim 0.1.0 · FS · SW · TOOL 005, edited",
144            "a note by hpr-sim 0.1.0 · FS · SW · TOOL 005",
145            "hpr-sim x · FS · SW · TOOL 005",
146            "hpr-sim says: · FS-ACHERNAR · SW · TOOL 001",
147        ] {
148            assert!(!is_stamp(text), "{text}");
149        }
150    }
151}