swf: Cleanup Error
This commit is contained in:
parent
e3f1fe2004
commit
9c1ebc7f20
|
@ -22,8 +22,10 @@ pub enum Error {
|
||||||
tag_code: u16,
|
tag_code: u16,
|
||||||
source: Box<dyn error::Error + 'static>,
|
source: Box<dyn error::Error + 'static>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// An IO error occurred (probably unexpected EOF).
|
/// An IO error occurred (probably unexpected EOF).
|
||||||
IoError(io::Error),
|
IoError(io::Error),
|
||||||
|
|
||||||
/// This SWF requires unsupported features.
|
/// This SWF requires unsupported features.
|
||||||
Unsupported(borrow::Cow<'static, str>),
|
Unsupported(borrow::Cow<'static, str>),
|
||||||
}
|
}
|
||||||
|
@ -32,36 +34,40 @@ impl Error {
|
||||||
/// Helper method to create `Error::Avm1ParseError`.
|
/// Helper method to create `Error::Avm1ParseError`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn avm1_parse_error(opcode: u8) -> Self {
|
pub fn avm1_parse_error(opcode: u8) -> Self {
|
||||||
Error::Avm1ParseError {
|
Self::Avm1ParseError {
|
||||||
opcode,
|
opcode,
|
||||||
source: None,
|
source: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to create `Error::Avm1ParseError`.
|
/// Helper method to create `Error::Avm1ParseError`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn avm1_parse_error_with_source(opcode: u8, source: impl error::Error + 'static) -> Self {
|
pub fn avm1_parse_error_with_source(opcode: u8, source: impl error::Error + 'static) -> Self {
|
||||||
Error::Avm1ParseError {
|
Self::Avm1ParseError {
|
||||||
opcode,
|
opcode,
|
||||||
source: Some(Box::new(source)),
|
source: Some(Box::new(source)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to create `Error::InvalidData`.
|
/// Helper method to create `Error::InvalidData`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn invalid_data(message: impl Into<borrow::Cow<'static, str>>) -> Self {
|
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`.
|
/// Helper method to create `Error::SwfParseError`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn swf_parse_error(tag_code: u16, source: impl error::Error + 'static) -> Self {
|
pub fn swf_parse_error(tag_code: u16, source: impl error::Error + 'static) -> Self {
|
||||||
Error::SwfParseError {
|
Self::SwfParseError {
|
||||||
tag_code,
|
tag_code,
|
||||||
source: Box::new(source),
|
source: Box::new(source),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to create `Error::Unsupported`.
|
/// Helper method to create `Error::Unsupported`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn unsupported(message: impl Into<borrow::Cow<'static, str>>) -> Self {
|
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 {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
use crate::num_traits::FromPrimitive;
|
use crate::num_traits::FromPrimitive;
|
||||||
match self {
|
match self {
|
||||||
Error::Avm1ParseError { opcode, source } => {
|
Self::Avm1ParseError { opcode, source } => {
|
||||||
let op = crate::avm1::opcode::OpCode::from_u8(*opcode);
|
let op = crate::avm1::opcode::OpCode::from_u8(*opcode);
|
||||||
"Error parsing AVM1 action ".fmt(f)?;
|
"Error parsing AVM1 action ".fmt(f)?;
|
||||||
if let Some(op) = op {
|
if let Some(op) = op {
|
||||||
|
@ -82,7 +88,7 @@ impl fmt::Display for Error {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Error::SwfParseError { tag_code, source } => {
|
Self::SwfParseError { tag_code, source } => {
|
||||||
"Error parsing SWF tag ".fmt(f)?;
|
"Error parsing SWF tag ".fmt(f)?;
|
||||||
if let Some(tag_code) = TagCode::from_u16(*tag_code) {
|
if let Some(tag_code) = TagCode::from_u16(*tag_code) {
|
||||||
write!(f, "{:?}", tag_code)?;
|
write!(f, "{:?}", tag_code)?;
|
||||||
|
@ -92,9 +98,9 @@ impl fmt::Display for Error {
|
||||||
write!(f, ": {}", source)?;
|
write!(f, ": {}", source)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Error::IoError(e) => e.fmt(f),
|
Self::IoError(e) => e.fmt(f),
|
||||||
Error::InvalidData(message) => write!(f, "Invalid data: {}", message),
|
Self::InvalidData(message) => write!(f, "Invalid data: {}", message),
|
||||||
Error::Unsupported(message) => write!(f, "Unsupported 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)> {
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
match self {
|
match self {
|
||||||
Error::Avm1ParseError { source, .. } => source.as_ref().map(|s| s.deref()),
|
Self::Avm1ParseError { source, .. } => source.as_ref().map(|s| s.deref()),
|
||||||
Error::IoError(e) => e.source(),
|
Self::IoError(e) => e.source(),
|
||||||
Error::InvalidData(_) => None,
|
Self::InvalidData(_) => None,
|
||||||
Error::SwfParseError { source, .. } => Some(source.as_ref()),
|
Self::SwfParseError { source, .. } => Some(source.as_ref()),
|
||||||
Error::Unsupported(_) => None,
|
Self::Unsupported(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for Error {
|
impl From<io::Error> for Error {
|
||||||
fn from(error: io::Error) -> Error {
|
fn from(error: io::Error) -> Self {
|
||||||
Error::IoError(error)
|
Self::IoError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue