From 3863d7023e1c7ae71f989e172200898d8a658f71 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 3 Jul 2021 19:36:00 -0700 Subject: define a standard decode error for client libraries to use additional variants will require clients to implement DecodeError, still --- src/lib.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index a5b8767..89b3579 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { - fn next(&mut self) -> Result; +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 { - 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>(&self, bytes: &mut T) -> Result { let mut inst = A::Instruction::default(); -- cgit v1.1