use espf::diag::EspfError;
pub fn parse_khz(raw: &str, tag: &str, id: &str) -> Result<u32, String> {
let t = raw.trim().to_ascii_lowercase();
let split = t.find(|c: char| !c.is_ascii_digit()).unwrap_or(t.len());
let (num, unit) = t.split_at(split);
let n: u32 = num.parse().map_err(|_| {
format!("<{tag} id=\"{id}\"> frequency=\"{raw}\" is not a frequency: use e.g. `100kHz`")
})?;
match unit.trim() {
"khz" => Ok(n),
other => Err(format!(
"<{tag} id=\"{id}\"> frequency=\"{raw}\" unit `{other}` is not supported yet: use `kHz`"
)),
}
}
pub fn plain(e: EspfError) -> String {
format!("{}: {}", e.title, e.label)
}
pub fn is_rust_ident(s: &str) -> bool {
!s.is_empty()
&& !s.starts_with(|c: char| c.is_ascii_digit())
&& s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}