use std::fmt::Formatter;
use num_derive::FromPrimitive;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
pub struct ExitCode {
value: u32,
}
impl ExitCode {
pub const fn new(value: u32) -> Self {
Self { value }
}
pub fn value(self) -> u32 {
self.value
}
pub fn is_success(self) -> bool {
self.value == 0
}
pub fn is_system_error(self) -> bool {
self.value < (Self::FIRST_USER_EXIT_CODE)
}
}
impl From<u32> for ExitCode {
fn from(value: u32) -> Self {
ExitCode { value }
}
}
impl std::fmt::Display for ExitCode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
impl ExitCode {
pub const OK: ExitCode = ExitCode::new(0);
pub const SYS_SENDER_INVALID: ExitCode = ExitCode::new(1);
pub const SYS_SENDER_STATE_INVALID: ExitCode = ExitCode::new(2);
pub const SYS_ILLEGAL_INSTRUCTION: ExitCode = ExitCode::new(4);
pub const SYS_INVALID_RECEIVER: ExitCode = ExitCode::new(5);
pub const SYS_INSUFFICIENT_FUNDS: ExitCode = ExitCode::new(6);
pub const SYS_OUT_OF_GAS: ExitCode = ExitCode::new(7);
pub const SYS_ILLEGAL_EXIT_CODE: ExitCode = ExitCode::new(9);
pub const SYS_ASSERTION_FAILED: ExitCode = ExitCode::new(10);
pub const SYS_MISSING_RETURN: ExitCode = ExitCode::new(11);
pub const FIRST_USER_EXIT_CODE: u32 = 16;
pub const USR_ILLEGAL_ARGUMENT: ExitCode = ExitCode::new(16);
pub const USR_NOT_FOUND: ExitCode = ExitCode::new(17);
pub const USR_FORBIDDEN: ExitCode = ExitCode::new(18);
pub const USR_INSUFFICIENT_FUNDS: ExitCode = ExitCode::new(19);
pub const USR_ILLEGAL_STATE: ExitCode = ExitCode::new(20);
pub const USR_SERIALIZATION: ExitCode = ExitCode::new(21);
pub const USR_UNHANDLED_MESSAGE: ExitCode = ExitCode::new(22);
pub const USR_UNSPECIFIED: ExitCode = ExitCode::new(23);
pub const USR_ASSERTION_FAILED: ExitCode = ExitCode::new(24);
pub const USR_READ_ONLY: ExitCode = ExitCode::new(25);
pub const USR_NOT_PAYABLE: ExitCode = ExitCode::new(26);
}
#[non_exhaustive]
#[repr(u32)]
#[derive(Copy, Clone, Eq, Debug, PartialEq, Error, FromPrimitive)]
pub enum ErrorNumber {
IllegalArgument = 1,
IllegalOperation = 2,
LimitExceeded = 3,
AssertionFailed = 4,
InsufficientFunds = 5,
NotFound = 6,
InvalidHandle = 7,
IllegalCid = 8,
IllegalCodec = 9,
Serialization = 10,
Forbidden = 11,
BufferTooSmall = 12,
ReadOnly = 13,
}
impl std::fmt::Display for ErrorNumber {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use ErrorNumber::*;
f.write_str(match *self {
IllegalArgument => "illegal argument",
IllegalOperation => "illegal operation",
LimitExceeded => "limit exceeded",
AssertionFailed => "filecoin assertion failed",
InsufficientFunds => "insufficient funds",
NotFound => "resource not found",
InvalidHandle => "invalid ipld block handle",
IllegalCid => "illegal cid specification",
IllegalCodec => "illegal ipld codec",
Serialization => "serialization error",
Forbidden => "operation forbidden",
BufferTooSmall => "buffer too small",
ReadOnly => "execution context is read-only",
})
}
}