define_struct! {
pub struct IoPort {
range: (usize, usize),
device: String,
}
}
use std::str::FromStr;
impl FromStr for IoPort {
type Err = crate::ProcErr;
fn from_str(s: &str) -> Result<IoPort, Self::Err> {
let items: Vec<&str> = s.splitn(3, |c| c == '-' || c == ':').map(|s| s.trim()).collect();
if items.len() != 3 {
return Err("require three items at least to parse ioport".into());
}
let start = usize::from_str_radix(items[0], 16)?;
let end = usize::from_str_radix(items[1], 16)?;
let device = items[2].to_string();
Ok(IoPort { range: (start, end), device })
}
}
list_impl! {
ioports, "/proc/ioports", IoPort, '\n', 0
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_ioport() {
let source = "00f0-00ff : fpu";
let correct = IoPort{
range: (0xf0, 0xff),
device: "fpu".to_string()
};
assert_eq!(correct, source.parse::<IoPort>().unwrap());
}
}