Skip to main content

rs_can/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Clone, Error)]
4pub enum Error {
5    /// Error when identifier bits cannot be represented as a valid CAN identifier.
6    #[error("RUST-CAN - invalid identifier: {0}")]
7    InvalidIdentifier(u32),
8    /// Error when data length is invalid.
9    #[error("RUST-CAN - invalid dlc or data length: {0}")]
10    InvalidDLC(usize),
11    /// Error when frame shape or payload is invalid.
12    #[error("RUST-CAN - invalid frame: {0}")]
13    InvalidFrame(String),
14    /// Error when operation like library loading, device or channel opening and so on.
15    #[error("RUST-CAN - initialize error: {0}")]
16    InitializeError(String),
17    /// Error when function is not implemented.
18    #[error("RUST-CAN - not implement error")]
19    NotImplementedError,
20    /// Error when function is not supported.
21    #[error("RUST-CAN - not supported error")]
22    NotSupportedError,
23    /// Error when operation timeout.
24    #[error("RUST-CAN - timeout error: {0}")]
25    TimeoutError(String),
26    /// Error when operation like transmit, receive and so on.
27    #[error("RUST-CAN - operation error: {0}")]
28    OperationError(String),
29    /// Error when others.
30    #[error("RUST-CAN - other error: {0}")]
31    OtherError(String),
32}
33
34impl Error {
35    #[inline(always)]
36    pub fn interface_not_matched<T: std::fmt::Display>(i: T) -> Self {
37        Self::InitializeError(format!("interface {} is not matched", i))
38    }
39    #[inline(always)]
40    pub fn device_open_error<T: std::fmt::Display>(msg: T) -> Self {
41        Self::OperationError(format!("{} when device opened", msg))
42    }
43    #[inline(always)]
44    pub fn device_not_opened() -> Self {
45        Self::operation_error("device is not opened")
46    }
47    #[inline(always)]
48    pub fn channel_not_opened<T: std::fmt::Display>(channel: T) -> Self {
49        Self::OperationError(format!("channel: {} is not opened", channel))
50    }
51    #[inline(always)]
52    pub fn channel_timeout<T: std::fmt::Display>(channel: T) -> Self {
53        Self::TimeoutError(format!("at channel: {}", channel))
54    }
55    #[inline(always)]
56    pub fn operation_error<T: Into<String>>(msg: T) -> Self {
57        Self::OperationError(msg.into())
58    }
59    #[inline(always)]
60    pub fn other_error<T: Into<String>>(msg: T) -> Self {
61        Self::OtherError(msg.into())
62    }
63}