hpr_motor/openrocket_curves.rs
1//! Which ThrustCurve.org data file holds the curve OpenRocket flies for a `.ork` motor's digest
2//! ([ADR-161](https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0161-openrocket-curves-by-digest.md)):
3//! a `.ork` motor takes that file first, from the bundled catalog or when it is fetched, where
4//! ThrustCurve holds several files of it with different curves
5//! ([ADR-288](https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0288-every-solid-motor-ships-in-the-package.md) ยง3).
6//!
7//! The table, `data/openrocket-curves.json`, is written by `cargo xtask ork-curves`. It covers
8//! the motors of OpenRocket 24.12's examples, and the motor each public flight predicted from a
9//! design file flies
10//! ([ADR-337](https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0337-the-macie-xl-predictions-each-flight-s-stored-simulation-flown-as-saved.md)),
11//! each matched by comparing OpenRocket's curve with every file's; it holds ids alone, no curve. A motor whose digest it lacks takes the bundled
12//! catalog's default file, or is fetched by name
13//! ([ADR-154](https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0154-motors-fetched-from-thrustcurve-by-name.md),
14//! fetching a motor from ThrustCurve.org by its name).
15
16use std::sync::OnceLock;
17
18use serde::Deserialize;
19
20/// The table as committed.
21const TABLE: &str = include_str!("../data/openrocket-curves.json");
22
23#[derive(Deserialize)]
24struct Table {
25 files: Vec<Entry>,
26}
27
28#[derive(Deserialize)]
29struct Entry {
30 digest: String,
31 simfile_id: String,
32}
33
34/// The ThrustCurve id of the file holding OpenRocket's curve for `digest`, if the table has it.
35pub fn file_for(digest: &str) -> Option<String> {
36 static FILES: OnceLock<Vec<Entry>> = OnceLock::new();
37 // The committed table parses (the test below holds it to), so an empty one is never taken.
38 let files = FILES.get_or_init(|| {
39 serde_json::from_str::<Table>(TABLE).map_or_else(|_| Vec::new(), |table| table.files)
40 });
41 files
42 .iter()
43 .find(|entry| entry.digest.eq_ignore_ascii_case(digest))
44 .map(|entry| entry.simfile_id.clone())
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 /// The table parses, names each digest once, and gives the three-stage example's A8 the file
52 /// whose curve is OpenRocket's (the other certification file of the A8 burns 0.2 s shorter).
53 #[test]
54 fn the_table_reads_and_names_openrockets_a8() {
55 let table: Table = serde_json::from_str(TABLE).unwrap();
56 let mut digests: Vec<String> = table
57 .files
58 .iter()
59 .map(|entry| entry.digest.to_ascii_lowercase())
60 .collect();
61 let count = digests.len();
62 digests.sort();
63 digests.dedup();
64 assert_eq!(digests.len(), count);
65 assert!(count >= 28, "{count}");
66 assert_eq!(
67 file_for("22aec01287ea1e3b8c6f66b26fe5fea6").as_deref(),
68 Some("5f4294d20002e900000004e3")
69 );
70 assert_eq!(
71 file_for("F3A785E1523935CAF239C7366CF81FEE").as_deref(),
72 Some("5f4294d20002e90000000897")
73 );
74 assert_eq!(file_for("00000000000000000000000000000000"), None);
75 }
76}