charm 0.0.1

ARM assembler & disassembler generated from the ARM exploration tools.
Documentation
//! Error objects used by this crate.

use std::error;
use std::fmt;
use std::io::Error as IoError;

// ------------------------------------------------------------------------------------------------
// Global error types.
// ------------------------------------------------------------------------------------------------

/// Convenient `Result` type for errors.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type used for all operations performed by Charm.
#[derive(Debug)]
pub enum Error {
    /// Decoder-related errors.
    Decoder(DecoderError),
    /// Encoder-related errors.
    Encoder(EncoderError),
    /// Operand-related errors.
    Operand(OperandError),
    /// I/O-related errors.
    Io(IoError),
    /// Fallback unknown error type.
    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)
    }
}

// ------------------------------------------------------------------------------------------------
// Decoder error types.
// ------------------------------------------------------------------------------------------------

/// Decoder-related errors.
#[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"),
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Encoder error types.
// ------------------------------------------------------------------------------------------------

/// Encoder-related errors.
#[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"),
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Operand error types.
// ------------------------------------------------------------------------------------------------

/// Operand-related errors.
#[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"),
        }
    }
}