fmi_schema/fmi2/
type.rs

1use yaserde_derive::{YaDeserialize, YaSerialize};
2
3use super::attribute_groups::{IntegerAttributes, RealAttributes};
4
5#[derive(Debug, PartialEq, YaSerialize, YaDeserialize)]
6pub enum SimpleTypeElement {
7    #[yaserde(flatten = true)]
8    Real(RealAttributes),
9    #[yaserde(flatten = true)]
10    Integer(IntegerAttributes),
11    #[yaserde()]
12    Boolean,
13    #[yaserde()]
14    String,
15    #[yaserde()]
16    Enumeration,
17}
18
19impl Default for SimpleTypeElement {
20    fn default() -> Self {
21        Self::Real(RealAttributes::default())
22    }
23}
24
25#[derive(Default, Debug, PartialEq, YaSerialize, YaDeserialize)]
26#[yaserde()]
27/// Type attributes of a scalar variable
28pub struct SimpleType {
29    #[yaserde(flatten = true)]
30    pub elem: SimpleTypeElement,
31
32    #[yaserde(attribute = true)]
33    /// Name of SimpleType element. "name" must be unique with respect to all other elements of the
34    /// TypeDefinitions list. Furthermore, "name" of a SimpleType must be different to all
35    /// "name"s of ScalarVariable.
36    pub name: String,
37
38    #[yaserde(attribute = true)]
39    /// Description of the SimpleType
40    pub description: Option<String>,
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::fmi2::{RealAttributes, SimpleTypeElement};
46
47    use super::SimpleType;
48
49    #[test]
50    fn test_simple_type() {
51        let xml = r#"
52        <SimpleType name="Acceleration">
53            <Real quantity="Acceleration" unit="m/s2"/>
54        </SimpleType>"#;
55
56        let simple_type: SimpleType = yaserde::de::from_str(xml).unwrap();
57        assert_eq!(simple_type.name, "Acceleration");
58        assert_eq!(simple_type.description, None);
59        assert_eq!(
60            simple_type.elem,
61            SimpleTypeElement::Real(RealAttributes {
62                quantity: Some("Acceleration".to_owned()),
63                unit: Some("m/s2".to_owned()),
64                ..Default::default()
65            })
66        );
67    }
68}