1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
use crate::{ByteArray, Op}; #[derive(Clone, Debug, Copy, Eq, PartialEq)] pub enum DataType { Generic, Integer, Boolean, ByteArray(Option<usize>), } pub type Integer = i32; #[derive(Clone, Debug, PartialEq, Eq)] pub enum StackItemData { Integer(Integer), Boolean(bool), ByteArray(ByteArray), } #[derive(Clone, Debug, Eq, PartialEq)] pub struct BitcoinInteger(pub Integer); #[derive(Clone, Debug, Eq, PartialEq)] pub struct BitcoinBoolean(pub bool); #[derive(Clone, Debug, Eq, PartialEq)] pub struct BitcoinByteArray(pub ByteArray); pub trait BitcoinDataType { type Type; fn to_data(&self) -> Self::Type; fn to_pushop(&self) -> Op; fn to_data_type(&self) -> DataType; } impl BitcoinDataType for Integer { type Type = BitcoinInteger; fn to_data(&self) -> Self::Type { BitcoinInteger(*self) } fn to_pushop(&self) -> Op { Op::PushInteger(*self) } fn to_data_type(&self) -> DataType { DataType::Integer } } impl BitcoinDataType for bool { type Type = BitcoinBoolean; fn to_data(&self) -> Self::Type { BitcoinBoolean(*self) } fn to_pushop(&self) -> Op { Op::PushBoolean(*self) } fn to_data_type(&self) -> DataType { DataType::Boolean } } impl BitcoinDataType for [u8] { type Type = BitcoinByteArray; fn to_data(&self) -> Self::Type { BitcoinByteArray(self.into()) } fn to_pushop(&self) -> Op { Op::PushByteArray { array: self.to_vec().into(), is_minimal: true, } } fn to_data_type(&self) -> DataType { DataType::ByteArray(None) } } impl BitcoinDataType for ByteArray { type Type = BitcoinByteArray; fn to_data(&self) -> Self::Type { BitcoinByteArray(self.clone()) } fn to_pushop(&self) -> Op { Op::PushByteArray { array: self.clone(), is_minimal: true, } } fn to_data_type(&self) -> DataType { DataType::ByteArray(Some(self.len())) } } impl From<Op> for StackItemData { fn from(op: Op) -> StackItemData { match op { Op::Code(_) => unimplemented!(), Op::Invalid(_) => unimplemented!(), Op::PushBoolean(boolean) => StackItemData::Boolean(boolean), Op::PushInteger(int) => StackItemData::Integer(int), Op::PushByteArray { array, .. } => StackItemData::ByteArray(array), } } }