use crate::Result;
use crate::{SerialFrameError, SerialFrameStopper};
use core::convert::TryFrom;
use crossbeam_channel::{unbounded, Receiver};
use derive_more::*;
use serialport::prelude::*;
#[derive(Display, Debug, Into, From, PartialEq, Eq)]
pub struct Line(pub String);
impl TryFrom<Vec<u8>> for Line {
type Error = crate::SerialFrameError;
fn try_from(input: Vec<u8>) -> Result<Self> {
let inputstr = String::from_utf8(input.clone())
.map_err(|_e| SerialFrameError::FailedConversion(input))?;
Ok(Self(inputstr))
}
}
pub fn create_line_sender(
serialport: Box<dyn SerialPort>,
) -> Result<(Receiver<Line>, SerialFrameStopper)> {
let (tx, rx) = unbounded();
let sender = crate::SerialFrameSender::new(b'\n', serialport);
let stopper = sender.start(tx)?;
Ok((rx, stopper))
}
pub fn create_cobs_sender(
serialport: Box<dyn SerialPort>,
) -> Result<(Receiver<Vec<u8>>, SerialFrameStopper)> {
let (tx, rx) = unbounded();
let sender = crate::SerialFrameSender::new(0, serialport);
let stopper = sender.start(tx)?;
Ok((rx, stopper))
}