From 9c1ebc7f2084f36e6e411d7f0b09c99042a8f7c6 Mon Sep 17 00:00:00 2001 From: relrelb Date: Sat, 26 Jun 2021 16:32:59 +0300 Subject: [PATCH] swf: Cleanup Error --- swf/src/error.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/swf/src/error.rs b/swf/src/error.rs index aa2a4df45..c29c86196 100644 --- a/swf/src/error.rs +++ b/swf/src/error.rs @@ -22,8 +22,10 @@ pub enum Error { tag_code: u16, source: Box, }, + /// An IO error occurred (probably unexpected EOF). IoError(io::Error), + /// This SWF requires unsupported features. Unsupported(borrow::Cow<'static, str>), } @@ -32,36 +34,40 @@ impl Error { /// Helper method to create `Error::Avm1ParseError`. #[inline] pub fn avm1_parse_error(opcode: u8) -> Self { - Error::Avm1ParseError { + Self::Avm1ParseError { opcode, source: None, } } + /// Helper method to create `Error::Avm1ParseError`. #[inline] pub fn avm1_parse_error_with_source(opcode: u8, source: impl error::Error + 'static) -> Self { - Error::Avm1ParseError { + Self::Avm1ParseError { opcode, source: Some(Box::new(source)), } } + /// Helper method to create `Error::InvalidData`. #[inline] pub fn invalid_data(message: impl Into>) -> Self { - Error::InvalidData(message.into()) + Self::InvalidData(message.into()) } + /// Helper method to create `Error::SwfParseError`. #[inline] pub fn swf_parse_error(tag_code: u16, source: impl error::Error + 'static) -> Self { - Error::SwfParseError { + Self::SwfParseError { tag_code, source: Box::new(source), } } + /// Helper method to create `Error::Unsupported`. #[inline] pub fn unsupported(message: impl Into>) -> Self { - Error::Unsupported(message.into()) + Self::Unsupported(message.into()) } } @@ -69,7 +75,7 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use crate::num_traits::FromPrimitive; match self { - Error::Avm1ParseError { opcode, source } => { + Self::Avm1ParseError { opcode, source } => { let op = crate::avm1::opcode::OpCode::from_u8(*opcode); "Error parsing AVM1 action ".fmt(f)?; if let Some(op) = op { @@ -82,7 +88,7 @@ impl fmt::Display for Error { } Ok(()) } - Error::SwfParseError { tag_code, source } => { + Self::SwfParseError { tag_code, source } => { "Error parsing SWF tag ".fmt(f)?; if let Some(tag_code) = TagCode::from_u16(*tag_code) { write!(f, "{:?}", tag_code)?; @@ -92,9 +98,9 @@ impl fmt::Display for Error { write!(f, ": {}", source)?; Ok(()) } - Error::IoError(e) => e.fmt(f), - Error::InvalidData(message) => write!(f, "Invalid data: {}", message), - Error::Unsupported(message) => write!(f, "Unsupported data: {}", message), + Self::IoError(e) => e.fmt(f), + Self::InvalidData(message) => write!(f, "Invalid data: {}", message), + Self::Unsupported(message) => write!(f, "Unsupported data: {}", message), } } } @@ -103,17 +109,17 @@ impl error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use std::ops::Deref; match self { - Error::Avm1ParseError { source, .. } => source.as_ref().map(|s| s.deref()), - Error::IoError(e) => e.source(), - Error::InvalidData(_) => None, - Error::SwfParseError { source, .. } => Some(source.as_ref()), - Error::Unsupported(_) => None, + Self::Avm1ParseError { source, .. } => source.as_ref().map(|s| s.deref()), + Self::IoError(e) => e.source(), + Self::InvalidData(_) => None, + Self::SwfParseError { source, .. } => Some(source.as_ref()), + Self::Unsupported(_) => None, } } } impl From for Error { - fn from(error: io::Error) -> Error { - Error::IoError(error) + fn from(error: io::Error) -> Self { + Self::IoError(error) } }