use std::io::{self, Read};
pub fn calc_checksum(data: &[u8]) -> u8 {
data.iter().fold(0, |x, &y| x.wrapping_add(y))
}
pub fn calc_crc(data: &[u8]) -> u16 {
crc16::State::<crc16::XMODEM>::calculate(data)
}
pub fn get_byte<R: Read>(reader: &mut R) -> std::io::Result<u8> {
let mut buff = [0];
(reader.read_exact(&mut buff))?;
Ok(buff[0])
}
pub fn get_byte_timeout<R: Read>(reader: &mut R) -> std::io::Result<Option<u8>> {
match get_byte(reader) {
Ok(c) => Ok(Some(c)),
Err(err) => {
if err.kind() == io::ErrorKind::TimedOut {
Ok(None)
} else {
Err(err)
}
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
ExhaustedRetries,
Canceled,
}