use core::{future::Future, pin::Pin};
use thiserror::Error;
#[cfg(feature = "serial")]
mod serial;
#[cfg(feature = "tcp")]
mod tcp;
#[cfg(feature = "serial")]
pub use serial::{SerialChannel, SerialOptions};
#[cfg(feature = "tcp")]
pub use tcp::{
DEFAULT_CONNECT_TIMEOUT, MAXIMUM_TCP_CONFIGURATION_DURATION, TcpChannel, TcpOptions,
};
pub type ChannelFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, ChannelError>> + Send + 'a>>;
#[derive(Debug, Error)]
pub enum ChannelError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "serial")]
#[error("serial-port error: {0}")]
Serial(#[from] tokio_serial::Error),
#[error("channel closed by the remote peer")]
Closed,
#[error("channel {0} timeout expired")]
Timeout(&'static str),
#[error("invalid channel configuration: {0}")]
Configuration(&'static str),
#[error("transport protocol error: {0}")]
Protocol(String),
}
pub trait ByteChannel: Send {
fn send<'a>(&'a mut self, bytes: &'a [u8]) -> ChannelFuture<'a, ()>;
fn receive<'a>(&'a mut self, buffer: &'a mut [u8]) -> ChannelFuture<'a, usize>;
fn flush(&mut self) -> ChannelFuture<'_, ()>;
}