1use std::borrow::Cow;
2
3use thiserror::Error;
4
5#[derive(Clone, Debug, Error)]
6pub enum Error {
7 #[error("Failed: {0}")]
8 Failed(Cow<'static, str>),
9 #[error("Invalid: {0}")]
10 Invalid(Cow<'static, str>),
11 #[error("Again: {0}")]
12 Again(Cow<'static, str>),
13 #[error("Canceled: {0}")]
14 Canceled(Cow<'static, str>),
15 #[error("Creation failed: {0}")]
16 CreationFailed(Cow<'static, str>),
17 #[error("Invalid parameter: {0} {1}")]
18 InvalidParameter(Cow<'static, str>, Cow<'static, str>),
19 #[error("Not implemented")]
20 NotImplemented,
21 #[error("Not found: {0}")]
22 NotFound(Cow<'static, str>),
23 #[error("Unsupported: {0}")]
24 Unsupported(Cow<'static, str>),
25 #[error("Initialization failed: {0}")]
26 InitializationFailed(Cow<'static, str>),
27 #[error("Open failed: {0}")]
28 OpenFailed(Cow<'static, str>),
29 #[error("Close failed: {0}")]
30 CloseFailed(Cow<'static, str>),
31 #[error("Start failed: {0}")]
32 StartFailed(Cow<'static, str>),
33 #[error("Stop failed: {0}")]
34 StopFailed(Cow<'static, str>),
35 #[error("Not running: {0}")]
36 NotRunning(Cow<'static, str>),
37 #[error("Get failed: {0}")]
38 GetFailed(Cow<'static, str>),
39 #[error("Set failed: {0}")]
40 SetFailed(Cow<'static, str>),
41 #[error("Read failed: {0}")]
42 ReadFailed(Cow<'static, str>),
43 #[error("Write failed: {0}")]
44 WriteFailed(Cow<'static, str>),
45}
46
47#[macro_export]
48macro_rules! invalid_error {
49 ($param:literal) => {
50 $crate::error::Error::Invalid($param.into())
51 };
52 ($param:expr) => {
53 $crate::error::Error::Invalid(format!("{:?}", $param).into())
54 };
55}
56
57#[macro_export]
58macro_rules! failed_error {
59 ($param:literal) => {
60 $crate::error::Error::Failed($param.into())
61 };
62 ($param:expr) => {
63 $crate::error::Error::Failed(format!("{:?}", $param).into())
64 };
65}
66
67#[macro_export]
68macro_rules! invalid_param_error {
69 ($param:expr) => {
70 $crate::error::Error::InvalidParameter(stringify!($param).into(), format!("{:?}", $param).into())
71 };
72}
73
74#[macro_export]
75macro_rules! none_param_error {
76 ($param:expr) => {
77 $crate::error::Error::InvalidParameter(stringify!($param).into(), stringify!(None).into())
78 };
79}
80
81#[macro_export]
82macro_rules! not_found_error {
83 ($param:literal) => {
84 $crate::error::Error::NotFound($param.into())
85 };
86 ($param:expr) => {
87 $crate::error::Error::NotFound(format!("{:?}", $param).into())
88 };
89}
90
91#[macro_export]
92macro_rules! unsupported_error {
93 ($param:literal) => {
94 $crate::error::Error::Unsupported($param.into())
95 };
96 ($param:expr) => {
97 $crate::error::Error::Unsupported(format!("{:?}", $param).into())
98 };
99}