Skip to main content

media_core/
error.rs

1use std::borrow::Cow;
2
3use thiserror::Error;
4
5#[derive(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("Invalid data: {0}")]
20    InvalidData(Cow<'static, str>),
21    #[error("Not implemented")]
22    NotImplemented,
23    #[error("Not found: {0}")]
24    NotFound(Cow<'static, str>),
25    #[error("Unsupported: {0}")]
26    Unsupported(Cow<'static, str>),
27    #[error("Initialization failed: {0}")]
28    InitializationFailed(Cow<'static, str>),
29    #[error("Open failed: {0}")]
30    OpenFailed(Cow<'static, str>),
31    #[error("Close failed: {0}")]
32    CloseFailed(Cow<'static, str>),
33    #[error("Start failed: {0}")]
34    StartFailed(Cow<'static, str>),
35    #[error("Stop failed: {0}")]
36    StopFailed(Cow<'static, str>),
37    #[error("Not running: {0}")]
38    NotRunning(Cow<'static, str>),
39    #[error("Get failed: {0}")]
40    GetFailed(Cow<'static, str>),
41    #[error("Set failed: {0}")]
42    SetFailed(Cow<'static, str>),
43    #[error("Read failed: {0}")]
44    ReadFailed(Cow<'static, str>),
45    #[error("Write failed: {0}")]
46    WriteFailed(Cow<'static, str>),
47    #[error("More data needed: {0}")]
48    MoreDataNeeded(usize),
49    #[error("Unexpected end of data")]
50    UnexpectedEndOfData,
51    #[error(transparent)]
52    IO(#[from] std::io::Error),
53}
54
55#[macro_export]
56macro_rules! invalid_error {
57    ($param:literal) => {
58        $crate::error::Error::Invalid($param.into())
59    };
60    ($param:expr) => {
61        $crate::error::Error::Invalid(format!("{:?}", $param).into())
62    };
63    ($key:expr, $value:expr) => {
64        $crate::error::Error::Invalid(format!("{}: {:?}", $key, $value).into())
65    };
66}
67
68#[macro_export]
69macro_rules! failed_error {
70    ($param:literal) => {
71        $crate::error::Error::Failed($param.into())
72    };
73    ($param:expr) => {
74        $crate::error::Error::Failed(format!("{:?}", $param).into())
75    };
76    ($key:expr, $value:expr) => {
77        $crate::error::Error::Failed(format!("{}: {:?}", $key, $value).into())
78    };
79}
80
81#[macro_export]
82macro_rules! again_error {
83    ($param:literal) => {
84        $crate::error::Error::Again($param.into())
85    };
86    ($param:expr) => {
87        $crate::error::Error::Again(format!("{:?}", $param).into())
88    };
89}
90
91#[macro_export]
92macro_rules! invalid_param_error {
93    ($param:expr) => {
94        $crate::error::Error::InvalidParameter(stringify!($param).into(), format!("{:?}", $param).into())
95    };
96}
97
98#[macro_export]
99macro_rules! invalid_data_error {
100    ($param:literal) => {
101        $crate::error::Error::InvalidData($param.into())
102    };
103    ($param:expr) => {
104        $crate::error::Error::InvalidData(format!("{:?}", $param).into())
105    };
106    ($key:expr, $value:expr) => {
107        $crate::error::Error::InvalidData(format!("{}: {:?}", $key, $value).into())
108    };
109}
110
111#[macro_export]
112macro_rules! none_param_error {
113    ($param:expr) => {
114        $crate::error::Error::InvalidParameter(stringify!($param).into(), stringify!(None).into())
115    };
116}
117
118#[macro_export]
119macro_rules! not_found_error {
120    ($param:literal) => {
121        $crate::error::Error::NotFound($param.into())
122    };
123    ($param:expr) => {
124        $crate::error::Error::NotFound(format!("{:?}", $param).into())
125    };
126    ($key:expr, $value:expr) => {
127        $crate::error::Error::NotFound(format!("{}: {:?}", $key, $value).into())
128    };
129}
130
131#[macro_export]
132macro_rules! unsupported_error {
133    ($param:literal) => {
134        $crate::error::Error::Unsupported($param.into())
135    };
136    ($param:expr) => {
137        $crate::error::Error::Unsupported(format!("{:?}", $param).into())
138    };
139    ($key:expr, $value:expr) => {
140        $crate::error::Error::Unsupported(format!("{}: {:?}", $key, $value).into())
141    };
142}
143
144#[macro_export]
145macro_rules! read_failed_error {
146    ($param:literal) => {
147        $crate::error::Error::ReadFailed($param.into())
148    };
149    ($param:expr) => {
150        $crate::error::Error::ReadFailed(format!("{:?}", $param).into())
151    };
152    ($key:expr, $value:expr) => {
153        $crate::error::Error::ReadFailed(format!("{}: {:?}", $key, $value).into())
154    };
155}
156
157#[macro_export]
158macro_rules! write_failed_error {
159    ($param:literal) => {
160        $crate::error::Error::WriteFailed($param.into())
161    };
162    ($param:expr) => {
163        $crate::error::Error::WriteFailed(format!("{:?}", $param).into())
164    };
165    ($key:expr, $value:expr) => {
166        $crate::error::Error::WriteFailed(format!("{}: {:?}", $key, $value).into())
167    };
168}