use std::borrow::Cow;
use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("{message}. Fix: {fix}")]
InvalidConfig {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
BackendUnavailable {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Submission {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Completion {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
ResourceExhausted {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Unsupported {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Canceled {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Timeout {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
Validation {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}. Fix: {fix}")]
ProcessState {
message: Cow<'static, str>,
fix: Cow<'static, str>,
},
#[error("{message}: {source}. Fix: {fix}")]
Io {
message: Cow<'static, str>,
#[source]
source: io::Error,
fix: Cow<'static, str>,
},
}
impl Error {
#[must_use]
pub fn invalid_config(message: impl Into<Cow<'static, str>>) -> Self {
Self::InvalidConfig {
message: message.into(),
fix: Cow::Borrowed("provide a non-zero queue depth and sensible batch sizes"),
}
}
#[must_use]
pub fn io(
message: impl Into<Cow<'static, str>>,
source: io::Error,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Io {
message: message.into(),
source,
fix: fix.into(),
}
}
#[must_use]
pub fn validation(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Validation {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn submission(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Submission {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn completion(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Completion {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn resource_exhausted(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::ResourceExhausted {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn backend_unavailable(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::BackendUnavailable {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn canceled(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Canceled {
message: message.into(),
fix: fix.into(),
}
}
#[must_use]
pub fn unsupported(message: impl Into<Cow<'static, str>>) -> Self {
Self::Unsupported {
message: message.into(),
fix: Cow::Borrowed("use a supported backend, platform, or operation mode"),
}
}
#[must_use]
pub fn timeout(
message: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self::Timeout {
message: message.into(),
fix: fix.into(),
}
}
}