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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use xmltree::Element;
#[cfg(feature = "unproven")]
pub use crate::encode::Encode;
pub use crate::parse::optional as parse_optional;
pub use crate::parse::Parse;
use crate::elementext::ElementExt;
use crate::error::*;
macro_rules! unwrap {
($e:expr) => {
$e.expect(concat!(file!(), ":", line!(), " ", stringify!($e)))
};
}
impl Parse for u32 {
type Object = u32;
type Error = anyhow::Error;
fn parse(tree: &Element) -> Result<u32> {
let text = tree.get_text()?;
if text.starts_with("0x") || text.starts_with("0X") {
u32::from_str_radix(&text["0x".len()..], 16).context(format!("{} invalid", text))
} else if text.starts_with('#') {
u32::from_str_radix(
&str::replace(&text.to_lowercase()["#".len()..], "x", "0"),
2,
)
.context(format!("{} invalid", text))
} else if text.starts_with("0b") {
u32::from_str_radix(&str::replace(&text["0b".len()..], "x", "0"), 2)
.context(format!("{} invalid", text))
} else {
text.parse::<u32>().context(format!("{} invalid", text))
}
}
}
pub struct BoolParse;
impl Parse for BoolParse {
type Object = bool;
type Error = anyhow::Error;
fn parse(tree: &Element) -> Result<bool> {
let text = unwrap!(tree.text.as_ref());
Ok(match text.as_ref() {
"0" => false,
"1" => true,
_ => match text.parse() {
Ok(b) => b,
Err(e) => {
return Err(SVDError::InvalidBooleanValue(tree.clone(), text.clone(), e).into())
}
},
})
}
}
pub struct DimIndex;
impl Parse for DimIndex {
type Object = Vec<String>;
type Error = anyhow::Error;
fn parse(tree: &Element) -> Result<Vec<String>> {
let text = tree.get_text()?;
if text.contains('-') {
let mut parts = text.splitn(2, '-');
let start = unwrap!(unwrap!(parts.next()).parse::<u32>());
let end = unwrap!(unwrap!(parts.next()).parse::<u32>()) + 1;
Ok((start..end).map(|i| i.to_string()).collect())
} else if text.contains(',') {
Ok(text.split(',').map(|s| s.to_string()).collect())
} else {
unreachable!()
}
}
}