diff --git a/swf/src/avm1/opcode.rs b/swf/src/avm1/opcode.rs index 1cfc84838..8a07a8972 100644 --- a/swf/src/avm1/opcode.rs +++ b/swf/src/avm1/opcode.rs @@ -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 { + 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) + } + } +} diff --git a/swf/src/avm1/read.rs b/swf/src/avm1/read.rs index 2196e3486..a8a817d52 100644 --- a/swf/src/avm1/read.rs +++ b/swf/src/avm1/read.rs @@ -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>> { - use num_traits::FromPrimitive; let action = if let Some(op) = OpCode::from_u8(opcode) { match op { OpCode::End => return Ok(None), diff --git a/swf/src/error.rs b/swf/src/error.rs index c29c86196..f21039560 100644 --- a/swf/src/error.rs +++ b/swf/src/error.rs @@ -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), diff --git a/swf/src/tag_code.rs b/swf/src/tag_code.rs index 4f22a1890..e74d77d62 100644 --- a/swf/src/tag_code.rs +++ b/swf/src/tag_code.rs @@ -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 { 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) + } + } }