1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use super::*;
use crate::svd::{WriteConstraint, WriteConstraintRange};

impl Parse for WriteConstraint {
    type Object = Self;
    type Error = SVDErrorAt;
    type Config = Config;

    fn parse(tree: &Node, _config: &Self::Config) -> Result<Self, Self::Error> {
        let child = tree.first_element_child().unwrap();
        if child.next_sibling_element().is_some() {
            return Err(SVDError::MoreThanOneWriteConstraint.at(tree.id()));
        }
        let field = child.tag_name().name();
        // Write constraint can only be one of the following
        match field {
            "writeAsRead" => tree.get_child_bool(field).map(WriteConstraint::WriteAsRead),
            "useEnumeratedValues" => tree
                .get_child_bool(field)
                .map(WriteConstraint::UseEnumeratedValues),
            "range" => WriteConstraintRange::parse(&tree.get_child_elem(field)?, &())
                .map(WriteConstraint::Range),
            _ => Err(SVDError::UnknownWriteConstraint.at(tree.id())),
        }
    }
}

impl Parse for WriteConstraintRange {
    type Object = Self;
    type Error = SVDErrorAt;
    type Config = ();

    fn parse(tree: &Node, _config: &Self::Config) -> Result<Self, Self::Error> {
        Ok(Self {
            min: tree.get_child_u64("minimum")?,
            max: tree.get_child_u64("maximum")?,
        })
    }
}