swf: Extract `Opcode::format` and `TagCode::format`

This commit is contained in:
relrelb 2021-06-26 16:35:32 +03:00 committed by Mike Welsh
parent 38ae22e834
commit 04af276ecb
4 changed files with 30 additions and 21 deletions

View File

@ -1,4 +1,3 @@
#[allow(dead_code, clippy::useless_attribute)]
#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive)]
pub enum OpCode {
End = 0x00,
@ -116,3 +115,17 @@ pub enum OpCode {
Call = 0x9E,
GotoFrame2 = 0x9F,
}
impl OpCode {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
pub fn format(opcode: u8) -> String {
if let Some(op) = Self::from_u8(opcode) {
format!("{:?}", op)
} else {
format!("Unknown({})", opcode)
}
}
}

View File

@ -87,7 +87,6 @@ impl<'a> Reader<'a> {
/// The final `length` returned will be total length of the action, including sub-blocks.
#[inline]
fn read_op(&mut self, opcode: u8, length: &mut usize) -> Result<Option<Action<'a>>> {
use num_traits::FromPrimitive;
let action = if let Some(op) = OpCode::from_u8(opcode) {
match op {
OpCode::End => return Ok(None),

View File

@ -1,3 +1,4 @@
use crate::avm1::opcode::OpCode;
use crate::tag_code::TagCode;
use std::{borrow, error, fmt, io};
@ -73,30 +74,21 @@ impl Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use crate::num_traits::FromPrimitive;
match self {
Self::Avm1ParseError { opcode, source } => {
let op = crate::avm1::opcode::OpCode::from_u8(*opcode);
"Error parsing AVM1 action ".fmt(f)?;
if let Some(op) = op {
write!(f, "{:?}", op)?;
} else {
write!(f, "Unknown({})", opcode)?;
};
write!(f, "Error parsing AVM1 action {}", OpCode::format(*opcode))?;
if let Some(source) = source {
write!(f, ": {}", source)?;
}
Ok(())
}
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)?;
} else {
write!(f, "Unknown({})", tag_code)?;
};
write!(f, ": {}", source)?;
Ok(())
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),

View File

@ -1,6 +1,3 @@
#![allow(clippy::useless_attribute)]
#[allow(dead_code)]
#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive)]
pub enum TagCode {
End = 0,
@ -95,4 +92,12 @@ impl TagCode {
pub fn from_u16(n: u16) -> Option<Self> {
num_traits::FromPrimitive::from_u16(n)
}
pub fn format(tag_code: u16) -> String {
if let Some(tag_code) = TagCode::from_u16(tag_code) {
format!("{:?}", tag_code)
} else {
format!("Unknown({})", tag_code)
}
}
}