|
| 1 | +use std::io::Write; |
| 2 | + |
| 3 | +use simplicityhl::{ |
| 4 | + jet::{JetHL, SourceJetClassification, TargetJetClassification}, |
| 5 | + simplicity::{ |
| 6 | + decode::Error, |
| 7 | + decode_bits, |
| 8 | + jet::{type_name::TypeName, Jet}, |
| 9 | + BitIter, BitWriter, Cmr, Cost, Error as SimplicityError, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +pub struct ExternalJet { |
| 14 | + pub index: u64, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
| 18 | +pub enum HappyJet { |
| 19 | + Verify, |
| 20 | +} |
| 21 | + |
| 22 | +impl HappyJet { |
| 23 | + pub fn index(&self) -> u64 { |
| 24 | + match self { |
| 25 | + HappyJet::Verify => 0, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn from_index(index: u64) -> Option<Self> { |
| 30 | + match index { |
| 31 | + 0 => Some(HappyJet::Verify), |
| 32 | + _ => None, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Jet for HappyJet { |
| 38 | + fn cmr(&self) -> Cmr { |
| 39 | + let bytes = match self { |
| 40 | + HappyJet::Verify => [ |
| 41 | + 0xcd, 0xca, 0x2a, 0x05, 0xe5, 0x2c, 0xef, 0xa5, 0x9d, 0xc7, 0xa5, 0xb0, 0xda, 0xe2, |
| 42 | + 0x20, 0x98, 0xfb, 0x89, 0x6e, 0x39, 0x13, 0xbf, 0xdd, 0x44, 0x6b, 0x59, 0x4e, 0x1f, |
| 43 | + 0x92, 0x50, 0x78, 0x3e, |
| 44 | + ], |
| 45 | + }; |
| 46 | + Cmr::from_byte_array(bytes) |
| 47 | + } |
| 48 | + |
| 49 | + fn source_ty(&self) -> TypeName { |
| 50 | + let name: &'static [u8] = match self { |
| 51 | + HappyJet::Verify => b"2", |
| 52 | + }; |
| 53 | + |
| 54 | + TypeName(name) |
| 55 | + } |
| 56 | + |
| 57 | + fn target_ty(&self) -> TypeName { |
| 58 | + let name: &'static [u8] = match self { |
| 59 | + HappyJet::Verify => b"1", |
| 60 | + }; |
| 61 | + |
| 62 | + TypeName(name) |
| 63 | + } |
| 64 | + |
| 65 | + fn encode(&self, w: &mut BitWriter<&mut dyn Write>) -> std::io::Result<usize> { |
| 66 | + let (n, len) = match self { |
| 67 | + HappyJet::Verify => (0, 1), |
| 68 | + }; |
| 69 | + |
| 70 | + w.write_bits_be(n, len) |
| 71 | + } |
| 72 | + |
| 73 | + fn decode<I: Iterator<Item = u8>>(bits: &mut BitIter<I>) -> Result<Self, Error> |
| 74 | + where |
| 75 | + Self: Sized, |
| 76 | + { |
| 77 | + decode_bits!(bits, { |
| 78 | + 0 => {HappyJet::Verify}, |
| 79 | + 1 => {} |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + fn cost(&self) -> Cost { |
| 84 | + match self { |
| 85 | + HappyJet::Verify => Cost::from_milliweight(44), |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + fn parse(s: &str) -> Result<Self, SimplicityError> |
| 90 | + where |
| 91 | + Self: Sized, |
| 92 | + { |
| 93 | + match s { |
| 94 | + "verify" => Ok(HappyJet::Verify), |
| 95 | + x => Err(SimplicityError::InvalidJetName(x.to_owned())), |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl std::fmt::Display for HappyJet { |
| 101 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 102 | + match self { |
| 103 | + HappyJet::Verify => write!(f, "verify"), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl JetHL for HappyJet { |
| 109 | + fn source_jet_classification(&self) -> SourceJetClassification { |
| 110 | + match self { |
| 111 | + HappyJet::Verify => SourceJetClassification::Custom(vec![simplicityhl::jet::bool()]), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn target_jet_classification(&self) -> TargetJetClassification { |
| 116 | + match self { |
| 117 | + HappyJet::Verify => TargetJetClassification::Unary, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + fn is_disabled(&self) -> bool { |
| 122 | + false |
| 123 | + } |
| 124 | + |
| 125 | + fn clone_box(&self) -> Box<dyn JetHL> { |
| 126 | + Box::new(*self) |
| 127 | + } |
| 128 | + |
| 129 | + fn as_jet(&self) -> &dyn Jet { |
| 130 | + self |
| 131 | + } |
| 132 | +} |
0 commit comments