1use std::array::TryFromSliceError;
2
3use crazyflie_link::Packet;
4use futures::task::SpawnError;
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
11pub enum Error {
12 ProtocolVersionNotSupported,
16 ProtocolError(String),
18 ParamError(String),
20 LogError(String),
22 ConversionError(String),
24 LinkError(crazyflie_link::Error),
26 Disconnected,
28 VariableNotFound,
30 SystemError(String),
32 AppchannelPacketTooLarge,
34 InvalidArgument(String),
37 Timeout,
39 MemoryError(String),
41 InvalidParameter(String),
43}
44
45impl std::fmt::Display for Error {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 Error::ProtocolVersionNotSupported => write!(f, "Protocol version not supported"),
49 Error::ProtocolError(msg) => write!(f, "Protocol error: {}", msg),
50 Error::ParamError(msg) => write!(f, "Parameter error: {}", msg),
51 Error::LogError(msg) => write!(f, "Log error: {}", msg),
52 Error::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
53 Error::LinkError(e) => write!(f, "Link error: {}", e),
54 Error::Disconnected => write!(f, "Disconnected"),
55 Error::VariableNotFound => write!(f, "Variable not found"),
56 Error::SystemError(msg) => write!(f, "System error: {}", msg),
57 Error::AppchannelPacketTooLarge => write!(f, "Appchannel packet too large"),
58 Error::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
59 Error::Timeout => write!(f, "Operation timed out"),
60 Error::MemoryError(msg) => write!(f, "Memory error: {}", msg),
61 Error::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
62 }
63 }
64}
65
66impl std::error::Error for Error {}
67
68impl From<TryFromSliceError> for Error {
69 fn from(e: TryFromSliceError) -> Self {
70 Self::ConversionError(format!("{:?}", e))
71 }
72}
73
74impl From<crazyflie_link::Error> for Error {
75 fn from(error: crazyflie_link::Error) -> Self {
76 Self::LinkError(error)
77 }
78}
79
80impl From<SpawnError> for Error {
81 fn from(error: SpawnError) -> Self {
82 Self::SystemError(format!("{}", error))
83 }
84}
85
86impl From<flume::RecvError> for Error {
87 fn from(_: flume::RecvError) -> Self {
88 self::Error::Disconnected
89 }
90}
91
92impl From<flume::SendError<Packet>> for Error {
93 fn from(_: flume::SendError<Packet>) -> Self {
94 self::Error::Disconnected
95 }
96}