use crate::avm1::opcode::OpCode;
use crate::tag_code::TagCode;
use std::{borrow, error, fmt, io};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Avm1ParseError {
opcode: u8,
source: Option<Box<dyn error::Error + 'static>>,
},
InvalidData(borrow::Cow<'static, str>),
SwfParseError {
tag_code: u16,
source: Box<dyn error::Error + 'static>,
},
IoError(io::Error),
Unsupported(borrow::Cow<'static, str>),
}
impl Error {
#[inline]
pub fn avm1_parse_error(opcode: u8) -> Self {
Self::Avm1ParseError {
opcode,
source: None,
}
}
#[inline]
pub fn avm1_parse_error_with_source(opcode: u8, source: impl error::Error + 'static) -> Self {
Self::Avm1ParseError {
opcode,
source: Some(Box::new(source)),
}
}
#[inline]
pub fn invalid_data(message: impl Into<borrow::Cow<'static, str>>) -> Self {
Self::InvalidData(message.into())
}
#[inline]
pub fn swf_parse_error(tag_code: u16, source: impl error::Error + 'static) -> Self {
Self::SwfParseError {
tag_code,
source: Box::new(source),
}
}
#[inline]
pub fn unsupported(message: impl Into<borrow::Cow<'static, str>>) -> Self {
Self::Unsupported(message.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Avm1ParseError { opcode, source } => {
write!(f, "Error parsing AVM1 action {}", OpCode::format(*opcode))?;
if let Some(source) = source {
write!(f, ": {}", source)?;
}
Ok(())
}
Self::SwfParseError { tag_code, source } => {
write!(
f,
"Error parsing SWF tag {}: {}",
TagCode::format(*tag_code),
source
)
}
Self::IoError(e) => e.fmt(f),
Self::InvalidData(message) => write!(f, "Invalid data: {}", message),
Self::Unsupported(message) => write!(f, "Unsupported data: {}", message),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use std::ops::Deref;
match self {
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) -> Self {
Self::IoError(error)
}
}