svd_parser/
device.rs

1use super::*;
2use crate::svd::{cpu::Cpu, peripheral::Peripheral, registerproperties::RegisterProperties};
3
4/// Parses a SVD file
5impl Parse for Device {
6    type Object = Self;
7    type Error = SVDErrorAt;
8    type Config = Config;
9
10    fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
11        if !tree.has_tag_name("device") {
12            return Err(SVDError::NotExpectedTag("device".to_string()).at(tree.id()));
13        }
14
15        let mut device = Device::builder()
16            .vendor(tree.get_child_text_opt("vendor")?)
17            .vendor_id(tree.get_child_text_opt("vendorID")?)
18            .name(tree.get_child_text("name")?)
19            .series(tree.get_child_text_opt("series")?)
20            .license_text(tree.get_child_text_opt("licenseText")?)
21            .cpu(optional::<Cpu>("cpu", tree, config)?)
22            .header_system_filename(tree.get_child_text_opt("headerSystemFilename")?)
23            .header_definitions_prefix(tree.get_child_text_opt("headerDefinitionsPrefix")?)
24            .default_register_properties(RegisterProperties::parse(tree, config)?)
25            .peripherals({
26                let ps: Result<Vec<_>, _> = tree
27                    .get_child_elem("peripherals")?
28                    .children()
29                    .filter(Node::is_element)
30                    .map(|t| Peripheral::parse(&t, config))
31                    .collect();
32                ps?
33            });
34        if let Some(version) = tree.get_child_text_opt("version")? {
35            device = device.version(version)
36        }
37        if let Some(description) = tree.get_child_text_opt("description")? {
38            device = device.description(description)
39        }
40        if let Some(bits) = optional::<u32>("addressUnitBits", tree, &())? {
41            device = device.address_unit_bits(bits)
42        }
43        if let Some(width) = optional::<u32>("width", tree, &())? {
44            device = device.width(width)
45        }
46        // TODO: accept namespace other than `xs`
47        // Now assert `xs` exists and `noNamespaceSchemaLocation` is under `xs`
48        if let Some(xmlns_xs) = tree.lookup_namespace_uri(Some("xs")) {
49            device = device.xmlns_xs(xmlns_xs.to_string());
50            if let Some(location) = tree.attribute((xmlns_xs, "noNamespaceSchemaLocation")) {
51                device = device.no_namespace_schema_location(location.to_string());
52            }
53        }
54        if let Some(schema_version) = tree.attribute("schemaVersion") {
55            device = device.schema_version(schema_version.to_string());
56        }
57        device
58            .build(config.validate_level)
59            .map_err(|e| SVDError::from(e).at(tree.id()))
60    }
61}