use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Status {
InvalidArgument,
InvalidState,
WrongThread,
Unsupported,
NativeError,
Cancelled,
Busy,
TargetLost,
NotFound,
AlreadyExists,
Other(i32),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub status: Status,
pub message: String,
}
impl Error {
#[must_use]
pub fn new(status: Status, message: impl Into<String>) -> Self {
Self { status, message: message.into() }
}
#[must_use]
pub fn invalid_argument(message: impl Into<String>) -> Self {
Self::new(Status::InvalidArgument, message)
}
#[must_use]
pub fn invalid_state(message: impl Into<String>) -> Self {
Self::new(Status::InvalidState, message)
}
#[must_use]
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(Status::NotFound, message)
}
#[must_use]
pub fn already_exists(message: impl Into<String>) -> Self {
Self::new(Status::AlreadyExists, message)
}
#[must_use]
pub fn native(message: impl Into<String>) -> Self {
Self::new(Status::NativeError, message)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.message.is_empty() {
write!(f, "{:?}", self.status)
} else {
write!(f, "{:?}: {}", self.status, self.message)
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;