use thiserror::Error;
use crate::connections::wrappers::encoded_data::{
EncodedToRadioPacket, EncodedToRadioPacketWithHeader, IncomingStreamData,
};
#[derive(Error, Debug)]
pub enum Error {
#[error("Invalid channel {channel} entered. Valid channels are in the range [0..7]")]
InvalidChannelIndex {
channel: u32,
},
#[error(transparent)]
EncodeError(#[from] prost::EncodeError),
#[error(transparent)]
JoinError(#[from] tokio::task::JoinError),
#[error("Packet handler failed with error {source:?}")]
PacketHandlerFailure {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error("Failed to build input data stream with error {source:?}")]
StreamBuildError {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
description: String,
},
#[error("Trying to send too much data")]
InvalidaDataSize {
data_length: usize,
},
#[error("Failed to remove packet header from packet buffer due to insufficient data length: {packet}")]
InsufficientPacketBufferLength {
packet: EncodedToRadioPacketWithHeader,
},
#[error("Invalid function parameter: {source:?}")]
InvalidParameter {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
description: String,
},
#[error(transparent)]
InternalStreamError(#[from] InternalStreamError),
#[error(transparent)]
InternalChannelError(#[from] InternalChannelError),
#[cfg(feature = "bluetooth-le")]
#[error(transparent)]
MacAddressParseError(#[from] btleplug::api::ParseBDAddrError),
}
#[warn(clippy::enum_variant_names)]
#[derive(Error, Debug)]
pub enum InternalStreamError {
#[error("Failed to read from stream with error {source:?}")]
StreamReadError {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error("Failed to write to stream with error {source:?}")]
StreamWriteError {
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error("Stream has reached EOF")]
Eof,
#[error("Connection lost")]
ConnectionLost,
}
impl InternalStreamError {
pub fn write_error(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::StreamWriteError {
source: Box::new(err),
}
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Error, Debug)]
pub enum InternalChannelError {
#[error(transparent)]
ToRadioWriteError(#[from] tokio::sync::mpsc::error::SendError<EncodedToRadioPacket>),
#[error(transparent)]
ToRadioWithHeaderWriteError(
#[from] tokio::sync::mpsc::error::SendError<EncodedToRadioPacketWithHeader>,
),
#[error(transparent)]
IncomingStreamDataWriteError(#[from] tokio::sync::mpsc::error::SendError<IncomingStreamData>),
#[error("Channel unexpectedly closed")]
ChannelClosedEarly,
}
#[derive(Error, Debug)]
#[error("Bluetooth low energy connection error")]
#[cfg(feature = "bluetooth-le")]
pub struct BleConnectionError();
mod test {
#[allow(dead_code)]
fn is_send<T: Send>() {}
#[allow(dead_code)]
fn is_sync<T: Sync>() {}
#[test]
fn test_send_sync() {
is_send::<super::Error>();
is_sync::<super::Error>();
}
}