1use crate::{ByteArray, Opcode};
2
3#[derive(Clone, Eq, PartialEq)]
4pub enum Op {
5 Code(Opcode),
6 Invalid(u8),
7 PushByteArray { array: ByteArray, is_minimal: bool },
8 PushBoolean(bool),
9 PushInteger(i32),
10}
11
12impl std::fmt::Display for Op {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
14 match self {
15 Op::Code(code) => write!(f, "{:?}", code),
16 Op::Invalid(code) => write!(f, "{:02x}", code),
17 Op::PushByteArray { array, .. } => write!(f, "0x{:?}", hex::encode(&array)),
18 Op::PushBoolean(boolean) => {
19 write!(f, "{}", if *boolean { "OP_TRUE" } else { "OP_FALSE" })
20 }
21 Op::PushInteger(int) => write!(f, "{}", int),
22 }
23 }
24}
25
26impl std::fmt::Debug for Op {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
28 match self {
29 Op::Code(code) => write!(f, "Code({:?})", code),
30 Op::Invalid(code) => write!(f, "Invalid(0x{:02x})", code),
31 Op::PushByteArray { array, .. } => {
32 write!(f, "PushByteArray(hex!({:?}).to_vec())", hex::encode(&array))
33 }
34 Op::PushBoolean(boolean) => write!(f, "PushBoolean({:?})", boolean),
35 Op::PushInteger(int) => write!(f, "PushInteger({})", int),
36 }
37 }
38}
39
40impl From<ByteArray> for Op {
41 fn from(array: ByteArray) -> Self {
42 Op::PushByteArray {
43 array,
44 is_minimal: true,
45 }
46 }
47}