1use super::*;
2use crate::svd::{
3    AddressBlock, Interrupt, Peripheral, PeripheralInfo, RegisterCluster, RegisterProperties,
4};
5
6impl Parse for Peripheral {
7    type Object = Self;
8    type Error = SVDErrorAt;
9    type Config = Config;
10
11    fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
12        parse_array("peripheral", tree, config)
13    }
14}
15
16impl Parse for PeripheralInfo {
17    type Object = Self;
18    type Error = SVDErrorAt;
19    type Config = Config;
20
21    fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
22        if !tree.has_tag_name("peripheral") {
23            return Err(SVDError::NotExpectedTag("peripheral".to_string()).at(tree.id()));
24        }
25
26        PeripheralInfo::builder()
27            .name(tree.get_child_text("name")?)
28            .display_name(tree.get_child_text_opt("displayName")?)
29            .version(tree.get_child_text_opt("version")?)
30            .description(tree.get_child_text_opt("description")?)
31            .alternate_peripheral(tree.get_child_text_opt("alternatePeripheral")?)
32            .group_name(tree.get_child_text_opt("groupName")?)
33            .prepend_to_name(tree.get_child_text_opt("prependToName")?)
34            .append_to_name(tree.get_child_text_opt("appendToName")?)
35            .header_struct_name(tree.get_child_text_opt("headerStructName")?)
36            .base_address(tree.get_child_u64("baseAddress")?)
37            .default_register_properties(RegisterProperties::parse(tree, config)?)
38            .address_block({
39                let ab: Result<Vec<_>, _> = tree
40                    .children()
41                    .filter(|t| t.is_element() && t.has_tag_name("addressBlock"))
42                    .map(|i| AddressBlock::parse(&i, config))
43                    .collect();
44                let ab = ab?;
45                if ab.is_empty() {
46                    None
47                } else {
48                    Some(ab)
49                }
50            })
51            .interrupt({
52                let interrupt: Result<Vec<_>, _> = tree
53                    .children()
54                    .filter(|t| t.is_element() && t.has_tag_name("interrupt"))
55                    .map(|i| Interrupt::parse(&i, config))
56                    .collect();
57                Some(interrupt?)
58            })
59            .registers(if let Some(registers) = tree.get_child("registers") {
60                let rs: Result<Vec<_>, _> = registers
61                    .children()
62                    .filter(Node::is_element)
63                    .map(|t| RegisterCluster::parse(&t, config))
64                    .collect();
65                Some(rs?)
66            } else {
67                None
68            })
69            .derived_from(tree.attribute("derivedFrom").map(|s| s.to_owned()))
70            .build(config.validate_level)
71            .map_err(|e| SVDError::from(e).at(tree.id()))
72    }
73}