use std::fmt;
pub type Result<T> = std::result::Result<T, HookError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum HookError {
Unknown = -1,
AlreadyInitialized = 1,
NotInitialized = 2,
AlreadyCreated = 3,
NotCreated = 4,
Enabled = 5,
Disabled = 6,
NotExecutable = 7,
UnsupportedFunction = 8,
MemoryAlloc = 9,
MemoryProtect = 10,
ModuleNotFound = 11,
FunctionNotFound = 12,
}
impl HookError {
pub fn as_str(self) -> &'static str {
match self {
HookError::Unknown => "Unknown error",
HookError::AlreadyInitialized => "Already initialized",
HookError::NotInitialized => "Not initialized",
HookError::AlreadyCreated => "Already created",
HookError::NotCreated => "Not created",
HookError::Enabled => "Already enabled",
HookError::Disabled => "Already disabled",
HookError::NotExecutable => "Not executable",
HookError::UnsupportedFunction => "Unsupported function",
HookError::MemoryAlloc => "Memory allocation failed",
HookError::MemoryProtect => "Memory protection failed",
HookError::ModuleNotFound => "Module not found",
HookError::FunctionNotFound => "Function not found",
}
}
pub fn from_code(code: i32) -> Self {
match code {
-1 => HookError::Unknown,
1 => HookError::AlreadyInitialized,
2 => HookError::NotInitialized,
3 => HookError::AlreadyCreated,
4 => HookError::NotCreated,
5 => HookError::Enabled,
6 => HookError::Disabled,
7 => HookError::NotExecutable,
8 => HookError::UnsupportedFunction,
9 => HookError::MemoryAlloc,
10 => HookError::MemoryProtect,
11 => HookError::ModuleNotFound,
12 => HookError::FunctionNotFound,
_ => HookError::Unknown,
}
}
}
impl fmt::Display for HookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl std::error::Error for HookError {}
pub const OK: i32 = 0;
pub fn ok<T>(value: T) -> Result<T> {
Ok(value)
}
pub fn err<T>(error: HookError) -> Result<T> {
Err(error)
}