#![allow(clippy::manual_strip)]
use roxmltree::Node;
use super::{ElementExt, Parse, SVDError, SVDErrorAt};
impl Parse for u32 {
type Object = u32;
type Error = SVDErrorAt;
type Config = ();
fn parse(tree: &Node, _config: &Self::Config) -> Result<u32, Self::Error> {
let text = tree.get_text()?;
(if text.starts_with("0x") || text.starts_with("0X") {
u32::from_str_radix(&text["0x".len()..], 16)
} else if text.starts_with('#') {
u32::from_str_radix(
&str::replace(&text.to_lowercase()["#".len()..], "x", "0"),
2,
)
} else if text.starts_with("0b") {
u32::from_str_radix(&str::replace(&text["0b".len()..], "x", "0"), 2)
} else {
text.parse::<u32>()
})
.map_err(|e| SVDError::from(e).at(tree.id()))
}
}
impl Parse for u64 {
type Object = u64;
type Error = SVDErrorAt;
type Config = ();
fn parse(tree: &Node, _config: &Self::Config) -> Result<u64, Self::Error> {
let text = tree.get_text()?;
(if text.starts_with("0x") || text.starts_with("0X") {
u64::from_str_radix(&text["0x".len()..], 16)
} else if text.starts_with('#') {
u64::from_str_radix(
&str::replace(&text.to_lowercase()["#".len()..], "x", "0"),
2,
)
} else if text.starts_with("0b") {
u64::from_str_radix(&str::replace(&text["0b".len()..], "x", "0"), 2)
} else {
text.parse::<u64>()
})
.map_err(|e| SVDError::from(e).at(tree.id()))
}
}
pub struct BoolParse;
impl Parse for BoolParse {
type Object = bool;
type Error = SVDErrorAt;
type Config = ();
fn parse(tree: &Node, _config: &Self::Config) -> Result<bool, Self::Error> {
let text = tree.get_text()?;
match text {
"0" => Ok(false),
"1" => Ok(true),
_ => match text.parse() {
Ok(b) => Ok(b),
Err(e) => Err(SVDError::InvalidBooleanValue(text.into(), e).at(tree.id())),
},
}
}
}