1use std::borrow::Cow;
4use std::io;
5
6use thiserror::Error;
7
8pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(Debug, Error)]
20#[non_exhaustive]
21pub enum Error {
22 #[error("{message}. Fix: {fix}")]
24 InvalidConfig {
25 message: Cow<'static, str>,
27 fix: Cow<'static, str>,
29 },
30 #[error("{message}. Fix: {fix}")]
32 BackendUnavailable {
33 message: Cow<'static, str>,
35 fix: Cow<'static, str>,
37 },
38 #[error("{message}. Fix: {fix}")]
40 Submission {
41 message: Cow<'static, str>,
43 fix: Cow<'static, str>,
45 },
46 #[error("{message}. Fix: {fix}")]
48 Completion {
49 message: Cow<'static, str>,
51 fix: Cow<'static, str>,
53 },
54 #[error("{message}. Fix: {fix}")]
56 ResourceExhausted {
57 message: Cow<'static, str>,
59 fix: Cow<'static, str>,
61 },
62 #[error("{message}. Fix: {fix}")]
64 Unsupported {
65 message: Cow<'static, str>,
67 fix: Cow<'static, str>,
69 },
70 #[error("{message}. Fix: {fix}")]
72 Canceled {
73 message: Cow<'static, str>,
75 fix: Cow<'static, str>,
77 },
78 #[error("{message}. Fix: {fix}")]
80 Timeout {
81 message: Cow<'static, str>,
83 fix: Cow<'static, str>,
85 },
86 #[error("{message}. Fix: {fix}")]
88 Validation {
89 message: Cow<'static, str>,
91 fix: Cow<'static, str>,
93 },
94 #[error("{message}. Fix: {fix}")]
96 ProcessState {
97 message: Cow<'static, str>,
99 fix: Cow<'static, str>,
101 },
102 #[error("{message}: {source}. Fix: {fix}")]
104 Io {
105 message: Cow<'static, str>,
107 #[source]
109 source: io::Error,
110 fix: Cow<'static, str>,
112 },
113}
114
115impl Error {
116 #[must_use]
118 pub fn invalid_config(message: impl Into<Cow<'static, str>>) -> Self {
119 Self::InvalidConfig {
120 message: message.into(),
121 fix: Cow::Borrowed("provide a non-zero queue depth and sensible batch sizes"),
122 }
123 }
124
125 #[must_use]
127 pub fn io(
128 message: impl Into<Cow<'static, str>>,
129 source: io::Error,
130 fix: impl Into<Cow<'static, str>>,
131 ) -> Self {
132 Self::Io {
133 message: message.into(),
134 source,
135 fix: fix.into(),
136 }
137 }
138
139 #[must_use]
141 pub fn validation(
142 message: impl Into<Cow<'static, str>>,
143 fix: impl Into<Cow<'static, str>>,
144 ) -> Self {
145 Self::Validation {
146 message: message.into(),
147 fix: fix.into(),
148 }
149 }
150
151 #[must_use]
153 pub fn submission(
154 message: impl Into<Cow<'static, str>>,
155 fix: impl Into<Cow<'static, str>>,
156 ) -> Self {
157 Self::Submission {
158 message: message.into(),
159 fix: fix.into(),
160 }
161 }
162
163 #[must_use]
165 pub fn completion(
166 message: impl Into<Cow<'static, str>>,
167 fix: impl Into<Cow<'static, str>>,
168 ) -> Self {
169 Self::Completion {
170 message: message.into(),
171 fix: fix.into(),
172 }
173 }
174
175 #[must_use]
177 pub fn resource_exhausted(
178 message: impl Into<Cow<'static, str>>,
179 fix: impl Into<Cow<'static, str>>,
180 ) -> Self {
181 Self::ResourceExhausted {
182 message: message.into(),
183 fix: fix.into(),
184 }
185 }
186
187 #[must_use]
189 pub fn backend_unavailable(
190 message: impl Into<Cow<'static, str>>,
191 fix: impl Into<Cow<'static, str>>,
192 ) -> Self {
193 Self::BackendUnavailable {
194 message: message.into(),
195 fix: fix.into(),
196 }
197 }
198
199 #[must_use]
201 pub fn canceled(
202 message: impl Into<Cow<'static, str>>,
203 fix: impl Into<Cow<'static, str>>,
204 ) -> Self {
205 Self::Canceled {
206 message: message.into(),
207 fix: fix.into(),
208 }
209 }
210
211 #[must_use]
213 pub fn unsupported(message: impl Into<Cow<'static, str>>) -> Self {
214 Self::Unsupported {
215 message: message.into(),
216 fix: Cow::Borrowed("use a supported backend, platform, or operation mode"),
217 }
218 }
219
220 #[must_use]
222 pub fn timeout(
223 message: impl Into<Cow<'static, str>>,
224 fix: impl Into<Cow<'static, str>>,
225 ) -> Self {
226 Self::Timeout {
227 message: message.into(),
228 fix: fix.into(),
229 }
230 }
231}