use std::error;
use std::fmt;
use std::io::Error as IoError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Decoder(DecoderError),
Encoder(EncoderError),
Operand(OperandError),
Io(IoError),
Unknown,
}
impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Decoder(e) => write!(f, "[Decoder error] {}", e),
Error::Encoder(e) => write!(f, "[Encoder error] {}", e),
Error::Operand(e) => write!(f, "[Operand error] {}", e),
Error::Io(e) => write!(f, "[IO error] {}", e),
Error::Unknown => write!(f, "Unknown error occured"),
}
}
}
impl From<DecoderError> for Error {
fn from(error: DecoderError) -> Self {
Error::Decoder(error)
}
}
impl From<EncoderError> for Error {
fn from(error: EncoderError) -> Self {
Error::Encoder(error)
}
}
impl From<OperandError> for Error {
fn from(error: OperandError) -> Self {
Error::Operand(error)
}
}
impl From<IoError> for Error {
fn from(error: IoError) -> Self {
Error::Io(error)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum DecoderError {
Unknown,
}
impl error::Error for DecoderError {}
impl fmt::Display for DecoderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unknown => write!(f, "Unknown error occured"),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum EncoderError {
Unknown,
}
impl error::Error for EncoderError {}
impl fmt::Display for EncoderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unknown => write!(f, "Unknown error occured"),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum OperandError {
RegisterOutOfRange(u32),
MnemonicConditionOutOfRange(u32),
ConditionOutOfRange(u32),
AssemblerQualifierOutOfRange(u32),
OperandIndexOutOfRange(usize, usize),
Unknown,
}
impl error::Error for OperandError {}
impl fmt::Display for OperandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RegisterOutOfRange(value) => {
write!(f, "Register index \"{value}\" out of bounds")
}
Self::MnemonicConditionOutOfRange(value) => {
write!(f, "Mnemonic condition \"{value}\" out of bounds")
}
Self::ConditionOutOfRange(value) => {
write!(f, "Condition \"{value}\" out of bounds")
}
Self::AssemblerQualifierOutOfRange(value) => {
write!(f, "Assembler qualifier \"{value}\" out of bounds")
}
Self::OperandIndexOutOfRange(value, max) => {
write!(
f,
"Operand index \"{value}\" out of bounds (maximum allowed is {max})"
)
}
Self::Unknown => write!(f, "Unknown error occured"),
}
}
}