Skip to main content

oxihuman_export/
makehuman_export.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5pub struct MhMorphParam {
6    pub name: String,
7    pub value: f32,
8}
9
10pub struct MhExport {
11    pub version: String,
12    pub params: Vec<MhMorphParam>,
13}
14
15pub fn new_mh_export() -> MhExport {
16    MhExport {
17        version: "1.2.0".to_string(),
18        params: Vec::new(),
19    }
20}
21
22pub fn mh_push_param(e: &mut MhExport, name: &str, value: f32) {
23    e.params.push(MhMorphParam {
24        name: name.to_string(),
25        value,
26    });
27}
28
29pub fn mh_to_mhm_string(e: &MhExport) -> String {
30    let mut lines = vec![format!("version {}", e.version)];
31    for p in &e.params {
32        lines.push(format!("{} {}", p.name, p.value));
33    }
34    lines.join("\n")
35}
36
37pub fn mh_param_count(e: &MhExport) -> usize {
38    e.params.len()
39}
40
41pub fn mh_find_param(e: &MhExport, name: &str) -> Option<f32> {
42    e.params.iter().find(|p| p.name == name).map(|p| p.value)
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_new_mh_export_empty() {
51        /* starts empty */
52        let e = new_mh_export();
53        assert_eq!(mh_param_count(&e), 0);
54    }
55
56    #[test]
57    fn test_mh_push_param() {
58        /* push param */
59        let mut e = new_mh_export();
60        mh_push_param(&mut e, "macrodetails/Age", 0.5);
61        assert_eq!(mh_param_count(&e), 1);
62    }
63
64    #[test]
65    fn test_mh_find_param_found() {
66        /* find existing param */
67        let mut e = new_mh_export();
68        mh_push_param(&mut e, "Age", 0.7);
69        let v = mh_find_param(&e, "Age");
70        assert!((v.expect("should succeed") - 0.7).abs() < 1e-6);
71    }
72
73    #[test]
74    fn test_mh_find_param_none() {
75        /* missing param */
76        let e = new_mh_export();
77        assert!(mh_find_param(&e, "nonexistent").is_none());
78    }
79
80    #[test]
81    fn test_mh_to_mhm_contains_version() {
82        /* mhm string contains version */
83        let e = new_mh_export();
84        let s = mh_to_mhm_string(&e);
85        assert!(s.contains("version"));
86    }
87}