cmsis_pack/pdsc/
condition.rs

1use anyhow::Error;
2use roxmltree::Node;
3
4use crate::utils::prelude::*;
5
6pub struct ConditionComponent {
7    pub device_family: Option<String>,
8    pub device_sub_family: Option<String>,
9    pub device_variant: Option<String>,
10    pub device_vendor: Option<String>,
11    pub device_name: Option<String>,
12}
13
14impl FromElem for ConditionComponent {
15    fn from_elem(e: &Node) -> Result<Self, Error> {
16        Ok(ConditionComponent {
17            device_family: attr_map(e, "Dfamily").ok(),
18            device_sub_family: attr_map(e, "Dsubfamily").ok(),
19            device_variant: attr_map(e, "Dvariant").ok(),
20            device_vendor: attr_map(e, "Dvendor").ok(),
21            device_name: attr_map(e, "Dname").ok(),
22        })
23    }
24}
25
26pub struct Condition {
27    pub id: String,
28    pub accept: Vec<ConditionComponent>,
29    pub deny: Vec<ConditionComponent>,
30    pub require: Vec<ConditionComponent>,
31}
32
33impl FromElem for Condition {
34    fn from_elem(e: &Node) -> Result<Self, Error> {
35        assert_root_name(e, "condition")?;
36        let mut accept = Vec::new();
37        let mut deny = Vec::new();
38        let mut require = Vec::new();
39        for elem in e.children().filter(|e| e.is_element()) {
40            match elem.tag_name().name() {
41                "accept" => {
42                    accept.push(ConditionComponent::from_elem(e)?);
43                }
44                "deny" => {
45                    deny.push(ConditionComponent::from_elem(e)?);
46                }
47                "require" => {
48                    require.push(ConditionComponent::from_elem(e)?);
49                }
50                "description" => {}
51                _ => {
52                    log::warn!(
53                        "Found unkonwn element {} in components",
54                        elem.tag_name().name()
55                    );
56                }
57            }
58        }
59        Ok(Condition {
60            id: attr_map(e, "id")?,
61            accept,
62            deny,
63            require,
64        })
65    }
66}
67
68#[derive(Default)]
69pub struct Conditions(pub Vec<Condition>);
70
71impl FromElem for Conditions {
72    fn from_elem(e: &Node) -> Result<Self, Error> {
73        assert_root_name(e, "conditions")?;
74        Ok(Conditions(
75            e.children()
76                .filter(|e| e.is_element())
77                .flat_map(|c| Condition::from_elem(&c).ok_warn())
78                .collect(),
79        ))
80    }
81}