use super::mt_data::MortData;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
const ELT15_F_XML: &str = include_str!("../../data/elt15_f.xml");
const ELT15_M_XML: &str = include_str!("../../data/elt15_m.xml");
pub static BUILTIN_MORT_DATA: Lazy<FxHashMap<&'static str, MortData>> = Lazy::new(|| {
let mut map = FxHashMap::default();
for id in ["AM92", "AF92", "PFA92", "PMA92"] {
match MortData::from_ifoa_url_id(id) {
Ok(data) => {
map.insert(id, data);
}
Err(e) => panic!("Failed to preload builtin table {id}: {e}"),
}
}
for id in ["PFA92C10", "PMA92C10", "PFA92C20", "PMA92C20"] {
match MortData::from_ifoa_custom(id) {
Ok(data) => {
map.insert(id, data);
}
Err(e) => panic!("Failed to preload builtin table {id}: {e}"),
}
}
for (key, xml_str) in [("ELT15_F", ELT15_F_XML), ("ELT15_M", ELT15_M_XML)] {
match MortData::from_soa_xml_string(xml_str) {
Ok(data) => {
map.insert(key, data);
}
Err(e) => panic!("Failed to preload builtin table {key}: {e}"),
}
}
for id in ["SULT"] {
match MortData::from_soa_custom(id) {
Ok(data) => {
map.insert(id, data);
}
Err(e) => panic!("Failed to preload builtin table {id}: {e}"),
}
}
map
});
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builtin_am92_matches_url_download() {
let builtin_data = BUILTIN_MORT_DATA
.get("AM92")
.expect("AM92 should be in builtin cache");
let url_data =
MortData::from_ifoa_url_id("AM92").expect("Failed to download AM92 from URL");
let builtin_df = &builtin_data.dataframe;
let url_df = &url_data.dataframe;
assert_eq!(
builtin_df.height(),
url_df.height(),
"Row count mismatch between builtin and URL AM92"
);
assert_eq!(
builtin_df.get_column_names(),
url_df.get_column_names(),
"Column names mismatch between builtin and URL AM92"
);
let builtin_qx = builtin_df.column("qx").unwrap().f64().unwrap();
let url_qx = url_df.column("qx").unwrap().f64().unwrap();
for i in 0..builtin_qx.len() {
let b_val = builtin_qx.get(i);
let u_val = url_qx.get(i);
match (b_val, u_val) {
(None, None) => continue,
(Some(b), Some(u)) => {
if b.is_nan() && u.is_nan() {
continue;
}
assert!(
(b - u).abs() < 1e-10,
"qx mismatch at row {}: builtin={}, URL={}",
i,
b,
u
);
}
_ => panic!(
"qx mismatch at row {}: builtin={:?}, URL={:?}",
i, b_val, u_val
),
}
}
}
}