Skip to main content

fmi_schema/fmi2/
type.rs

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