swf: Cleanup Error

This commit is contained in:
relrelb 2021-06-26 16:32:59 +03:00 committed by relrelb
parent e3f1fe2004
commit 9c1ebc7f20
1 changed files with 23 additions and 17 deletions

View File

@ -22,8 +22,10 @@ pub enum Error {
tag_code: u16,
source: Box<dyn error::Error + 'static>,
},
/// 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<borrow::Cow<'static, str>>) -> 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<borrow::Cow<'static, str>>) -> 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<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::IoError(error)
fn from(error: io::Error) -> Self {
Self::IoError(error)
}
}