diff --git a/src/main.rs b/src/main.rs index 2b4638e..971348e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,46 @@ -use anyhow::Result; +use anyhow::{bail, Error, Result}; use std::{ io::{BufRead, BufReader}, + str::FromStr, time::Duration, }; +#[derive(Debug)] +enum Speed { + Slow, + Fast, + Inst, +} + +impl FromStr for Speed { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "SLOW" => Ok(Self::Slow), + "FAST" => Ok(Self::Fast), + "INST" => Ok(Self::Inst), + _ => bail!("Could not parse speed"), + } + } +} + +fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> { + let input = input + .replace("CPS, ", "") + .replace(", CPM, ", " ") + .replace(", uSv/hr, ", " ") + .replace(", ", " "); + let input: Vec<&str> = input.split(' ').collect(); + + Ok(( + input[0].parse::()?, + input[1].parse::()?, + input[2].parse::()?, + input[3].parse::()?, + )) +} + fn main() -> Result<()> { let port_name = "/dev/serial0"; let baud_rate = 9600; @@ -17,5 +54,6 @@ fn main() -> Result<()> { let mut line = String::new(); port.read_line(&mut line)?; println!("{:?}", line); + println!("{:?}", parse(line.trim())?); } }