1use thiserror::Error;
2
3#[derive(Debug, Clone, Error)]
4pub enum Error {
5 #[error("RUST-CAN - invalid identifier: {0}")]
7 InvalidIdentifier(u32),
8 #[error("RUST-CAN - invalid dlc or data length: {0}")]
10 InvalidDLC(usize),
11 #[error("RUST-CAN - invalid frame: {0}")]
13 InvalidFrame(String),
14 #[error("RUST-CAN - initialize error: {0}")]
16 InitializeError(String),
17 #[error("RUST-CAN - not implement error")]
19 NotImplementedError,
20 #[error("RUST-CAN - not supported error")]
22 NotSupportedError,
23 #[error("RUST-CAN - timeout error: {0}")]
25 TimeoutError(String),
26 #[error("RUST-CAN - operation error: {0}")]
28 OperationError(String),
29 #[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}