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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use super::*;
use crate::svd::{
    AddressBlock, Interrupt, Peripheral, PeripheralInfo, RegisterCluster, RegisterProperties,
};

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

    fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
        parse_array("peripheral", tree, config)
    }
}

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

    fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
        if !tree.has_tag_name("peripheral") {
            return Err(SVDError::NotExpectedTag("peripheral".to_string()).at(tree.id()));
        }

        PeripheralInfo::builder()
            .name(tree.get_child_text("name")?)
            .display_name(tree.get_child_text_opt("displayName")?)
            .version(tree.get_child_text_opt("version")?)
            .description(tree.get_child_text_opt("description")?)
            .alternate_peripheral(tree.get_child_text_opt("alternatePeripheral")?)
            .group_name(tree.get_child_text_opt("groupName")?)
            .prepend_to_name(tree.get_child_text_opt("prependToName")?)
            .append_to_name(tree.get_child_text_opt("appendToName")?)
            .header_struct_name(tree.get_child_text_opt("headerStructName")?)
            .base_address(tree.get_child_u64("baseAddress")?)
            .default_register_properties(RegisterProperties::parse(tree, config)?)
            .address_block({
                let ab: Result<Vec<_>, _> = tree
                    .children()
                    .filter(|t| t.is_element() && t.has_tag_name("addressBlock"))
                    .map(|i| AddressBlock::parse(&i, config))
                    .collect();
                let ab = ab?;
                if ab.is_empty() {
                    None
                } else {
                    Some(ab)
                }
            })
            .interrupt({
                let interrupt: Result<Vec<_>, _> = tree
                    .children()
                    .filter(|t| t.is_element() && t.has_tag_name("interrupt"))
                    .map(|i| Interrupt::parse(&i, config))
                    .collect();
                Some(interrupt?)
            })
            .registers(if let Some(registers) = tree.get_child("registers") {
                let rs: Result<Vec<_>, _> = registers
                    .children()
                    .filter(Node::is_element)
                    .map(|t| RegisterCluster::parse(&t, config))
                    .collect();
                Some(rs?)
            } else {
                None
            })
            .derived_from(tree.attribute("derivedFrom").map(|s| s.to_owned()))
            .build(config.validate_level)
            .map_err(|e| SVDError::from(e).at(tree.id()))
    }
}