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 interface_not_matched<T: std::fmt::Display>(i: T) -> Self {
28        Self::InitializeError(format!("interface {} is not matched", i))
29    }
30    #[inline(always)]
31    pub fn device_open_error<T: std::fmt::Display>(msg: T) -> Self {
32        Self::OperationError(format!("{} when device opened", msg))
33    }
34    #[inline(always)]
35    pub fn device_not_opened() -> Self {
36        Self::operation_error("device is not opened")
37    }
38    #[inline(always)]
39    pub fn channel_not_opened<T: std::fmt::Display>(channel: T) -> Self {
40        Self::OperationError(format!("channel: {} is not opened", channel))
41    }
42    #[inline(always)]
43    pub fn channel_timeout<T: std::fmt::Display>(channel: T) -> Self {
44        Self::TimeoutError(format!("at channel: {}", channel))
45    }
46    #[inline(always)]
47    pub fn operation_error<T: Into<String>>(msg: T) -> Self {
48        Self::OperationError(msg.into())
49    }
50    #[inline(always)]
51    pub fn other_error<T: Into<String>>(msg: T) -> Self {
52        Self::OtherError(msg.into())
53    }
54}