Skip to main content

fmi_schema/fmi3/
unit.rs

1use super::Annotations;
2
3#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
4#[xml(tag = "Unit", strict(unknown_attribute, unknown_element))]
5pub struct Fmi3Unit {
6    #[xml(attr = "name")]
7    pub name: String,
8    #[xml(child = "BaseUnit")]
9    pub base_unit: Option<BaseUnit>,
10    #[xml(child = "DisplayUnit")]
11    pub display_unit: Vec<DisplayUnit>,
12    #[xml(child = "Annotations")]
13    pub annotations: Option<Annotations>,
14}
15
16#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
17#[xml(tag = "BaseUnit", strict(unknown_attribute, unknown_element))]
18pub struct BaseUnit {
19    #[xml(attr = "kg")]
20    pub kg: Option<i32>,
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(child = "Annotations")]
45    pub annotations: Option<Annotations>,
46    #[xml(attr = "name")]
47    pub name: String,
48    #[xml(attr = "factor")]
49    pub factor: Option<f64>,
50    #[xml(attr = "offset")]
51    pub offset: Option<f64>,
52    #[xml(attr = "inverse")]
53    pub inverse: Option<bool>,
54}
55
56#[test]
57fn test_dependencies_kind() {
58    use hard_xml::{XmlRead, XmlWrite};
59
60    let xml = r#"<Unit name="m/s2"><BaseUnit m="1" s="-2"/></Unit>"#;
61    let unit = Fmi3Unit::from_str(xml).unwrap();
62
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
73    let xml_out = unit.to_string().unwrap();
74    assert_eq!(xml_out, xml);
75}
76
77#[test]
78fn test_display_unit() {
79    use hard_xml::XmlRead;
80
81    let xml = r#"<DisplayUnit name="km/h" factor="0.2777777777777778" offset="0"/>"#;
82
83    let display_unit = DisplayUnit::from_str(xml).unwrap();
84
85    assert_eq!(display_unit.name, "km/h");
86    assert_eq!(display_unit.factor, Some(0.2777777777777778));
87    assert_eq!(display_unit.offset, Some(0.0));
88}