use alloc::string::String;
use thiserror::Error;
pub type Result<T> = core::result::Result<T, CoffeeLdrError>;
#[derive(Debug, Error)]
pub enum CoffeeLdrError {
#[error("{0}")]
Msg(String),
#[error("binrw error: {0}")]
Binrw(binrw::Error),
#[error("hex error: {0}")]
Hex(hex::FromHexError),
#[error("io error: {0}")]
Io(binrw::io::Error),
#[error("coff error: {0}")]
CoffError(#[from] CoffError),
#[error("memory allocation error: code {0}")]
MemoryAllocationError(u32),
#[error("memory protection error: code {0}")]
MemoryProtectionError(u32),
#[error("invalid symbol format: '{0}'")]
InvalidSymbolFormat(String),
#[error("invalid relocation type: {0}")]
InvalidRelocationType(u16),
#[error("symbol not found: '{0}'")]
FunctionNotFound(String),
#[error("internal symbol not found: '{0}'")]
FunctionInternalNotFound(String),
#[error("module not found: '{0}'")]
ModuleNotFound(String),
#[error("error loading COFF file")]
ParsingError,
#[error("arch mismatch: expected {expected}, actual {actual}")]
ArchitectureMismatch { expected: &'static str, actual: &'static str },
#[error("too many symbols (max {0})")]
TooManySymbols(usize),
#[error("symbol parse error: '{0}'")]
ParseError(String),
#[error("symbol ignored (missing required prefix)")]
SymbolIgnored,
#[error("output read error")]
OutputError,
#[error("missing .text section in target module")]
StompingTextSectionNotFound,
#[error("stomping size overflow")]
StompingSizeOverflow,
#[error("missing base address for target section")]
MissingStompingBaseAddress,
}
#[derive(Debug, Error)]
pub enum CoffError {
#[error("file read error: {0}")]
FileReadError(String),
#[error("invalid COFF header")]
InvalidCoffFile,
#[error("invalid COFF symbols")]
InvalidCoffSymbolsFile,
#[error("invalid COFF section headers")]
InvalidCoffSectionFile,
#[error("unsupported architecture")]
UnsupportedArchitecture,
#[error("invalid number of sections or symbols")]
InvalidSectionsOrSymbols,
#[error("section limit exceeded (max 96)")]
SectionLimitExceeded,
}
impl From<hex::FromHexError> for CoffeeLdrError {
fn from(err: hex::FromHexError) -> Self {
CoffeeLdrError::Hex(err)
}
}
impl From<binrw::io::Error> for CoffeeLdrError {
fn from(err: binrw::io::Error) -> Self {
CoffeeLdrError::Binrw(binrw::Error::Io(err))
}
}