diff options
author | iximeow <me@iximeow.net> | 2021-07-03 19:36:00 -0700 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2021-07-03 19:36:00 -0700 |
commit | 3863d7023e1c7ae71f989e172200898d8a658f71 (patch) | |
tree | a0a39ad211ddf5f701fc19437312aec96cbd9ecd | |
parent | 7c53435a1c346fb224e9f5ef08304016e240be62 (diff) |
define a standard decode error for client libraries to use
additional variants will require clients to implement DecodeError, still
-rw-r--r-- | src/lib.rs | 23 |
1 files changed, 17 insertions, 6 deletions
@@ -38,17 +38,28 @@ pub trait DecodeError { fn bad_operand(&self) -> bool; } -pub enum ReadError { +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +pub enum StandardDecodeError { ExhaustedInput, - IOError(&'static str), + InvalidOpcode, + InvalidOperand, } -pub trait Reader<Item> { - fn next(&mut self) -> Result<Item, ReadError>; +impl fmt::Display for StandardDecodeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + StandardDecodeError::ExhaustedInput => write!(f, "exhausted input"), + StandardDecodeError::InvalidOpcode => write!(f, "invalid opcode"), + StandardDecodeError::InvalidOperand => write!(f, "invalid operand"), + } + } } -pub trait Decoder<A: Arch + ?Sized> { - type Error: DecodeError + Debug + Display; +impl DecodeError for StandardDecodeError { + fn data_exhausted(&self) -> bool { *self == StandardDecodeError::ExhaustedInput } + fn bad_opcode(&self) -> bool { *self == StandardDecodeError::InvalidOpcode } + fn bad_operand(&self) -> bool { *self == StandardDecodeError::InvalidOperand } +} fn decode<T: Reader<A::Word>>(&self, bytes: &mut T) -> Result<A::Instruction, Self::Error> { let mut inst = A::Instruction::default(); |