Skip to main content

fmi_schema/fmi2/
unit.rs

1#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
2#[xml(tag = "Unit", strict(unknown_attribute, unknown_element))]
3/// Unit definition (with respect to SI base units) and default display units
4pub struct Fmi2Unit {
5    #[xml(attr = "name")]
6    pub name: String,
7    /// BaseUnit_value = factor*Unit_value + offset
8    #[xml(child = "BaseUnit")]
9    pub base_unit: Option<BaseUnit>,
10    #[xml(child = "DisplayUnit")]
11    pub display_unit: Vec<DisplayUnit>,
12}
13
14#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
15#[xml(tag = "BaseUnit", strict(unknown_attribute, unknown_element))]
16pub struct BaseUnit {
17    /// Exponent of SI base unit "kg"
18    #[xml(attr = "kg")]
19    pub kg: Option<i32>,
20    /// Exponent of SI base unit "m"
21    #[xml(attr = "m")]
22    pub m: Option<i32>,
23    #[xml(attr = "s")]
24    pub s: Option<i32>,
25    #[xml(attr = "A")]
26    pub a: Option<i32>,
27    #[xml(attr = "K")]
28    pub k: Option<i32>,
29    #[xml(attr = "mol")]
30    pub mol: Option<i32>,
31    #[xml(attr = "cd")]
32    pub cd: Option<i32>,
33    #[xml(attr = "rad")]
34    pub rad: Option<i32>,
35    #[xml(attr = "factor")]
36    pub factor: Option<f64>,
37    #[xml(attr = "offset")]
38    pub offset: Option<f64>,
39}
40
41#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
42#[xml(tag = "DisplayUnit", strict(unknown_attribute, unknown_element))]
43pub struct DisplayUnit {
44    #[xml(attr = "name")]
45    pub name: String,
46    #[xml(attr = "factor")]
47    pub factor: Option<f64>,
48    #[xml(attr = "offset")]
49    pub offset: Option<f64>,
50    #[xml(attr = "inverse")]
51    pub inverse: Option<bool>,
52}
53
54#[test]
55fn test_dependencies_kind() {
56    use hard_xml::XmlRead;
57
58    let xml = r#"
59    <Unit name="m/s2"><BaseUnit m="1" s="-2"/></Unit>
60    "#;
61
62    let unit: Fmi2Unit = Fmi2Unit::from_str(xml).unwrap();
63    assert_eq!(unit.name, "m/s2");
64    assert_eq!(
65        unit.base_unit,
66        Some(BaseUnit {
67            m: Some(1),
68            s: Some(-2),
69            ..Default::default()
70        })
71    )
72}