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