rs_can/
error.rs

1use thiserror::Error;
2
3#[derive(Debug,  Clone, Error)]
4pub enum Error {
5    /// Error when operation like library loading, device or channel opening and so on.
6    #[error("RUST-CAN - initialize error: {0}")]
7    InitializeError(String),
8    /// Error when function is not implemented.
9    #[error("RUST-CAN - not implement error")]
10    NotImplementedError,
11    /// Error when function is not supported.
12    #[error("RUST-CAN - not supported error")]
13    NotSupportedError,
14    /// Error when operation timeout.
15    #[error("RUST-CAN - timeout error: {0}")]
16    TimeoutError(String),
17    /// Error when operation like transmit, receive and so on.
18    #[error("RUST-CAN - operation error: {0}")]
19    OperationError(String),
20    /// Error when others.
21    #[error("RUST-CAN - other error: {0}")]
22    OtherError(String),
23}
24
25impl Error {
26    #[inline(always)]
27    pub fn device_open_error(msg: &str) -> Self {
28        Self::OperationError(format!("{} when device opened", msg))
29    }
30    #[inline(always)]
31    pub fn device_not_opened() -> Self {
32        Self::operation_error("device is not opened")
33    }
34    #[inline(always)]
35    pub fn channel_not_opened<T: std::fmt::Display>(channel: T) -> Self {
36        Self::OperationError(format!("channel: {} is not opened", channel))
37    }
38    #[inline(always)]
39    pub fn channel_timeout<T: std::fmt::Display>(channel: T) -> Self {
40        Self::TimeoutError(format!("at channel: {}", channel))
41    }
42    #[inline(always)]
43    pub fn operation_error(msg: &str) -> Self {
44        Self::OperationError(msg.into())
45    }
46    #[inline(always)]
47    pub fn other_error(msg: &str) -> Self {
48        Self::OtherError(msg.into())
49    }
50}