use std::borrow::Cow;
use std::fmt;
use crate::sys::{self, AnvilResult};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
InvalidArgument,
NotDeclared,
WrongKind,
Conflict,
UnsafeSite,
BytesMismatch,
OutOfMemory,
WrongPhase,
Unsupported,
Internal,
NotFound,
AlreadyExists,
Suspended,
Limit,
Disabled,
Other(AnvilResult),
}
impl ErrorCode {
pub const fn from_result(result: AnvilResult) -> Option<Self> {
Some(match result {
sys::ANVIL_OK => return None,
sys::ANVIL_ERROR_INVALID_ARGUMENT => Self::InvalidArgument,
sys::ANVIL_ERROR_NOT_DECLARED => Self::NotDeclared,
sys::ANVIL_ERROR_WRONG_KIND => Self::WrongKind,
sys::ANVIL_ERROR_CONFLICT => Self::Conflict,
sys::ANVIL_ERROR_UNSAFE_SITE => Self::UnsafeSite,
sys::ANVIL_ERROR_BYTES_MISMATCH => Self::BytesMismatch,
sys::ANVIL_ERROR_OUT_OF_MEMORY => Self::OutOfMemory,
sys::ANVIL_ERROR_WRONG_PHASE => Self::WrongPhase,
sys::ANVIL_ERROR_UNSUPPORTED => Self::Unsupported,
sys::ANVIL_ERROR_INTERNAL => Self::Internal,
sys::ANVIL_ERROR_NOT_FOUND => Self::NotFound,
sys::ANVIL_ERROR_ALREADY_EXISTS => Self::AlreadyExists,
sys::ANVIL_ERROR_SUSPENDED => Self::Suspended,
sys::ANVIL_ERROR_LIMIT => Self::Limit,
sys::ANVIL_ERROR_DISABLED => Self::Disabled,
other => Self::Other(other),
})
}
pub const fn result(self) -> AnvilResult {
match self {
Self::InvalidArgument => sys::ANVIL_ERROR_INVALID_ARGUMENT,
Self::NotDeclared => sys::ANVIL_ERROR_NOT_DECLARED,
Self::WrongKind => sys::ANVIL_ERROR_WRONG_KIND,
Self::Conflict => sys::ANVIL_ERROR_CONFLICT,
Self::UnsafeSite => sys::ANVIL_ERROR_UNSAFE_SITE,
Self::BytesMismatch => sys::ANVIL_ERROR_BYTES_MISMATCH,
Self::OutOfMemory => sys::ANVIL_ERROR_OUT_OF_MEMORY,
Self::WrongPhase => sys::ANVIL_ERROR_WRONG_PHASE,
Self::Unsupported => sys::ANVIL_ERROR_UNSUPPORTED,
Self::Internal => sys::ANVIL_ERROR_INTERNAL,
Self::NotFound => sys::ANVIL_ERROR_NOT_FOUND,
Self::AlreadyExists => sys::ANVIL_ERROR_ALREADY_EXISTS,
Self::Suspended => sys::ANVIL_ERROR_SUSPENDED,
Self::Limit => sys::ANVIL_ERROR_LIMIT,
Self::Disabled => sys::ANVIL_ERROR_DISABLED,
Self::Other(sys::ANVIL_OK) => sys::ANVIL_ERROR_INTERNAL,
Self::Other(other) => other,
}
}
pub const fn description(self) -> &'static str {
match self {
Self::InvalidArgument => "invalid argument",
Self::NotDeclared => "not declared in symbols.toml or settings.toml",
Self::WrongKind => "wrong kind of symbol or setting",
Self::Conflict => "conflict with another change",
Self::UnsafeSite => "unsafe site",
Self::BytesMismatch => "bytes mismatch",
Self::OutOfMemory => "out of memory",
Self::WrongPhase => "wrong phase",
Self::Unsupported => "unsupported",
Self::Internal => "internal error",
Self::NotFound => "not found",
Self::AlreadyExists => "already exists",
Self::Suspended => "suspended in this session",
Self::Limit => "over the limit",
Self::Disabled => "turned off by the player",
Self::Other(_) => "unknown result",
}
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.description())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
code: ErrorCode,
context: Cow<'static, str>,
}
impl Error {
pub fn new(code: ErrorCode, context: impl Into<Cow<'static, str>>) -> Self {
Self {
code,
context: context.into(),
}
}
pub const fn code(&self) -> ErrorCode {
self.code
}
pub fn context(&self) -> &str {
&self.context
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.context.is_empty() {
f.write_str(self.code.description())
} else {
write!(f, "{}: {}", self.context, self.code.description())
}
}
}
impl std::error::Error for Error {}
pub(crate) fn check<C: Into<Cow<'static, str>>>(
result: AnvilResult,
context: impl FnOnce() -> C,
) -> Result<()> {
match ErrorCode::from_result(result) {
None => Ok(()),
Some(code) => Err(Error::new(code, context())),
}
}
pub(crate) fn need<F>(function: Option<F>, name: &'static str) -> Result<F> {
function.ok_or_else(|| {
Error::new(
ErrorCode::Unsupported,
format!("{name}: not provided by this version"),
)
})
}