qualia_client_core/wellfair/
body_constitution.rs1use std::path::Path;
7
8use wellfare_core::anatomy::{BodyConstitution, BodyFit, PhysiologicalState};
9
10pub const CONSTITUTION_FILE: &str = "wellfair/body_constitution.json";
12
13pub fn load(storage_root: impl AsRef<Path>) -> Option<BodyConstitution> {
15 let path = storage_root.as_ref().join(CONSTITUTION_FILE);
16 let bytes = std::fs::read(path).ok()?;
17 serde_json::from_slice(&bytes).ok()
18}
19
20pub fn save(storage_root: impl AsRef<Path>, body: &BodyConstitution) -> Result<(), String> {
22 body.validate()?;
23 let path = storage_root.as_ref().join(CONSTITUTION_FILE);
24 if let Some(parent) = path.parent() {
25 std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
26 }
27 let bytes = serde_json::to_vec_pretty(body).map_err(|e| e.to_string())?;
28 std::fs::write(path, bytes).map_err(|e| e.to_string())
29}
30
31pub fn clear(storage_root: impl AsRef<Path>) -> Result<(), String> {
33 let path = storage_root.as_ref().join(CONSTITUTION_FILE);
34 match std::fs::remove_file(path) {
35 Ok(()) => Ok(()),
36 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
37 Err(e) => Err(e.to_string()),
38 }
39}
40
41pub fn fit_json_for_reference(model: &str, constitution_json: &str) -> Result<(Vec<u8>, Vec<u8>), String> {
47 use wellfare_core::anatomy::Karyotype;
48 let mut constitution: BodyConstitution = if constitution_json.trim().is_empty() {
49 BodyConstitution::default()
50 } else {
51 serde_json::from_str(constitution_json).map_err(|e| format!("constitution: {e}"))?
52 };
53 if constitution.characteristics.karyotype.is_none() {
54 constitution.characteristics.karyotype = Some(match model {
55 "female" => Karyotype::Xx,
56 "male" => Karyotype::Xy,
57 _ => return Err("model must be male or female".into()),
58 });
59 }
60 constitution.validate()?;
61 let fit = constitution.fit();
62 let fit_bytes = serde_json::to_vec_pretty(&fit).map_err(|e| e.to_string())?;
63 let constitution_bytes = serde_json::to_vec_pretty(&constitution).map_err(|e| e.to_string())?;
64 Ok((fit_bytes, constitution_bytes))
65}
66
67pub fn fit_for(constitution: &BodyConstitution, phys: &PhysiologicalState) -> BodyFit {
70 let pregnancy = match phys {
71 PhysiologicalState::Reproductive(wellfare_core::anatomy::ReproductiveState::Pregnant(
72 t,
73 )) => Some(*t),
74 _ => constitution.attributes.pregnancy,
75 };
76 constitution.fit_with_pregnancy(pregnancy)
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use wellfare_core::anatomy::{Karyotype, ReproductiveState};
83
84 #[test]
85 fn persists_and_clears() {
86 let dir = tempfile::tempdir().unwrap();
87 assert!(load(dir.path()).is_none());
88 let mut c = BodyConstitution::default();
89 c.measurements.stature_mm = Some(1720);
90 c.characteristics.karyotype = Some(Karyotype::Xx);
91 c.knowledge
92 .ethnicities
93 .push(wellfare_core::anatomy::EthnicityAffiliation::declared("Ashkenazi").unwrap());
94 c.attributes.eye_colour = Some("brown".into());
95 c.measurements.sleeve_mm = Some(610);
96 c.measurements.foot_left_mm = Some(265);
97 save(dir.path(), &c).unwrap();
98 let back = load(dir.path()).unwrap();
99 assert_eq!(back.measurements.stature_mm, Some(1720));
100 assert_eq!(back.knowledge.ethnicities.len(), 1);
101 assert_eq!(back.knowledge.ethnicities[0].token, "ashkenazi");
102 assert_eq!(back.attributes.eye_colour.as_deref(), Some("brown"));
103 assert_eq!(back.measurements.sleeve_mm, Some(610));
104 assert_eq!(back.measurements.foot_left_mm, Some(265));
105 clear(dir.path()).unwrap();
106 assert!(load(dir.path()).is_none());
107 }
108
109 #[test]
110 fn save_rejects_invalid() {
111 let dir = tempfile::tempdir().unwrap();
112 let mut c = BodyConstitution::default();
113 c.measurements.stature_mm = Some(10);
114 assert!(save(dir.path(), &c).is_err());
115 }
116
117 #[test]
118 fn phys_pregnancy_drives_fit() {
119 let c = BodyConstitution::default();
120 let phys = PhysiologicalState::Reproductive(ReproductiveState::Pregnant(
121 wellfare_core::anatomy::Trimester::Third,
122 ));
123 let fit = fit_for(&c, &phys);
124 assert!(fit.pregnancy_abdomen > 0.3);
125 }
126}