1use std::fmt;
2use std::string::String;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum NeoError {
10 InvalidOperation,
12 InvalidArgument,
14 InvalidType,
16 OutOfBounds,
18 DivisionByZero,
20 Overflow,
22 Underflow,
24 NullReference,
26 InvalidState,
28 Custom(String),
30}
31
32impl fmt::Display for NeoError {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 NeoError::InvalidOperation => write!(f, "Invalid operation: the requested operation cannot be performed in the current context"),
36 NeoError::InvalidArgument => write!(f, "Invalid argument: one or more arguments have invalid values or types"),
37 NeoError::InvalidType => write!(f, "Invalid type: type mismatch encountered during execution"),
38 NeoError::OutOfBounds => write!(f, "Out of bounds: index or offset exceeds valid range"),
39 NeoError::DivisionByZero => write!(f, "Division by zero: cannot divide by zero"),
40 NeoError::Overflow => write!(f, "Overflow: arithmetic operation resulted in overflow"),
41 NeoError::Underflow => write!(f, "Underflow: arithmetic operation resulted in underflow"),
42 NeoError::NullReference => write!(f, "Null reference: attempted to access a null or invalid reference"),
43 NeoError::InvalidState => write!(f, "Invalid state: internal state is invalid or corrupted"),
44 NeoError::Custom(msg) => write!(f, "{}", msg),
45 }
46 }
47}
48
49impl NeoError {
50 pub fn new(message: &str) -> Self {
60 NeoError::Custom(message.to_string())
61 }
62
63 pub fn message(&self) -> &str {
65 match self {
66 NeoError::Custom(msg) => msg,
67 _ => self.as_str(),
68 }
69 }
70
71 pub fn as_str(&self) -> &'static str {
73 match self {
74 NeoError::InvalidOperation => "InvalidOperation",
75 NeoError::InvalidArgument => "InvalidArgument",
76 NeoError::InvalidType => "InvalidType",
77 NeoError::OutOfBounds => "OutOfBounds",
78 NeoError::DivisionByZero => "DivisionByZero",
79 NeoError::Overflow => "Overflow",
80 NeoError::Underflow => "Underflow",
81 NeoError::NullReference => "NullReference",
82 NeoError::InvalidState => "InvalidState",
83 NeoError::Custom(_) => "Custom",
84 }
85 }
86}
87
88pub type NeoResult<T> = Result<T, NeoError>;