1#![allow(clippy::manual_strip)]
3
4use roxmltree::Node;
5
6use super::{ElementExt, Parse, SVDError, SVDErrorAt};
7
8impl Parse for u32 {
9 type Object = u32;
10 type Error = SVDErrorAt;
11 type Config = ();
12
13 fn parse(tree: &Node, _config: &Self::Config) -> Result<u32, Self::Error> {
14 let text = tree.get_text()?;
15
16 (if text.starts_with("0x") || text.starts_with("0X") {
17 u32::from_str_radix(&text["0x".len()..], 16)
18 } else if text.starts_with('#') {
19 u32::from_str_radix(
23 &str::replace(&text.to_lowercase()["#".len()..], "x", "0"),
24 2,
25 )
26 } else if text.starts_with("0b") {
27 u32::from_str_radix(&str::replace(&text["0b".len()..], "x", "0"), 2)
31 } else {
32 text.parse::<u32>()
33 })
34 .map_err(|e| SVDError::from(e).at(tree.id()))
35 }
36}
37
38impl Parse for u64 {
39 type Object = u64;
40 type Error = SVDErrorAt;
41 type Config = ();
42
43 fn parse(tree: &Node, _config: &Self::Config) -> Result<u64, Self::Error> {
44 let text = tree.get_text()?;
45
46 (if text.starts_with("0x") || text.starts_with("0X") {
47 u64::from_str_radix(&text["0x".len()..], 16)
48 } else if text.starts_with('#') {
49 u64::from_str_radix(
53 &str::replace(&text.to_lowercase()["#".len()..], "x", "0"),
54 2,
55 )
56 } else if text.starts_with("0b") {
57 u64::from_str_radix(&str::replace(&text["0b".len()..], "x", "0"), 2)
61 } else {
62 text.parse::<u64>()
63 })
64 .map_err(|e| SVDError::from(e).at(tree.id()))
65 }
66}
67
68pub struct BoolParse;
69
70impl Parse for BoolParse {
71 type Object = bool;
72 type Error = SVDErrorAt;
73 type Config = ();
74
75 fn parse(tree: &Node, _config: &Self::Config) -> Result<bool, Self::Error> {
76 let text = tree.get_text()?;
77 match text {
78 "0" => Ok(false),
79 "1" => Ok(true),
80 _ => match text.parse() {
81 Ok(b) => Ok(b),
82 Err(e) => Err(SVDError::InvalidBooleanValue(text.into(), e).at(tree.id())),
83 },
84 }
85 }
86}