From 375bb2f18756436491d78f6efce806a63089d24d Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 22 Sep 2022 16:25:58 +0800 Subject: [PATCH 01/19] feat: Add cota extension mol --- src/extension.mol | 64 + src/extension.rs | 6261 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 6325 insertions(+) create mode 100644 src/extension.mol create mode 100644 src/extension.rs diff --git a/src/extension.mol b/src/extension.mol new file mode 100644 index 0000000..cbda691 --- /dev/null +++ b/src/extension.mol @@ -0,0 +1,64 @@ +import common; + +array Byte6 [byte; 6]; +array Byte9 [byte; 9]; +array Byte20 [byte; 20]; +array Byte24 [byte; 24]; + +vector LockScriptVec ; +vector ExtensionVec ; + +table ExtensionLeaves { + keys: ExtensionVec, + values: ExtensionVec, + proof: Bytes, +} + +// SubKeys +struct SubKey { + smt_type: Uint16, + sub_type: Byte6, + ext_data: Uint32, + reserved: Byte20, +} +struct SubValue { + key_type: Uint16, + pubkey_hash: Byte20, + reserved: Byte9, + padding: byte, +} +vector SubKeyVec ; +vector SubValueVec ; +table SubKeyEntry { + keys: SubKeyVec, + values: SubValueVec, +} +option SubKeyEntryOpt (SubKeyEntry); + + +// Social Recovery +struct SocialKey { + smt_type: Uint16, + sub_type: Byte6, + reserved: Byte24, +} +table SocialValue { + recovery_mode: byte, + must: byte, + total: byte, + signers: LockScriptVec, +} +vector SocialKeyVec ; +vector SocialValueVec ; +table SocialEntry { + keys: SocialKeyVec, + values: SocialValueVec, +} +option SocialEntryOpt (SocialEntry); + + +table ExtensionEntries { + leaves: ExtensionLeaves, + subkey: SubKeyEntryOpt, + social: SocialEntryOpt, +} \ No newline at end of file diff --git a/src/extension.rs b/src/extension.rs new file mode 100644 index 0000000..eb3eb05 --- /dev/null +++ b/src/extension.rs @@ -0,0 +1,6261 @@ +// Generated by Molecule 0.7.3 + +#![allow(unused_imports)] +#![allow(dead_code)] +#![allow(clippy::if_same_then_else)] + +use super::ckb_types::prelude::*; +use super::molecule::{self, prelude::*}; +extern crate alloc; +pub use alloc::vec::*; +// these lines above are manually added + +use super::common::*; +use molecule::prelude::*; +#[derive(Clone)] +pub struct Byte6(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte6 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0]; + Byte6::new_unchecked(v.into()) + } +} +impl Byte6 { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte6 { + type Builder = Byte6Builder; + + const NAME: &'static str = "Byte6"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte6(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte6Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte6Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte6Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte6Reader<'r> { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte6Reader<'r> { + type Entity = Byte6; + + const NAME: &'static str = "Byte6Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte6Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte6Builder(pub(crate) [Byte; 6]); +impl ::core::fmt::Debug for Byte6Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte6Builder { + fn default() -> Self { + Byte6Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte6Builder { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn set(mut self, v: [Byte; 6]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } +} +impl molecule::prelude::Builder for Byte6Builder { + type Entity = Byte6; + + const NAME: &'static str = "Byte6Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte6::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte9(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte9 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte9::new_unchecked(v.into()) + } +} +impl Byte9 { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { + Byte9Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte9 { + type Builder = Byte9Builder; + + const NAME: &'static str = "Byte9"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte9(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte9Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte9Reader<'r> { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte9Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte9Builder(pub(crate) [Byte; 9]); +impl ::core::fmt::Debug for Byte9Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte9Builder { + fn default() -> Self { + Byte9Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte9Builder { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn set(mut self, v: [Byte; 9]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } +} +impl molecule::prelude::Builder for Byte9Builder { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte9::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte20(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte20 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte20::new_unchecked(v.into()) + } +} +impl Byte20 { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte20 { + type Builder = Byte20Builder; + + const NAME: &'static str = "Byte20"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte20(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte20Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte20Reader<'r> { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte20Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte20Builder(pub(crate) [Byte; 20]); +impl ::core::fmt::Debug for Byte20Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte20Builder { + fn default() -> Self { + Byte20Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte20Builder { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn set(mut self, v: [Byte; 20]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } +} +impl molecule::prelude::Builder for Byte20Builder { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte20::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte24(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte24 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + Byte24::new_unchecked(v.into()) + } +} +impl Byte24 { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { + Byte24Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte24 { + type Builder = Byte24Builder; + + const NAME: &'static str = "Byte24"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte24(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte24Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte24Reader<'r> { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte24Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte24Builder(pub(crate) [Byte; 24]); +impl ::core::fmt::Debug for Byte24Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte24Builder { + fn default() -> Self { + Byte24Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte24Builder { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn set(mut self, v: [Byte; 24]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } +} +impl molecule::prelude::Builder for Byte24Builder { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte24::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct LockScriptVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for LockScriptVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + LockScriptVec::new_unchecked(v.into()) + } +} +impl LockScriptVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Bytes { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + Bytes::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> LockScriptVecReader<'r> { + LockScriptVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for LockScriptVec { + type Builder = LockScriptVecBuilder; + + const NAME: &'static str = "LockScriptVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + LockScriptVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + LockScriptVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + LockScriptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct LockScriptVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> LockScriptVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for LockScriptVecReader<'r> { + type Entity = LockScriptVec; + + const NAME: &'static str = "LockScriptVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + LockScriptVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + BytesReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct LockScriptVecBuilder(pub(crate) Vec); +impl LockScriptVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Bytes) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Bytes) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for LockScriptVecBuilder { + type Entity = LockScriptVec; + + const NAME: &'static str = "LockScriptVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + LockScriptVec::new_unchecked(inner.into()) + } +} +pub struct LockScriptVecIterator(LockScriptVec, usize, usize); +impl ::core::iter::Iterator for LockScriptVecIterator { + type Item = Bytes; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for LockScriptVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for LockScriptVec { + type IntoIter = LockScriptVecIterator; + type Item = Bytes; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + LockScriptVecIterator(self, 0, len) + } +} +impl<'r> LockScriptVecReader<'r> { + pub fn iter<'t>(&'t self) -> LockScriptVecReaderIterator<'t, 'r> { + LockScriptVecReaderIterator(&self, 0, self.len()) + } +} +pub struct LockScriptVecReaderIterator<'t, 'r>(&'t LockScriptVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for LockScriptVecReaderIterator<'t, 'r> { + type Item = BytesReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for LockScriptVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct ExtensionVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ExtensionVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for ExtensionVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for ExtensionVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for ExtensionVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + ExtensionVec::new_unchecked(v.into()) + } +} +impl ExtensionVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> ExtensionVecReader<'r> { + ExtensionVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for ExtensionVec { + type Builder = ExtensionVecBuilder; + + const NAME: &'static str = "ExtensionVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + ExtensionVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct ExtensionVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for ExtensionVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for ExtensionVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for ExtensionVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> ExtensionVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for ExtensionVecReader<'r> { + type Entity = ExtensionVec; + + const NAME: &'static str = "ExtensionVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + ExtensionVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct ExtensionVecBuilder(pub(crate) Vec); +impl ExtensionVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Byte32) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Byte32) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for ExtensionVecBuilder { + type Entity = ExtensionVec; + + const NAME: &'static str = "ExtensionVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + ExtensionVec::new_unchecked(inner.into()) + } +} +pub struct ExtensionVecIterator(ExtensionVec, usize, usize); +impl ::core::iter::Iterator for ExtensionVecIterator { + type Item = Byte32; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for ExtensionVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for ExtensionVec { + type IntoIter = ExtensionVecIterator; + type Item = Byte32; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + ExtensionVecIterator(self, 0, len) + } +} +impl<'r> ExtensionVecReader<'r> { + pub fn iter<'t>(&'t self) -> ExtensionVecReaderIterator<'t, 'r> { + ExtensionVecReaderIterator(&self, 0, self.len()) + } +} +pub struct ExtensionVecReaderIterator<'t, 'r>(&'t ExtensionVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for ExtensionVecReaderIterator<'t, 'r> { + type Item = Byte32Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for ExtensionVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct ExtensionLeaves(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ExtensionLeaves { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for ExtensionLeaves { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for ExtensionLeaves { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "proof", self.proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for ExtensionLeaves { + fn default() -> Self { + let v: Vec = vec![ + 28, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + ExtensionLeaves::new_unchecked(v.into()) + } +} +impl ExtensionLeaves { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> ExtensionVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ExtensionVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn values(&self) -> ExtensionVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ExtensionVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } else { + Bytes::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> ExtensionLeavesReader<'r> { + ExtensionLeavesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for ExtensionLeaves { + type Builder = ExtensionLeavesBuilder; + + const NAME: &'static str = "ExtensionLeaves"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + ExtensionLeaves(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionLeavesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionLeavesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .keys(self.keys()) + .values(self.values()) + .proof(self.proof()) + } +} +#[derive(Clone, Copy)] +pub struct ExtensionLeavesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for ExtensionLeavesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for ExtensionLeavesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for ExtensionLeavesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "proof", self.proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> ExtensionLeavesReader<'r> { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> ExtensionVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn values(&self) -> ExtensionVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } else { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for ExtensionLeavesReader<'r> { + type Entity = ExtensionLeaves; + + const NAME: &'static str = "ExtensionLeavesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + ExtensionLeavesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + ExtensionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + ExtensionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct ExtensionLeavesBuilder { + pub(crate) keys: ExtensionVec, + pub(crate) values: ExtensionVec, + pub(crate) proof: Bytes, +} +impl ExtensionLeavesBuilder { + pub const FIELD_COUNT: usize = 3; + + pub fn keys(mut self, v: ExtensionVec) -> Self { + self.keys = v; + self + } + + pub fn values(mut self, v: ExtensionVec) -> Self { + self.values = v; + self + } + + pub fn proof(mut self, v: Bytes) -> Self { + self.proof = v; + self + } +} +impl molecule::prelude::Builder for ExtensionLeavesBuilder { + type Entity = ExtensionLeaves; + + const NAME: &'static str = "ExtensionLeavesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.keys.as_slice().len() + + self.values.as_slice().len() + + self.proof.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.keys.as_slice().len(); + offsets.push(total_size); + total_size += self.values.as_slice().len(); + offsets.push(total_size); + total_size += self.proof.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.keys.as_slice())?; + writer.write_all(self.values.as_slice())?; + writer.write_all(self.proof.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + ExtensionLeaves::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubKey(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl ::core::default::Default for SubKey { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + SubKey::new_unchecked(v.into()) + } +} +impl SubKey { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) + } + + pub fn sub_type(&self) -> Byte6 { + Byte6::new_unchecked(self.0.slice(2..8)) + } + + pub fn ext_data(&self) -> Uint32 { + Uint32::new_unchecked(self.0.slice(8..12)) + } + + pub fn reserved(&self) -> Byte20 { + Byte20::new_unchecked(self.0.slice(12..32)) + } + + pub fn as_reader<'r>(&'r self) -> SubKeyReader<'r> { + SubKeyReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKey { + type Builder = SubKeyBuilder; + + const NAME: &'static str = "SubKey"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKey(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .smt_type(self.smt_type()) + .sub_type(self.sub_type()) + .ext_data(self.ext_data()) + .reserved(self.reserved()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl<'r> SubKeyReader<'r> { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + } + + pub fn sub_type(&self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(&self.as_slice()[2..8]) + } + + pub fn ext_data(&self) -> Uint32Reader<'r> { + Uint32Reader::new_unchecked(&self.as_slice()[8..12]) + } + + pub fn reserved(&self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(&self.as_slice()[12..32]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyReader<'r> { + type Entity = SubKey; + + const NAME: &'static str = "SubKeyReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyBuilder { + pub(crate) smt_type: Uint16, + pub(crate) sub_type: Byte6, + pub(crate) ext_data: Uint32, + pub(crate) reserved: Byte20, +} +impl SubKeyBuilder { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(mut self, v: Uint16) -> Self { + self.smt_type = v; + self + } + + pub fn sub_type(mut self, v: Byte6) -> Self { + self.sub_type = v; + self + } + + pub fn ext_data(mut self, v: Uint32) -> Self { + self.ext_data = v; + self + } + + pub fn reserved(mut self, v: Byte20) -> Self { + self.reserved = v; + self + } +} +impl molecule::prelude::Builder for SubKeyBuilder { + type Entity = SubKey; + + const NAME: &'static str = "SubKeyBuilder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.smt_type.as_slice())?; + writer.write_all(self.sub_type.as_slice())?; + writer.write_all(self.ext_data.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKey::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubValue(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, ", {}: {}", "padding", self.padding())?; + write!(f, " }}") + } +} +impl ::core::default::Default for SubValue { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + SubValue::new_unchecked(v.into()) + } +} +impl SubValue { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; + + pub fn key_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) + } + + pub fn pubkey_hash(&self) -> Byte20 { + Byte20::new_unchecked(self.0.slice(2..22)) + } + + pub fn reserved(&self) -> Byte9 { + Byte9::new_unchecked(self.0.slice(22..31)) + } + + pub fn padding(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn as_reader<'r>(&'r self) -> SubValueReader<'r> { + SubValueReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubValue { + type Builder = SubValueBuilder; + + const NAME: &'static str = "SubValue"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubValue(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .key_type(self.key_type()) + .pubkey_hash(self.pubkey_hash()) + .reserved(self.reserved()) + .padding(self.padding()) + } +} +#[derive(Clone, Copy)] +pub struct SubValueReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, ", {}: {}", "padding", self.padding())?; + write!(f, " }}") + } +} +impl<'r> SubValueReader<'r> { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; + + pub fn key_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + } + + pub fn pubkey_hash(&self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(&self.as_slice()[2..22]) + } + + pub fn reserved(&self) -> Byte9Reader<'r> { + Byte9Reader::new_unchecked(&self.as_slice()[22..31]) + } + + pub fn padding(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { + type Entity = SubValue; + + const NAME: &'static str = "SubValueReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubValueReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubValueBuilder { + pub(crate) key_type: Uint16, + pub(crate) pubkey_hash: Byte20, + pub(crate) reserved: Byte9, + pub(crate) padding: Byte, +} +impl SubValueBuilder { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; + + pub fn key_type(mut self, v: Uint16) -> Self { + self.key_type = v; + self + } + + pub fn pubkey_hash(mut self, v: Byte20) -> Self { + self.pubkey_hash = v; + self + } + + pub fn reserved(mut self, v: Byte9) -> Self { + self.reserved = v; + self + } + + pub fn padding(mut self, v: Byte) -> Self { + self.padding = v; + self + } +} +impl molecule::prelude::Builder for SubValueBuilder { + type Entity = SubValue; + + const NAME: &'static str = "SubValueBuilder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.key_type.as_slice())?; + writer.write_all(self.pubkey_hash.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + writer.write_all(self.padding.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubValue::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubKeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SubKeyVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SubKeyVec::new_unchecked(v.into()) + } +} +impl SubKeyVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubKey { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubKey::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SubKeyVecReader<'r> { + SubKeyVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyVec { + type Builder = SubKeyVecBuilder; + + const NAME: &'static str = "SubKeyVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SubKeyVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubKeyReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubKeyReader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyVecReader<'r> { + type Entity = SubKeyVec; + + const NAME: &'static str = "SubKeyVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyVecBuilder(pub(crate) Vec); +impl SubKeyVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SubKey) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SubKey) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SubKeyVecBuilder { + type Entity = SubKeyVec; + + const NAME: &'static str = "SubKeyVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyVec::new_unchecked(inner.into()) + } +} +pub struct SubKeyVecIterator(SubKeyVec, usize, usize); +impl ::core::iter::Iterator for SubKeyVecIterator { + type Item = SubKey; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SubKeyVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SubKeyVec { + type IntoIter = SubKeyVecIterator; + type Item = SubKey; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SubKeyVecIterator(self, 0, len) + } +} +impl<'r> SubKeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> SubKeyVecReaderIterator<'t, 'r> { + SubKeyVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SubKeyVecReaderIterator<'t, 'r>(&'t SubKeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SubKeyVecReaderIterator<'t, 'r> { + type Item = SubKeyReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubKeyVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SubValueVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SubValueVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SubValueVec::new_unchecked(v.into()) + } +} +impl SubValueVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubValue { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubValue::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SubValueVecReader<'r> { + SubValueVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubValueVec { + type Builder = SubValueVecBuilder; + + const NAME: &'static str = "SubValueVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubValueVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SubValueVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SubValueVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubValueReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubValueReader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { + type Entity = SubValueVec; + + const NAME: &'static str = "SubValueVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubValueVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubValueVecBuilder(pub(crate) Vec); +impl SubValueVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SubValue) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SubValue) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SubValueVecBuilder { + type Entity = SubValueVec; + + const NAME: &'static str = "SubValueVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubValueVec::new_unchecked(inner.into()) + } +} +pub struct SubValueVecIterator(SubValueVec, usize, usize); +impl ::core::iter::Iterator for SubValueVecIterator { + type Item = SubValue; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SubValueVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SubValueVec { + type IntoIter = SubValueVecIterator; + type Item = SubValue; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SubValueVecIterator(self, 0, len) + } +} +impl<'r> SubValueVecReader<'r> { + pub fn iter<'t>(&'t self) -> SubValueVecReaderIterator<'t, 'r> { + SubValueVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SubValueVecReaderIterator<'t, 'r>(&'t SubValueVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SubValueVecReaderIterator<'t, 'r> { + type Item = SubValueReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubValueVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SubKeyEntry(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SubKeyEntry { + fn default() -> Self { + let v: Vec = vec![ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + SubKeyEntry::new_unchecked(v.into()) + } +} +impl SubKeyEntry { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SubKeyVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SubKeyVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn values(&self) -> SubValueVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SubValueVec::new_unchecked(self.0.slice(start..end)) + } else { + SubValueVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SubKeyEntryReader<'r> { + SubKeyEntryReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyEntry { + type Builder = SubKeyEntryBuilder; + + const NAME: &'static str = "SubKeyEntry"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyEntry(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntryReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().keys(self.keys()).values(self.values()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyEntryReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SubKeyEntryReader<'r> { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SubKeyVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SubKeyVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn values(&self) -> SubValueVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SubValueVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SubValueVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyEntryReader<'r> { + type Entity = SubKeyEntry; + + const NAME: &'static str = "SubKeyEntryReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyEntryReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + SubKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + SubValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyEntryBuilder { + pub(crate) keys: SubKeyVec, + pub(crate) values: SubValueVec, +} +impl SubKeyEntryBuilder { + pub const FIELD_COUNT: usize = 2; + + pub fn keys(mut self, v: SubKeyVec) -> Self { + self.keys = v; + self + } + + pub fn values(mut self, v: SubValueVec) -> Self { + self.values = v; + self + } +} +impl molecule::prelude::Builder for SubKeyEntryBuilder { + type Entity = SubKeyEntry; + + const NAME: &'static str = "SubKeyEntryBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.keys.as_slice().len() + + self.values.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.keys.as_slice().len(); + offsets.push(total_size); + total_size += self.values.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.keys.as_slice())?; + writer.write_all(self.values.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyEntry::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubKeyEntryOpt(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl ::core::default::Default for SubKeyEntryOpt { + fn default() -> Self { + let v: Vec = vec![]; + SubKeyEntryOpt::new_unchecked(v.into()) + } +} +impl SubKeyEntryOpt { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option { + if self.is_none() { + None + } else { + Some(SubKeyEntry::new_unchecked(self.0.clone())) + } + } + + pub fn as_reader<'r>(&'r self) -> SubKeyEntryOptReader<'r> { + SubKeyEntryOptReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyEntryOpt { + type Builder = SubKeyEntryOptBuilder; + + const NAME: &'static str = "SubKeyEntryOpt"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyEntryOpt(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntryOptReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntryOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set(self.to_opt()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyEntryOptReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl<'r> SubKeyEntryOptReader<'r> { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option> { + if self.is_none() { + None + } else { + Some(SubKeyEntryReader::new_unchecked(self.as_slice())) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyEntryOptReader<'r> { + type Entity = SubKeyEntryOpt; + + const NAME: &'static str = "SubKeyEntryOptReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyEntryOptReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + if !slice.is_empty() { + SubKeyEntryReader::verify(&slice[..], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyEntryOptBuilder(pub(crate) Option); +impl SubKeyEntryOptBuilder { + pub fn set(mut self, v: Option) -> Self { + self.0 = v; + self + } +} +impl molecule::prelude::Builder for SubKeyEntryOptBuilder { + type Entity = SubKeyEntryOpt; + + const NAME: &'static str = "SubKeyEntryOptBuilder"; + + fn expected_length(&self) -> usize { + self.0 + .as_ref() + .map(|ref inner| inner.as_slice().len()) + .unwrap_or(0) + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + self.0 + .as_ref() + .map(|ref inner| writer.write_all(inner.as_slice())) + .unwrap_or(Ok(())) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyEntryOpt::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialKey(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl ::core::default::Default for SocialKey { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + SocialKey::new_unchecked(v.into()) + } +} +impl SocialKey { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) + } + + pub fn sub_type(&self) -> Byte6 { + Byte6::new_unchecked(self.0.slice(2..8)) + } + + pub fn reserved(&self) -> Byte24 { + Byte24::new_unchecked(self.0.slice(8..32)) + } + + pub fn as_reader<'r>(&'r self) -> SocialKeyReader<'r> { + SocialKeyReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialKey { + type Builder = SocialKeyBuilder; + + const NAME: &'static str = "SocialKey"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialKey(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .smt_type(self.smt_type()) + .sub_type(self.sub_type()) + .reserved(self.reserved()) + } +} +#[derive(Clone, Copy)] +pub struct SocialKeyReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl<'r> SocialKeyReader<'r> { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + } + + pub fn sub_type(&self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(&self.as_slice()[2..8]) + } + + pub fn reserved(&self) -> Byte24Reader<'r> { + Byte24Reader::new_unchecked(&self.as_slice()[8..32]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialKeyReader<'r> { + type Entity = SocialKey; + + const NAME: &'static str = "SocialKeyReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialKeyReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialKeyBuilder { + pub(crate) smt_type: Uint16, + pub(crate) sub_type: Byte6, + pub(crate) reserved: Byte24, +} +impl SocialKeyBuilder { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(mut self, v: Uint16) -> Self { + self.smt_type = v; + self + } + + pub fn sub_type(mut self, v: Byte6) -> Self { + self.sub_type = v; + self + } + + pub fn reserved(mut self, v: Byte24) -> Self { + self.reserved = v; + self + } +} +impl molecule::prelude::Builder for SocialKeyBuilder { + type Entity = SocialKey; + + const NAME: &'static str = "SocialKeyBuilder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.smt_type.as_slice())?; + writer.write_all(self.sub_type.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialKey::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialValue(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; + write!(f, ", {}: {}", "must", self.must())?; + write!(f, ", {}: {}", "total", self.total())?; + write!(f, ", {}: {}", "signers", self.signers())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SocialValue { + fn default() -> Self { + let v: Vec = vec![ + 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; + SocialValue::new_unchecked(v.into()) + } +} +impl SocialValue { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn recovery_mode(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn must(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn total(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn signers(&self) -> LockScriptVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + LockScriptVec::new_unchecked(self.0.slice(start..end)) + } else { + LockScriptVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialValueReader<'r> { + SocialValueReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialValue { + type Builder = SocialValueBuilder; + + const NAME: &'static str = "SocialValue"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialValue(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .recovery_mode(self.recovery_mode()) + .must(self.must()) + .total(self.total()) + .signers(self.signers()) + } +} +#[derive(Clone, Copy)] +pub struct SocialValueReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; + write!(f, ", {}: {}", "must", self.must())?; + write!(f, ", {}: {}", "total", self.total())?; + write!(f, ", {}: {}", "signers", self.signers())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SocialValueReader<'r> { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn recovery_mode(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn must(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn total(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn signers(&self) -> LockScriptVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + LockScriptVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + LockScriptVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialValueReader<'r> { + type Entity = SocialValue; + + const NAME: &'static str = "SocialValueReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialValueReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + LockScriptVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialValueBuilder { + pub(crate) recovery_mode: Byte, + pub(crate) must: Byte, + pub(crate) total: Byte, + pub(crate) signers: LockScriptVec, +} +impl SocialValueBuilder { + pub const FIELD_COUNT: usize = 4; + + pub fn recovery_mode(mut self, v: Byte) -> Self { + self.recovery_mode = v; + self + } + + pub fn must(mut self, v: Byte) -> Self { + self.must = v; + self + } + + pub fn total(mut self, v: Byte) -> Self { + self.total = v; + self + } + + pub fn signers(mut self, v: LockScriptVec) -> Self { + self.signers = v; + self + } +} +impl molecule::prelude::Builder for SocialValueBuilder { + type Entity = SocialValue; + + const NAME: &'static str = "SocialValueBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.recovery_mode.as_slice().len() + + self.must.as_slice().len() + + self.total.as_slice().len() + + self.signers.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.recovery_mode.as_slice().len(); + offsets.push(total_size); + total_size += self.must.as_slice().len(); + offsets.push(total_size); + total_size += self.total.as_slice().len(); + offsets.push(total_size); + total_size += self.signers.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.recovery_mode.as_slice())?; + writer.write_all(self.must.as_slice())?; + writer.write_all(self.total.as_slice())?; + writer.write_all(self.signers.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialValue::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialKeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SocialKeyVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SocialKeyVec::new_unchecked(v.into()) + } +} +impl SocialKeyVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialKey { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SocialKey::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SocialKeyVecReader<'r> { + SocialKeyVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialKeyVec { + type Builder = SocialKeyVecBuilder; + + const NAME: &'static str = "SocialKeyVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialKeyVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SocialKeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SocialKeyVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialKeyReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SocialKeyReader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialKeyVecReader<'r> { + type Entity = SocialKeyVec; + + const NAME: &'static str = "SocialKeyVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialKeyVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialKeyVecBuilder(pub(crate) Vec); +impl SocialKeyVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SocialKey) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SocialKey) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SocialKeyVecBuilder { + type Entity = SocialKeyVec; + + const NAME: &'static str = "SocialKeyVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialKeyVec::new_unchecked(inner.into()) + } +} +pub struct SocialKeyVecIterator(SocialKeyVec, usize, usize); +impl ::core::iter::Iterator for SocialKeyVecIterator { + type Item = SocialKey; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SocialKeyVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SocialKeyVec { + type IntoIter = SocialKeyVecIterator; + type Item = SocialKey; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SocialKeyVecIterator(self, 0, len) + } +} +impl<'r> SocialKeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> SocialKeyVecReaderIterator<'t, 'r> { + SocialKeyVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SocialKeyVecReaderIterator<'t, 'r>(&'t SocialKeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SocialKeyVecReaderIterator<'t, 'r> { + type Item = SocialKeyReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialKeyVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SocialValueVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SocialValueVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + SocialValueVec::new_unchecked(v.into()) + } +} +impl SocialValueVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialValue { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SocialValue::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SocialValue::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialValueVecReader<'r> { + SocialValueVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialValueVec { + type Builder = SocialValueVecBuilder; + + const NAME: &'static str = "SocialValueVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialValueVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SocialValueVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SocialValueVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialValueReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SocialValueReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SocialValueReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialValueVecReader<'r> { + type Entity = SocialValueVec; + + const NAME: &'static str = "SocialValueVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialValueVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + SocialValueReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialValueVecBuilder(pub(crate) Vec); +impl SocialValueVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SocialValue) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SocialValue) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SocialValueVecBuilder { + type Entity = SocialValueVec; + + const NAME: &'static str = "SocialValueVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialValueVec::new_unchecked(inner.into()) + } +} +pub struct SocialValueVecIterator(SocialValueVec, usize, usize); +impl ::core::iter::Iterator for SocialValueVecIterator { + type Item = SocialValue; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SocialValueVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SocialValueVec { + type IntoIter = SocialValueVecIterator; + type Item = SocialValue; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SocialValueVecIterator(self, 0, len) + } +} +impl<'r> SocialValueVecReader<'r> { + pub fn iter<'t>(&'t self) -> SocialValueVecReaderIterator<'t, 'r> { + SocialValueVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SocialValueVecReaderIterator<'t, 'r>(&'t SocialValueVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SocialValueVecReaderIterator<'t, 'r> { + type Item = SocialValueReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialValueVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SocialEntry(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialEntry { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SocialEntry { + fn default() -> Self { + let v: Vec = vec![ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; + SocialEntry::new_unchecked(v.into()) + } +} +impl SocialEntry { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SocialKeyVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialKeyVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn values(&self) -> SocialValueVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SocialValueVec::new_unchecked(self.0.slice(start..end)) + } else { + SocialValueVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialEntryReader<'r> { + SocialEntryReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialEntry { + type Builder = SocialEntryBuilder; + + const NAME: &'static str = "SocialEntry"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialEntry(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntryReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().keys(self.keys()).values(self.values()) + } +} +#[derive(Clone, Copy)] +pub struct SocialEntryReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialEntryReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SocialEntryReader<'r> { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SocialKeyVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialKeyVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn values(&self) -> SocialValueVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SocialValueVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SocialValueVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialEntryReader<'r> { + type Entity = SocialEntry; + + const NAME: &'static str = "SocialEntryReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialEntryReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + SocialKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + SocialValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialEntryBuilder { + pub(crate) keys: SocialKeyVec, + pub(crate) values: SocialValueVec, +} +impl SocialEntryBuilder { + pub const FIELD_COUNT: usize = 2; + + pub fn keys(mut self, v: SocialKeyVec) -> Self { + self.keys = v; + self + } + + pub fn values(mut self, v: SocialValueVec) -> Self { + self.values = v; + self + } +} +impl molecule::prelude::Builder for SocialEntryBuilder { + type Entity = SocialEntry; + + const NAME: &'static str = "SocialEntryBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.keys.as_slice().len() + + self.values.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.keys.as_slice().len(); + offsets.push(total_size); + total_size += self.values.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.keys.as_slice())?; + writer.write_all(self.values.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialEntry::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialEntryOpt(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialEntryOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl ::core::default::Default for SocialEntryOpt { + fn default() -> Self { + let v: Vec = vec![]; + SocialEntryOpt::new_unchecked(v.into()) + } +} +impl SocialEntryOpt { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option { + if self.is_none() { + None + } else { + Some(SocialEntry::new_unchecked(self.0.clone())) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialEntryOptReader<'r> { + SocialEntryOptReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialEntryOpt { + type Builder = SocialEntryOptBuilder; + + const NAME: &'static str = "SocialEntryOpt"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialEntryOpt(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntryOptReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntryOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set(self.to_opt()) + } +} +#[derive(Clone, Copy)] +pub struct SocialEntryOptReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialEntryOptReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl<'r> SocialEntryOptReader<'r> { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option> { + if self.is_none() { + None + } else { + Some(SocialEntryReader::new_unchecked(self.as_slice())) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialEntryOptReader<'r> { + type Entity = SocialEntryOpt; + + const NAME: &'static str = "SocialEntryOptReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialEntryOptReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + if !slice.is_empty() { + SocialEntryReader::verify(&slice[..], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialEntryOptBuilder(pub(crate) Option); +impl SocialEntryOptBuilder { + pub fn set(mut self, v: Option) -> Self { + self.0 = v; + self + } +} +impl molecule::prelude::Builder for SocialEntryOptBuilder { + type Entity = SocialEntryOpt; + + const NAME: &'static str = "SocialEntryOptBuilder"; + + fn expected_length(&self) -> usize { + self.0 + .as_ref() + .map(|ref inner| inner.as_slice().len()) + .unwrap_or(0) + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + self.0 + .as_ref() + .map(|ref inner| writer.write_all(inner.as_slice())) + .unwrap_or(Ok(())) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialEntryOpt::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct ExtensionEntries(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ExtensionEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for ExtensionEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for ExtensionEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "leaves", self.leaves())?; + write!(f, ", {}: {}", "subkey", self.subkey())?; + write!(f, ", {}: {}", "social", self.social())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for ExtensionEntries { + fn default() -> Self { + let v: Vec = vec![ + 44, 0, 0, 0, 16, 0, 0, 0, 44, 0, 0, 0, 44, 0, 0, 0, 28, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, + 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + ExtensionEntries::new_unchecked(v.into()) + } +} +impl ExtensionEntries { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn leaves(&self) -> ExtensionLeaves { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ExtensionLeaves::new_unchecked(self.0.slice(start..end)) + } + + pub fn subkey(&self) -> SubKeyEntryOpt { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + SubKeyEntryOpt::new_unchecked(self.0.slice(start..end)) + } + + pub fn social(&self) -> SocialEntryOpt { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + SocialEntryOpt::new_unchecked(self.0.slice(start..end)) + } else { + SocialEntryOpt::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> ExtensionEntriesReader<'r> { + ExtensionEntriesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for ExtensionEntries { + type Builder = ExtensionEntriesBuilder; + + const NAME: &'static str = "ExtensionEntries"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + ExtensionEntries(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + ExtensionEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .leaves(self.leaves()) + .subkey(self.subkey()) + .social(self.social()) + } +} +#[derive(Clone, Copy)] +pub struct ExtensionEntriesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for ExtensionEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for ExtensionEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for ExtensionEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "leaves", self.leaves())?; + write!(f, ", {}: {}", "subkey", self.subkey())?; + write!(f, ", {}: {}", "social", self.social())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> ExtensionEntriesReader<'r> { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn leaves(&self) -> ExtensionLeavesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ExtensionLeavesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn subkey(&self) -> SubKeyEntryOptReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + SubKeyEntryOptReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn social(&self) -> SocialEntryOptReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + SocialEntryOptReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SocialEntryOptReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for ExtensionEntriesReader<'r> { + type Entity = ExtensionEntries; + + const NAME: &'static str = "ExtensionEntriesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + ExtensionEntriesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + ExtensionLeavesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + SubKeyEntryOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + SocialEntryOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct ExtensionEntriesBuilder { + pub(crate) leaves: ExtensionLeaves, + pub(crate) subkey: SubKeyEntryOpt, + pub(crate) social: SocialEntryOpt, +} +impl ExtensionEntriesBuilder { + pub const FIELD_COUNT: usize = 3; + + pub fn leaves(mut self, v: ExtensionLeaves) -> Self { + self.leaves = v; + self + } + + pub fn subkey(mut self, v: SubKeyEntryOpt) -> Self { + self.subkey = v; + self + } + + pub fn social(mut self, v: SocialEntryOpt) -> Self { + self.social = v; + self + } +} +impl molecule::prelude::Builder for ExtensionEntriesBuilder { + type Entity = ExtensionEntries; + + const NAME: &'static str = "ExtensionEntriesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.leaves.as_slice().len() + + self.subkey.as_slice().len() + + self.social.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.leaves.as_slice().len(); + offsets.push(total_size); + total_size += self.subkey.as_slice().len(); + offsets.push(total_size); + total_size += self.social.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.leaves.as_slice())?; + writer.write_all(self.subkey.as_slice())?; + writer.write_all(self.social.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + ExtensionEntries::new_unchecked(inner.into()) + } +} From e1a0d35ee71d76db9a07e198482c4333e8e972cd Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 22 Sep 2022 16:47:32 +0800 Subject: [PATCH 02/19] chore: Bump to v0.6.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 494a3c2..998ac39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,7 +225,7 @@ dependencies = [ [[package]] name = "cota-smt" -version = "0.5.0" +version = "0.6.0" dependencies = [ "blake2b-ref 0.3.0", "cfg-if 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index 1eabb44..3148e46 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cota-smt" -version = "0.5.0" +version = "0.6.0" edition = "2018" [dependencies] From 4f2e2ee8a2e67768bb0fbd33bda8a48784f36991 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 22 Sep 2022 17:03:34 +0800 Subject: [PATCH 03/19] refactor: Add old_values to extension --- src/extension.mol | 1 + src/extension.rs | 57 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/extension.mol b/src/extension.mol index cbda691..4bfc3a5 100644 --- a/src/extension.mol +++ b/src/extension.mol @@ -11,6 +11,7 @@ vector ExtensionVec ; table ExtensionLeaves { keys: ExtensionVec, values: ExtensionVec, + old_values: ExtensionVec, proof: Bytes, } diff --git a/src/extension.rs b/src/extension.rs index eb3eb05..ae744a2 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -2364,6 +2364,7 @@ impl ::core::fmt::Display for ExtensionLeaves { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "keys", self.keys())?; write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "old_values", self.old_values())?; write!(f, ", {}: {}", "proof", self.proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { @@ -2375,13 +2376,14 @@ impl ::core::fmt::Display for ExtensionLeaves { impl ::core::default::Default for ExtensionLeaves { fn default() -> Self { let v: Vec = vec![ - 28, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; ExtensionLeaves::new_unchecked(v.into()) } } impl ExtensionLeaves { - pub const FIELD_COUNT: usize = 3; + pub const FIELD_COUNT: usize = 4; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2417,11 +2419,18 @@ impl ExtensionLeaves { ExtensionVec::new_unchecked(self.0.slice(start..end)) } - pub fn proof(&self) -> Bytes { + pub fn old_values(&self) -> ExtensionVec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ExtensionVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; + let end = molecule::unpack_number(&slice[20..]) as usize; Bytes::new_unchecked(self.0.slice(start..end)) } else { Bytes::new_unchecked(self.0.slice(start..)) @@ -2465,6 +2474,7 @@ impl molecule::prelude::Entity for ExtensionLeaves { Self::new_builder() .keys(self.keys()) .values(self.values()) + .old_values(self.old_values()) .proof(self.proof()) } } @@ -2489,6 +2499,7 @@ impl<'r> ::core::fmt::Display for ExtensionLeavesReader<'r> { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "keys", self.keys())?; write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "old_values", self.old_values())?; write!(f, ", {}: {}", "proof", self.proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { @@ -2498,7 +2509,7 @@ impl<'r> ::core::fmt::Display for ExtensionLeavesReader<'r> { } } impl<'r> ExtensionLeavesReader<'r> { - pub const FIELD_COUNT: usize = 3; + pub const FIELD_COUNT: usize = 4; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -2534,11 +2545,18 @@ impl<'r> ExtensionLeavesReader<'r> { ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn proof(&self) -> BytesReader<'r> { + pub fn old_values(&self) -> ExtensionVecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; + let end = molecule::unpack_number(&slice[20..]) as usize; BytesReader::new_unchecked(&self.as_slice()[start..end]) } else { BytesReader::new_unchecked(&self.as_slice()[start..]) @@ -2601,18 +2619,20 @@ impl<'r> molecule::prelude::Reader<'r> for ExtensionLeavesReader<'r> { } ExtensionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; ExtensionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + ExtensionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; Ok(()) } } #[derive(Debug, Default)] pub struct ExtensionLeavesBuilder { - pub(crate) keys: ExtensionVec, - pub(crate) values: ExtensionVec, - pub(crate) proof: Bytes, + pub(crate) keys: ExtensionVec, + pub(crate) values: ExtensionVec, + pub(crate) old_values: ExtensionVec, + pub(crate) proof: Bytes, } impl ExtensionLeavesBuilder { - pub const FIELD_COUNT: usize = 3; + pub const FIELD_COUNT: usize = 4; pub fn keys(mut self, v: ExtensionVec) -> Self { self.keys = v; @@ -2624,6 +2644,11 @@ impl ExtensionLeavesBuilder { self } + pub fn old_values(mut self, v: ExtensionVec) -> Self { + self.old_values = v; + self + } + pub fn proof(mut self, v: Bytes) -> Self { self.proof = v; self @@ -2638,6 +2663,7 @@ impl molecule::prelude::Builder for ExtensionLeavesBuilder { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.keys.as_slice().len() + self.values.as_slice().len() + + self.old_values.as_slice().len() + self.proof.as_slice().len() } @@ -2649,6 +2675,8 @@ impl molecule::prelude::Builder for ExtensionLeavesBuilder { offsets.push(total_size); total_size += self.values.as_slice().len(); offsets.push(total_size); + total_size += self.old_values.as_slice().len(); + offsets.push(total_size); total_size += self.proof.as_slice().len(); writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; for offset in offsets.into_iter() { @@ -2656,6 +2684,7 @@ impl molecule::prelude::Builder for ExtensionLeavesBuilder { } writer.write_all(self.keys.as_slice())?; writer.write_all(self.values.as_slice())?; + writer.write_all(self.old_values.as_slice())?; writer.write_all(self.proof.as_slice())?; Ok(()) } @@ -5966,8 +5995,8 @@ impl ::core::fmt::Display for ExtensionEntries { impl ::core::default::Default for ExtensionEntries { fn default() -> Self { let v: Vec = vec![ - 44, 0, 0, 0, 16, 0, 0, 0, 44, 0, 0, 0, 44, 0, 0, 0, 28, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, - 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 52, 0, 0, 0, 16, 0, 0, 0, 52, 0, 0, 0, 52, 0, 0, 0, 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, + 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; ExtensionEntries::new_unchecked(v.into()) } From 3a021c1eee461e6313638e2ca0f891d3506d85d5 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 22 Sep 2022 17:15:40 +0800 Subject: [PATCH 04/19] chore: Add extension module to lib --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 43411ee..70b7206 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod common; pub mod define; +pub mod extension; pub mod mint; pub mod registry; pub mod smt; From ae9af73c90f672a65878781b419fb65a75be6b55 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 20 Oct 2022 14:28:41 +0800 Subject: [PATCH 05/19] refactor: Update extension mol --- src/extension.mol | 50 +- src/extension.rs | 5398 ++++----------------------------------------- 2 files changed, 379 insertions(+), 5069 deletions(-) diff --git a/src/extension.mol b/src/extension.mol index 4bfc3a5..0629c26 100644 --- a/src/extension.mol +++ b/src/extension.mol @@ -1,9 +1,6 @@ import common; array Byte6 [byte; 6]; -array Byte9 [byte; 9]; -array Byte20 [byte; 20]; -array Byte24 [byte; 24]; vector LockScriptVec ; vector ExtensionVec ; @@ -15,51 +12,8 @@ table ExtensionLeaves { proof: Bytes, } -// SubKeys -struct SubKey { - smt_type: Uint16, - sub_type: Byte6, - ext_data: Uint32, - reserved: Byte20, -} -struct SubValue { - key_type: Uint16, - pubkey_hash: Byte20, - reserved: Byte9, - padding: byte, -} -vector SubKeyVec ; -vector SubValueVec ; -table SubKeyEntry { - keys: SubKeyVec, - values: SubValueVec, -} -option SubKeyEntryOpt (SubKeyEntry); - - -// Social Recovery -struct SocialKey { - smt_type: Uint16, - sub_type: Byte6, - reserved: Byte24, -} -table SocialValue { - recovery_mode: byte, - must: byte, - total: byte, - signers: LockScriptVec, -} -vector SocialKeyVec ; -vector SocialValueVec ; -table SocialEntry { - keys: SocialKeyVec, - values: SocialValueVec, -} -option SocialEntryOpt (SocialEntry); - - table ExtensionEntries { leaves: ExtensionLeaves, - subkey: SubKeyEntryOpt, - social: SocialEntryOpt, + sub_type: Byte6, + raw_data: Bytes, } \ No newline at end of file diff --git a/src/extension.rs b/src/extension.rs index ae744a2..1f8177d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -285,4527 +285,8 @@ impl molecule::prelude::Builder for Byte6Builder { } } #[derive(Clone)] -pub struct Byte9(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte9 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte9::new_unchecked(v.into()) - } -} -impl Byte9 { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { - Byte9Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte9 { - type Builder = Byte9Builder; - - const NAME: &'static str = "Byte9"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte9(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte9Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte9Reader<'r> { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte9Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte9Builder(pub(crate) [Byte; 9]); -impl ::core::fmt::Debug for Byte9Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte9Builder { - fn default() -> Self { - Byte9Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte9Builder { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn set(mut self, v: [Byte; 9]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } -} -impl molecule::prelude::Builder for Byte9Builder { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte9::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte20(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte20 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte20::new_unchecked(v.into()) - } -} -impl Byte20 { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte20 { - type Builder = Byte20Builder; - - const NAME: &'static str = "Byte20"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte20(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte20Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte20Reader<'r> { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte20Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte20Builder(pub(crate) [Byte; 20]); -impl ::core::fmt::Debug for Byte20Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte20Builder { - fn default() -> Self { - Byte20Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte20Builder { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn set(mut self, v: [Byte; 20]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } -} -impl molecule::prelude::Builder for Byte20Builder { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte20::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte24(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte24 { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Byte24::new_unchecked(v.into()) - } -} -impl Byte24 { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { - Byte24Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte24 { - type Builder = Byte24Builder; - - const NAME: &'static str = "Byte24"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte24(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte24Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte24Reader<'r> { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte24Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte24Builder(pub(crate) [Byte; 24]); -impl ::core::fmt::Debug for Byte24Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte24Builder { - fn default() -> Self { - Byte24Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte24Builder { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn set(mut self, v: [Byte; 24]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } - - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self - } - - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self - } - - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self - } -} -impl molecule::prelude::Builder for Byte24Builder { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte24::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct LockScriptVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for LockScriptVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - LockScriptVec::new_unchecked(v.into()) - } -} -impl LockScriptVec { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> Bytes { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - Bytes::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } - } - - pub fn as_reader<'r>(&'r self) -> LockScriptVecReader<'r> { - LockScriptVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for LockScriptVec { - type Builder = LockScriptVecBuilder; - - const NAME: &'static str = "LockScriptVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - LockScriptVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - LockScriptVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - LockScriptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct LockScriptVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> LockScriptVecReader<'r> { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for LockScriptVecReader<'r> { - type Entity = LockScriptVec; - - const NAME: &'static str = "LockScriptVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - LockScriptVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - BytesReader::verify(&slice[start..end], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct LockScriptVecBuilder(pub(crate) Vec); -impl LockScriptVecBuilder { - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: Bytes) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: Bytes) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for LockScriptVecBuilder { - type Entity = LockScriptVec; - - const NAME: &'static str = "LockScriptVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - LockScriptVec::new_unchecked(inner.into()) - } -} -pub struct LockScriptVecIterator(LockScriptVec, usize, usize); -impl ::core::iter::Iterator for LockScriptVecIterator { - type Item = Bytes; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for LockScriptVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for LockScriptVec { - type IntoIter = LockScriptVecIterator; - type Item = Bytes; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - LockScriptVecIterator(self, 0, len) - } -} -impl<'r> LockScriptVecReader<'r> { - pub fn iter<'t>(&'t self) -> LockScriptVecReaderIterator<'t, 'r> { - LockScriptVecReaderIterator(&self, 0, self.len()) - } -} -pub struct LockScriptVecReaderIterator<'t, 'r>(&'t LockScriptVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for LockScriptVecReaderIterator<'t, 'r> { - type Item = BytesReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for LockScriptVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct ExtensionVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for ExtensionVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for ExtensionVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for ExtensionVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for ExtensionVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - ExtensionVec::new_unchecked(v.into()) - } -} -impl ExtensionVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> Byte32 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte32::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> ExtensionVecReader<'r> { - ExtensionVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for ExtensionVec { - type Builder = ExtensionVecBuilder; - - const NAME: &'static str = "ExtensionVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - ExtensionVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - ExtensionVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - ExtensionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct ExtensionVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for ExtensionVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for ExtensionVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for ExtensionVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> ExtensionVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte32Reader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for ExtensionVecReader<'r> { - type Entity = ExtensionVec; - - const NAME: &'static str = "ExtensionVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - ExtensionVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct ExtensionVecBuilder(pub(crate) Vec); -impl ExtensionVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: Byte32) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: Byte32) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for ExtensionVecBuilder { - type Entity = ExtensionVec; - - const NAME: &'static str = "ExtensionVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - ExtensionVec::new_unchecked(inner.into()) - } -} -pub struct ExtensionVecIterator(ExtensionVec, usize, usize); -impl ::core::iter::Iterator for ExtensionVecIterator { - type Item = Byte32; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for ExtensionVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for ExtensionVec { - type IntoIter = ExtensionVecIterator; - type Item = Byte32; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - ExtensionVecIterator(self, 0, len) - } -} -impl<'r> ExtensionVecReader<'r> { - pub fn iter<'t>(&'t self) -> ExtensionVecReaderIterator<'t, 'r> { - ExtensionVecReaderIterator(&self, 0, self.len()) - } -} -pub struct ExtensionVecReaderIterator<'t, 'r>(&'t ExtensionVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for ExtensionVecReaderIterator<'t, 'r> { - type Item = Byte32Reader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for ExtensionVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct ExtensionLeaves(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for ExtensionLeaves { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for ExtensionLeaves { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for ExtensionLeaves { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - write!(f, ", {}: {}", "old_values", self.old_values())?; - write!(f, ", {}: {}", "proof", self.proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for ExtensionLeaves { - fn default() -> Self { - let v: Vec = vec![ - 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - ExtensionLeaves::new_unchecked(v.into()) - } -} -impl ExtensionLeaves { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> ExtensionVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - ExtensionVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn values(&self) -> ExtensionVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - ExtensionVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn old_values(&self) -> ExtensionVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - ExtensionVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn proof(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } else { - Bytes::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> ExtensionLeavesReader<'r> { - ExtensionLeavesReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for ExtensionLeaves { - type Builder = ExtensionLeavesBuilder; - - const NAME: &'static str = "ExtensionLeaves"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - ExtensionLeaves(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - ExtensionLeavesReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - ExtensionLeavesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .keys(self.keys()) - .values(self.values()) - .old_values(self.old_values()) - .proof(self.proof()) - } -} -#[derive(Clone, Copy)] -pub struct ExtensionLeavesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for ExtensionLeavesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for ExtensionLeavesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for ExtensionLeavesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - write!(f, ", {}: {}", "old_values", self.old_values())?; - write!(f, ", {}: {}", "proof", self.proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> ExtensionLeavesReader<'r> { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> ExtensionVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn values(&self) -> ExtensionVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn old_values(&self) -> ExtensionVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn proof(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } else { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for ExtensionLeavesReader<'r> { - type Entity = ExtensionLeaves; - - const NAME: &'static str = "ExtensionLeavesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - ExtensionLeavesReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - ExtensionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - ExtensionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - ExtensionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct ExtensionLeavesBuilder { - pub(crate) keys: ExtensionVec, - pub(crate) values: ExtensionVec, - pub(crate) old_values: ExtensionVec, - pub(crate) proof: Bytes, -} -impl ExtensionLeavesBuilder { - pub const FIELD_COUNT: usize = 4; - - pub fn keys(mut self, v: ExtensionVec) -> Self { - self.keys = v; - self - } - - pub fn values(mut self, v: ExtensionVec) -> Self { - self.values = v; - self - } - - pub fn old_values(mut self, v: ExtensionVec) -> Self { - self.old_values = v; - self - } - - pub fn proof(mut self, v: Bytes) -> Self { - self.proof = v; - self - } -} -impl molecule::prelude::Builder for ExtensionLeavesBuilder { - type Entity = ExtensionLeaves; - - const NAME: &'static str = "ExtensionLeavesBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.keys.as_slice().len() - + self.values.as_slice().len() - + self.old_values.as_slice().len() - + self.proof.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.keys.as_slice().len(); - offsets.push(total_size); - total_size += self.values.as_slice().len(); - offsets.push(total_size); - total_size += self.old_values.as_slice().len(); - offsets.push(total_size); - total_size += self.proof.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.keys.as_slice())?; - writer.write_all(self.values.as_slice())?; - writer.write_all(self.old_values.as_slice())?; - writer.write_all(self.proof.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - ExtensionLeaves::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubKey(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SubKey { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SubKey::new_unchecked(v.into()) - } -} -impl SubKey { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn sub_type(&self) -> Byte6 { - Byte6::new_unchecked(self.0.slice(2..8)) - } - - pub fn ext_data(&self) -> Uint32 { - Uint32::new_unchecked(self.0.slice(8..12)) - } - - pub fn reserved(&self) -> Byte20 { - Byte20::new_unchecked(self.0.slice(12..32)) - } - - pub fn as_reader<'r>(&'r self) -> SubKeyReader<'r> { - SubKeyReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKey { - type Builder = SubKeyBuilder; - - const NAME: &'static str = "SubKey"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKey(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .smt_type(self.smt_type()) - .sub_type(self.sub_type()) - .ext_data(self.ext_data()) - .reserved(self.reserved()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl<'r> SubKeyReader<'r> { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn sub_type(&self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(&self.as_slice()[2..8]) - } - - pub fn ext_data(&self) -> Uint32Reader<'r> { - Uint32Reader::new_unchecked(&self.as_slice()[8..12]) - } - - pub fn reserved(&self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(&self.as_slice()[12..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyReader<'r> { - type Entity = SubKey; - - const NAME: &'static str = "SubKeyReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyBuilder { - pub(crate) smt_type: Uint16, - pub(crate) sub_type: Byte6, - pub(crate) ext_data: Uint32, - pub(crate) reserved: Byte20, -} -impl SubKeyBuilder { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(mut self, v: Uint16) -> Self { - self.smt_type = v; - self - } - - pub fn sub_type(mut self, v: Byte6) -> Self { - self.sub_type = v; - self - } - - pub fn ext_data(mut self, v: Uint32) -> Self { - self.ext_data = v; - self - } - - pub fn reserved(mut self, v: Byte20) -> Self { - self.reserved = v; - self - } -} -impl molecule::prelude::Builder for SubKeyBuilder { - type Entity = SubKey; - - const NAME: &'static str = "SubKeyBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.smt_type.as_slice())?; - writer.write_all(self.sub_type.as_slice())?; - writer.write_all(self.ext_data.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKey::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubValue(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "key_type", self.key_type())?; - write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, ", {}: {}", "padding", self.padding())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SubValue { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SubValue::new_unchecked(v.into()) - } -} -impl SubValue { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn key_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn pubkey_hash(&self) -> Byte20 { - Byte20::new_unchecked(self.0.slice(2..22)) - } - - pub fn reserved(&self) -> Byte9 { - Byte9::new_unchecked(self.0.slice(22..31)) - } - - pub fn padding(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) - } - - pub fn as_reader<'r>(&'r self) -> SubValueReader<'r> { - SubValueReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubValue { - type Builder = SubValueBuilder; - - const NAME: &'static str = "SubValue"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubValue(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .key_type(self.key_type()) - .pubkey_hash(self.pubkey_hash()) - .reserved(self.reserved()) - .padding(self.padding()) - } -} -#[derive(Clone, Copy)] -pub struct SubValueReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "key_type", self.key_type())?; - write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, ", {}: {}", "padding", self.padding())?; - write!(f, " }}") - } -} -impl<'r> SubValueReader<'r> { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn key_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn pubkey_hash(&self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(&self.as_slice()[2..22]) - } - - pub fn reserved(&self) -> Byte9Reader<'r> { - Byte9Reader::new_unchecked(&self.as_slice()[22..31]) - } - - pub fn padding(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[31..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { - type Entity = SubValue; - - const NAME: &'static str = "SubValueReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubValueReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubValueBuilder { - pub(crate) key_type: Uint16, - pub(crate) pubkey_hash: Byte20, - pub(crate) reserved: Byte9, - pub(crate) padding: Byte, -} -impl SubValueBuilder { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn key_type(mut self, v: Uint16) -> Self { - self.key_type = v; - self - } - - pub fn pubkey_hash(mut self, v: Byte20) -> Self { - self.pubkey_hash = v; - self - } - - pub fn reserved(mut self, v: Byte9) -> Self { - self.reserved = v; - self - } - - pub fn padding(mut self, v: Byte) -> Self { - self.padding = v; - self - } -} -impl molecule::prelude::Builder for SubValueBuilder { - type Entity = SubValue; - - const NAME: &'static str = "SubValueBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.key_type.as_slice())?; - writer.write_all(self.pubkey_hash.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - writer.write_all(self.padding.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubValue::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubKeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SubKeyVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SubKeyVec::new_unchecked(v.into()) - } -} -impl SubKeyVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubKey { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubKey::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> SubKeyVecReader<'r> { - SubKeyVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyVec { - type Builder = SubKeyVecBuilder; - - const NAME: &'static str = "SubKeyVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SubKeyVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubKeyReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubKeyReader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyVecReader<'r> { - type Entity = SubKeyVec; - - const NAME: &'static str = "SubKeyVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyVecBuilder(pub(crate) Vec); -impl SubKeyVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SubKey) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SubKey) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SubKeyVecBuilder { - type Entity = SubKeyVec; - - const NAME: &'static str = "SubKeyVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyVec::new_unchecked(inner.into()) - } -} -pub struct SubKeyVecIterator(SubKeyVec, usize, usize); -impl ::core::iter::Iterator for SubKeyVecIterator { - type Item = SubKey; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SubKeyVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SubKeyVec { - type IntoIter = SubKeyVecIterator; - type Item = SubKey; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SubKeyVecIterator(self, 0, len) - } -} -impl<'r> SubKeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> SubKeyVecReaderIterator<'t, 'r> { - SubKeyVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SubKeyVecReaderIterator<'t, 'r>(&'t SubKeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SubKeyVecReaderIterator<'t, 'r> { - type Item = SubKeyReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubKeyVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SubValueVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SubValueVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SubValueVec::new_unchecked(v.into()) - } -} -impl SubValueVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubValue { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubValue::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> SubValueVecReader<'r> { - SubValueVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubValueVec { - type Builder = SubValueVecBuilder; - - const NAME: &'static str = "SubValueVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubValueVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SubValueVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SubValueVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubValueReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubValueReader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { - type Entity = SubValueVec; - - const NAME: &'static str = "SubValueVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubValueVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubValueVecBuilder(pub(crate) Vec); -impl SubValueVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SubValue) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SubValue) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SubValueVecBuilder { - type Entity = SubValueVec; - - const NAME: &'static str = "SubValueVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubValueVec::new_unchecked(inner.into()) - } -} -pub struct SubValueVecIterator(SubValueVec, usize, usize); -impl ::core::iter::Iterator for SubValueVecIterator { - type Item = SubValue; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SubValueVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SubValueVec { - type IntoIter = SubValueVecIterator; - type Item = SubValue; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SubValueVecIterator(self, 0, len) - } -} -impl<'r> SubValueVecReader<'r> { - pub fn iter<'t>(&'t self) -> SubValueVecReaderIterator<'t, 'r> { - SubValueVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SubValueVecReaderIterator<'t, 'r>(&'t SubValueVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SubValueVecReaderIterator<'t, 'r> { - type Item = SubValueReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubValueVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SubKeyEntry(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyEntry { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyEntry { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyEntry { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SubKeyEntry { - fn default() -> Self { - let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - SubKeyEntry::new_unchecked(v.into()) - } -} -impl SubKeyEntry { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SubKeyVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SubKeyVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn values(&self) -> SubValueVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SubValueVec::new_unchecked(self.0.slice(start..end)) - } else { - SubValueVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SubKeyEntryReader<'r> { - SubKeyEntryReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyEntry { - type Builder = SubKeyEntryBuilder; - - const NAME: &'static str = "SubKeyEntry"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyEntry(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntryReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().keys(self.keys()).values(self.values()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyEntryReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyEntryReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyEntryReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyEntryReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SubKeyEntryReader<'r> { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SubKeyVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SubKeyVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn values(&self) -> SubValueVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SubValueVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - SubValueVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyEntryReader<'r> { - type Entity = SubKeyEntry; - - const NAME: &'static str = "SubKeyEntryReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyEntryReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - SubKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SubValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyEntryBuilder { - pub(crate) keys: SubKeyVec, - pub(crate) values: SubValueVec, -} -impl SubKeyEntryBuilder { - pub const FIELD_COUNT: usize = 2; - - pub fn keys(mut self, v: SubKeyVec) -> Self { - self.keys = v; - self - } - - pub fn values(mut self, v: SubValueVec) -> Self { - self.values = v; - self - } -} -impl molecule::prelude::Builder for SubKeyEntryBuilder { - type Entity = SubKeyEntry; - - const NAME: &'static str = "SubKeyEntryBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.keys.as_slice().len() - + self.values.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.keys.as_slice().len(); - offsets.push(total_size); - total_size += self.values.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.keys.as_slice())?; - writer.write_all(self.values.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyEntry::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubKeyEntryOpt(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl ::core::default::Default for SubKeyEntryOpt { - fn default() -> Self { - let v: Vec = vec![]; - SubKeyEntryOpt::new_unchecked(v.into()) - } -} -impl SubKeyEntryOpt { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - - pub fn to_opt(&self) -> Option { - if self.is_none() { - None - } else { - Some(SubKeyEntry::new_unchecked(self.0.clone())) - } - } - - pub fn as_reader<'r>(&'r self) -> SubKeyEntryOptReader<'r> { - SubKeyEntryOptReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyEntryOpt { - type Builder = SubKeyEntryOptBuilder; - - const NAME: &'static str = "SubKeyEntryOpt"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyEntryOpt(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntryOptReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntryOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set(self.to_opt()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyEntryOptReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl<'r> SubKeyEntryOptReader<'r> { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - - pub fn to_opt(&self) -> Option> { - if self.is_none() { - None - } else { - Some(SubKeyEntryReader::new_unchecked(self.as_slice())) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyEntryOptReader<'r> { - type Entity = SubKeyEntryOpt; - - const NAME: &'static str = "SubKeyEntryOptReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyEntryOptReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - if !slice.is_empty() { - SubKeyEntryReader::verify(&slice[..], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyEntryOptBuilder(pub(crate) Option); -impl SubKeyEntryOptBuilder { - pub fn set(mut self, v: Option) -> Self { - self.0 = v; - self - } -} -impl molecule::prelude::Builder for SubKeyEntryOptBuilder { - type Entity = SubKeyEntryOpt; - - const NAME: &'static str = "SubKeyEntryOptBuilder"; - - fn expected_length(&self) -> usize { - self.0 - .as_ref() - .map(|ref inner| inner.as_slice().len()) - .unwrap_or(0) - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - self.0 - .as_ref() - .map(|ref inner| writer.write_all(inner.as_slice())) - .unwrap_or(Ok(())) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyEntryOpt::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialKey(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SocialKey { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SocialKey::new_unchecked(v.into()) - } -} -impl SocialKey { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn sub_type(&self) -> Byte6 { - Byte6::new_unchecked(self.0.slice(2..8)) - } - - pub fn reserved(&self) -> Byte24 { - Byte24::new_unchecked(self.0.slice(8..32)) - } - - pub fn as_reader<'r>(&'r self) -> SocialKeyReader<'r> { - SocialKeyReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialKey { - type Builder = SocialKeyBuilder; - - const NAME: &'static str = "SocialKey"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialKey(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .smt_type(self.smt_type()) - .sub_type(self.sub_type()) - .reserved(self.reserved()) - } -} -#[derive(Clone, Copy)] -pub struct SocialKeyReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl<'r> SocialKeyReader<'r> { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn sub_type(&self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(&self.as_slice()[2..8]) - } - - pub fn reserved(&self) -> Byte24Reader<'r> { - Byte24Reader::new_unchecked(&self.as_slice()[8..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialKeyReader<'r> { - type Entity = SocialKey; - - const NAME: &'static str = "SocialKeyReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialKeyReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialKeyBuilder { - pub(crate) smt_type: Uint16, - pub(crate) sub_type: Byte6, - pub(crate) reserved: Byte24, -} -impl SocialKeyBuilder { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(mut self, v: Uint16) -> Self { - self.smt_type = v; - self - } - - pub fn sub_type(mut self, v: Byte6) -> Self { - self.sub_type = v; - self - } - - pub fn reserved(mut self, v: Byte24) -> Self { - self.reserved = v; - self - } -} -impl molecule::prelude::Builder for SocialKeyBuilder { - type Entity = SocialKey; - - const NAME: &'static str = "SocialKeyBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.smt_type.as_slice())?; - writer.write_all(self.sub_type.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialKey::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialValue(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; - write!(f, ", {}: {}", "must", self.must())?; - write!(f, ", {}: {}", "total", self.total())?; - write!(f, ", {}: {}", "signers", self.signers())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SocialValue { - fn default() -> Self { - let v: Vec = vec![ - 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - SocialValue::new_unchecked(v.into()) - } -} -impl SocialValue { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn recovery_mode(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn must(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn total(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn signers(&self) -> LockScriptVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - LockScriptVec::new_unchecked(self.0.slice(start..end)) - } else { - LockScriptVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialValueReader<'r> { - SocialValueReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialValue { - type Builder = SocialValueBuilder; - - const NAME: &'static str = "SocialValue"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialValue(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .recovery_mode(self.recovery_mode()) - .must(self.must()) - .total(self.total()) - .signers(self.signers()) - } -} -#[derive(Clone, Copy)] -pub struct SocialValueReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; - write!(f, ", {}: {}", "must", self.must())?; - write!(f, ", {}: {}", "total", self.total())?; - write!(f, ", {}: {}", "signers", self.signers())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SocialValueReader<'r> { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn recovery_mode(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn must(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn total(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn signers(&self) -> LockScriptVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - LockScriptVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - LockScriptVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialValueReader<'r> { - type Entity = SocialValue; - - const NAME: &'static str = "SocialValueReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialValueReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - LockScriptVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialValueBuilder { - pub(crate) recovery_mode: Byte, - pub(crate) must: Byte, - pub(crate) total: Byte, - pub(crate) signers: LockScriptVec, -} -impl SocialValueBuilder { - pub const FIELD_COUNT: usize = 4; - - pub fn recovery_mode(mut self, v: Byte) -> Self { - self.recovery_mode = v; - self - } - - pub fn must(mut self, v: Byte) -> Self { - self.must = v; - self - } - - pub fn total(mut self, v: Byte) -> Self { - self.total = v; - self - } - - pub fn signers(mut self, v: LockScriptVec) -> Self { - self.signers = v; - self - } -} -impl molecule::prelude::Builder for SocialValueBuilder { - type Entity = SocialValue; - - const NAME: &'static str = "SocialValueBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.recovery_mode.as_slice().len() - + self.must.as_slice().len() - + self.total.as_slice().len() - + self.signers.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.recovery_mode.as_slice().len(); - offsets.push(total_size); - total_size += self.must.as_slice().len(); - offsets.push(total_size); - total_size += self.total.as_slice().len(); - offsets.push(total_size); - total_size += self.signers.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.recovery_mode.as_slice())?; - writer.write_all(self.must.as_slice())?; - writer.write_all(self.total.as_slice())?; - writer.write_all(self.signers.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialValue::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialKeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialKeyVec { +pub struct LockScriptVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for LockScriptVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -4814,12 +295,12 @@ impl ::core::fmt::LowerHex for SocialKeyVec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for SocialKeyVec { +impl ::core::fmt::Debug for LockScriptVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for SocialKeyVec { +impl ::core::fmt::Display for LockScriptVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -4832,21 +313,23 @@ impl ::core::fmt::Display for SocialKeyVec { write!(f, "]") } } -impl ::core::default::Default for SocialKeyVec { +impl ::core::default::Default for LockScriptVec { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SocialKeyVec::new_unchecked(v.into()) + let v: Vec = vec![4, 0, 0, 0]; + LockScriptVec::new_unchecked(v.into()) } } -impl SocialKeyVec { - pub const ITEM_SIZE: usize = 32; - +impl LockScriptVec { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -4857,7 +340,7 @@ impl SocialKeyVec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -4865,23 +348,30 @@ impl SocialKeyVec { } } - pub fn get_unchecked(&self, idx: usize) -> SocialKey { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SocialKey::new_unchecked(self.0.slice(start..end)) + pub fn get_unchecked(&self, idx: usize) -> Bytes { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + Bytes::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } } - pub fn as_reader<'r>(&'r self) -> SocialKeyVecReader<'r> { - SocialKeyVecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> LockScriptVecReader<'r> { + LockScriptVecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for SocialKeyVec { - type Builder = SocialKeyVecBuilder; +impl molecule::prelude::Entity for LockScriptVec { + type Builder = LockScriptVecBuilder; - const NAME: &'static str = "SocialKeyVec"; + const NAME: &'static str = "LockScriptVec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialKeyVec(data) + LockScriptVec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -4893,11 +383,11 @@ impl molecule::prelude::Entity for SocialKeyVec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + LockScriptVecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + LockScriptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -4909,8 +399,8 @@ impl molecule::prelude::Entity for SocialKeyVec { } } #[derive(Clone, Copy)] -pub struct SocialKeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialKeyVecReader<'r> { +pub struct LockScriptVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for LockScriptVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -4919,12 +409,12 @@ impl<'r> ::core::fmt::LowerHex for SocialKeyVecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for SocialKeyVecReader<'r> { +impl<'r> ::core::fmt::Debug for LockScriptVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for SocialKeyVecReader<'r> { +impl<'r> ::core::fmt::Display for LockScriptVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -4937,15 +427,17 @@ impl<'r> ::core::fmt::Display for SocialKeyVecReader<'r> { write!(f, "]") } } -impl<'r> SocialKeyVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - +impl<'r> LockScriptVecReader<'r> { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -4956,7 +448,7 @@ impl<'r> SocialKeyVecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -4964,90 +456,144 @@ impl<'r> SocialKeyVecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> SocialKeyReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SocialKeyReader::new_unchecked(&self.as_slice()[start..end]) + pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } } } -impl<'r> molecule::prelude::Reader<'r> for SocialKeyVecReader<'r> { - type Entity = SocialKeyVec; +impl<'r> molecule::prelude::Reader<'r> for LockScriptVecReader<'r> { + type Entity = LockScriptVec; - const NAME: &'static str = "SocialKeyVecReader"; + const NAME: &'static str = "LockScriptVecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - SocialKeyVecReader(slice) + LockScriptVecReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { return Ok(()); } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + BytesReader::verify(&slice[start..end], compatible)?; } Ok(()) } } #[derive(Debug, Default)] -pub struct SocialKeyVecBuilder(pub(crate) Vec); -impl SocialKeyVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { +pub struct LockScriptVecBuilder(pub(crate) Vec); +impl LockScriptVecBuilder { + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: SocialKey) -> Self { + pub fn push(mut self, v: Bytes) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: SocialKey) -> Option { + pub fn replace(&mut self, index: usize, v: Bytes) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for SocialKeyVecBuilder { - type Entity = SocialKeyVec; +impl molecule::prelude::Builder for LockScriptVecBuilder { + type Entity = LockScriptVec; - const NAME: &'static str = "SocialKeyVecBuilder"; + const NAME: &'static str = "LockScriptVecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } } Ok(()) } @@ -5056,12 +602,12 @@ impl molecule::prelude::Builder for SocialKeyVecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialKeyVec::new_unchecked(inner.into()) + LockScriptVec::new_unchecked(inner.into()) } } -pub struct SocialKeyVecIterator(SocialKeyVec, usize, usize); -impl ::core::iter::Iterator for SocialKeyVecIterator { - type Item = SocialKey; +pub struct LockScriptVecIterator(LockScriptVec, usize, usize); +impl ::core::iter::Iterator for LockScriptVecIterator { + type Item = Bytes; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -5073,28 +619,28 @@ impl ::core::iter::Iterator for SocialKeyVecIterator { } } } -impl ::core::iter::ExactSizeIterator for SocialKeyVecIterator { +impl ::core::iter::ExactSizeIterator for LockScriptVecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for SocialKeyVec { - type IntoIter = SocialKeyVecIterator; - type Item = SocialKey; +impl ::core::iter::IntoIterator for LockScriptVec { + type IntoIter = LockScriptVecIterator; + type Item = Bytes; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - SocialKeyVecIterator(self, 0, len) + LockScriptVecIterator(self, 0, len) } } -impl<'r> SocialKeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> SocialKeyVecReaderIterator<'t, 'r> { - SocialKeyVecReaderIterator(&self, 0, self.len()) +impl<'r> LockScriptVecReader<'r> { + pub fn iter<'t>(&'t self) -> LockScriptVecReaderIterator<'t, 'r> { + LockScriptVecReaderIterator(&self, 0, self.len()) } } -pub struct SocialKeyVecReaderIterator<'t, 'r>(&'t SocialKeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SocialKeyVecReaderIterator<'t, 'r> { - type Item = SocialKeyReader<'t>; +pub struct LockScriptVecReaderIterator<'t, 'r>(&'t LockScriptVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for LockScriptVecReaderIterator<'t, 'r> { + type Item = BytesReader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -5106,14 +652,14 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for SocialKeyVecReaderIterator<'t, 'r> { } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialKeyVecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for LockScriptVecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } } #[derive(Clone)] -pub struct SocialValueVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialValueVec { +pub struct ExtensionVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ExtensionVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -5122,12 +668,12 @@ impl ::core::fmt::LowerHex for SocialValueVec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for SocialValueVec { +impl ::core::fmt::Debug for ExtensionVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for SocialValueVec { +impl ::core::fmt::Display for ExtensionVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -5140,23 +686,21 @@ impl ::core::fmt::Display for SocialValueVec { write!(f, "]") } } -impl ::core::default::Default for SocialValueVec { +impl ::core::default::Default for ExtensionVec { fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - SocialValueVec::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0]; + ExtensionVec::new_unchecked(v.into()) } } -impl SocialValueVec { +impl ExtensionVec { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -5167,7 +711,7 @@ impl SocialValueVec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -5175,30 +719,23 @@ impl SocialValueVec { } } - pub fn get_unchecked(&self, idx: usize) -> SocialValue { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SocialValue::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SocialValue::new_unchecked(self.0.slice(start..end)) - } + pub fn get_unchecked(&self, idx: usize) -> Byte32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32::new_unchecked(self.0.slice(start..end)) } - pub fn as_reader<'r>(&'r self) -> SocialValueVecReader<'r> { - SocialValueVecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> ExtensionVecReader<'r> { + ExtensionVecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for SocialValueVec { - type Builder = SocialValueVecBuilder; +impl molecule::prelude::Entity for ExtensionVec { + type Builder = ExtensionVecBuilder; - const NAME: &'static str = "SocialValueVec"; + const NAME: &'static str = "ExtensionVec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialValueVec(data) + ExtensionVec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -5210,11 +747,11 @@ impl molecule::prelude::Entity for SocialValueVec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) + ExtensionVecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + ExtensionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -5226,8 +763,8 @@ impl molecule::prelude::Entity for SocialValueVec { } } #[derive(Clone, Copy)] -pub struct SocialValueVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialValueVecReader<'r> { +pub struct ExtensionVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for ExtensionVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -5236,12 +773,12 @@ impl<'r> ::core::fmt::LowerHex for SocialValueVecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for SocialValueVecReader<'r> { +impl<'r> ::core::fmt::Debug for ExtensionVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for SocialValueVecReader<'r> { +impl<'r> ::core::fmt::Display for ExtensionVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -5254,17 +791,15 @@ impl<'r> ::core::fmt::Display for SocialValueVecReader<'r> { write!(f, "]") } } -impl<'r> SocialValueVecReader<'r> { +impl<'r> ExtensionVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -5275,7 +810,7 @@ impl<'r> SocialValueVecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -5283,144 +818,90 @@ impl<'r> SocialValueVecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> SocialValueReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SocialValueReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SocialValueReader::new_unchecked(&self.as_slice()[start..end]) - } + pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32Reader::new_unchecked(&self.as_slice()[start..end]) } } -impl<'r> molecule::prelude::Reader<'r> for SocialValueVecReader<'r> { - type Entity = SocialValueVec; +impl<'r> molecule::prelude::Reader<'r> for ExtensionVecReader<'r> { + type Entity = ExtensionVec; - const NAME: &'static str = "SocialValueVecReader"; + const NAME: &'static str = "ExtensionVecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - SocialValueVecReader(slice) + ExtensionVecReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } return Ok(()); } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - SocialValueReader::verify(&slice[start..end], compatible)?; + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } Ok(()) } } #[derive(Debug, Default)] -pub struct SocialValueVecBuilder(pub(crate) Vec); -impl SocialValueVecBuilder { - pub fn set(mut self, v: Vec) -> Self { +pub struct ExtensionVecBuilder(pub(crate) Vec); +impl ExtensionVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: SocialValue) -> Self { + pub fn push(mut self, v: Byte32) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: SocialValue) -> Option { + pub fn replace(&mut self, index: usize, v: Byte32) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for SocialValueVecBuilder { - type Entity = SocialValueVec; +impl molecule::prelude::Builder for ExtensionVecBuilder { + type Entity = ExtensionVec; - const NAME: &'static str = "SocialValueVecBuilder"; + const NAME: &'static str = "ExtensionVecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; } Ok(()) } @@ -5429,12 +910,12 @@ impl molecule::prelude::Builder for SocialValueVecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialValueVec::new_unchecked(inner.into()) + ExtensionVec::new_unchecked(inner.into()) } } -pub struct SocialValueVecIterator(SocialValueVec, usize, usize); -impl ::core::iter::Iterator for SocialValueVecIterator { - type Item = SocialValue; +pub struct ExtensionVecIterator(ExtensionVec, usize, usize); +impl ::core::iter::Iterator for ExtensionVecIterator { + type Item = Byte32; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -5446,28 +927,28 @@ impl ::core::iter::Iterator for SocialValueVecIterator { } } } -impl ::core::iter::ExactSizeIterator for SocialValueVecIterator { +impl ::core::iter::ExactSizeIterator for ExtensionVecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for SocialValueVec { - type IntoIter = SocialValueVecIterator; - type Item = SocialValue; +impl ::core::iter::IntoIterator for ExtensionVec { + type IntoIter = ExtensionVecIterator; + type Item = Byte32; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - SocialValueVecIterator(self, 0, len) + ExtensionVecIterator(self, 0, len) } } -impl<'r> SocialValueVecReader<'r> { - pub fn iter<'t>(&'t self) -> SocialValueVecReaderIterator<'t, 'r> { - SocialValueVecReaderIterator(&self, 0, self.len()) +impl<'r> ExtensionVecReader<'r> { + pub fn iter<'t>(&'t self) -> ExtensionVecReaderIterator<'t, 'r> { + ExtensionVecReaderIterator(&self, 0, self.len()) } } -pub struct SocialValueVecReaderIterator<'t, 'r>(&'t SocialValueVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SocialValueVecReaderIterator<'t, 'r> { - type Item = SocialValueReader<'t>; +pub struct ExtensionVecReaderIterator<'t, 'r>(&'t ExtensionVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for ExtensionVecReaderIterator<'t, 'r> { + type Item = Byte32Reader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -5479,14 +960,14 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for SocialValueVecReaderIterator<'t, 'r> } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialValueVecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for ExtensionVecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } } #[derive(Clone)] -pub struct SocialEntry(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialEntry { +pub struct ExtensionLeaves(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ExtensionLeaves { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -5495,16 +976,18 @@ impl ::core::fmt::LowerHex for SocialEntry { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for SocialEntry { +impl ::core::fmt::Debug for ExtensionLeaves { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for SocialEntry { +impl ::core::fmt::Display for ExtensionLeaves { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "keys", self.keys())?; write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "old_values", self.old_values())?; + write!(f, ", {}: {}", "proof", self.proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -5512,16 +995,17 @@ impl ::core::fmt::Display for SocialEntry { write!(f, " }}") } } -impl ::core::default::Default for SocialEntry { +impl ::core::default::Default for ExtensionLeaves { fn default() -> Self { let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; - SocialEntry::new_unchecked(v.into()) + ExtensionLeaves::new_unchecked(v.into()) } } -impl SocialEntry { - pub const FIELD_COUNT: usize = 2; +impl ExtensionLeaves { + pub const FIELD_COUNT: usize = 4; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -5543,35 +1027,49 @@ impl SocialEntry { Self::FIELD_COUNT != self.field_count() } - pub fn keys(&self) -> SocialKeyVec { + pub fn keys(&self) -> ExtensionVec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[4..]) as usize; let end = molecule::unpack_number(&slice[8..]) as usize; - SocialKeyVec::new_unchecked(self.0.slice(start..end)) + ExtensionVec::new_unchecked(self.0.slice(start..end)) } - pub fn values(&self) -> SocialValueVec { + pub fn values(&self) -> ExtensionVec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ExtensionVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn old_values(&self) -> ExtensionVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ExtensionVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SocialValueVec::new_unchecked(self.0.slice(start..end)) + let end = molecule::unpack_number(&slice[20..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) } else { - SocialValueVec::new_unchecked(self.0.slice(start..)) + Bytes::new_unchecked(self.0.slice(start..)) } } - pub fn as_reader<'r>(&'r self) -> SocialEntryReader<'r> { - SocialEntryReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> ExtensionLeavesReader<'r> { + ExtensionLeavesReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for SocialEntry { - type Builder = SocialEntryBuilder; +impl molecule::prelude::Entity for ExtensionLeaves { + type Builder = ExtensionLeavesBuilder; - const NAME: &'static str = "SocialEntry"; + const NAME: &'static str = "ExtensionLeaves"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialEntry(data) + ExtensionLeaves(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -5583,11 +1081,11 @@ impl molecule::prelude::Entity for SocialEntry { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntryReader::from_slice(slice).map(|reader| reader.to_entity()) + ExtensionLeavesReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntryReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + ExtensionLeavesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -5595,12 +1093,16 @@ impl molecule::prelude::Entity for SocialEntry { } fn as_builder(self) -> Self::Builder { - Self::new_builder().keys(self.keys()).values(self.values()) + Self::new_builder() + .keys(self.keys()) + .values(self.values()) + .old_values(self.old_values()) + .proof(self.proof()) } } #[derive(Clone, Copy)] -pub struct SocialEntryReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialEntryReader<'r> { +pub struct ExtensionLeavesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for ExtensionLeavesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -5609,16 +1111,18 @@ impl<'r> ::core::fmt::LowerHex for SocialEntryReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for SocialEntryReader<'r> { +impl<'r> ::core::fmt::Debug for ExtensionLeavesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for SocialEntryReader<'r> { +impl<'r> ::core::fmt::Display for ExtensionLeavesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "keys", self.keys())?; write!(f, ", {}: {}", "values", self.values())?; + write!(f, ", {}: {}", "old_values", self.old_values())?; + write!(f, ", {}: {}", "proof", self.proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -5626,8 +1130,8 @@ impl<'r> ::core::fmt::Display for SocialEntryReader<'r> { write!(f, " }}") } } -impl<'r> SocialEntryReader<'r> { - pub const FIELD_COUNT: usize = 2; +impl<'r> ExtensionLeavesReader<'r> { + pub const FIELD_COUNT: usize = 4; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -5649,35 +1153,49 @@ impl<'r> SocialEntryReader<'r> { Self::FIELD_COUNT != self.field_count() } - pub fn keys(&self) -> SocialKeyVecReader<'r> { + pub fn keys(&self) -> ExtensionVecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[4..]) as usize; let end = molecule::unpack_number(&slice[8..]) as usize; - SocialKeyVecReader::new_unchecked(&self.as_slice()[start..end]) + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn values(&self) -> SocialValueVecReader<'r> { + pub fn values(&self) -> ExtensionVecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn old_values(&self) -> ExtensionVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ExtensionVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SocialValueVecReader::new_unchecked(&self.as_slice()[start..end]) + let end = molecule::unpack_number(&slice[20..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) } else { - SocialValueVecReader::new_unchecked(&self.as_slice()[start..]) + BytesReader::new_unchecked(&self.as_slice()[start..]) } } } -impl<'r> molecule::prelude::Reader<'r> for SocialEntryReader<'r> { - type Entity = SocialEntry; +impl<'r> molecule::prelude::Reader<'r> for ExtensionLeavesReader<'r> { + type Entity = ExtensionLeaves; - const NAME: &'static str = "SocialEntryReader"; + const NAME: &'static str = "ExtensionLeavesReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - SocialEntryReader(slice) + ExtensionLeavesReader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -5721,38 +1239,54 @@ impl<'r> molecule::prelude::Reader<'r> for SocialEntryReader<'r> { if offsets.windows(2).any(|i| i[0] > i[1]) { return ve!(Self, OffsetsNotMatch); } - SocialKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SocialValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + ExtensionVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + ExtensionVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + ExtensionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; Ok(()) } } #[derive(Debug, Default)] -pub struct SocialEntryBuilder { - pub(crate) keys: SocialKeyVec, - pub(crate) values: SocialValueVec, +pub struct ExtensionLeavesBuilder { + pub(crate) keys: ExtensionVec, + pub(crate) values: ExtensionVec, + pub(crate) old_values: ExtensionVec, + pub(crate) proof: Bytes, } -impl SocialEntryBuilder { - pub const FIELD_COUNT: usize = 2; +impl ExtensionLeavesBuilder { + pub const FIELD_COUNT: usize = 4; - pub fn keys(mut self, v: SocialKeyVec) -> Self { + pub fn keys(mut self, v: ExtensionVec) -> Self { self.keys = v; self } - pub fn values(mut self, v: SocialValueVec) -> Self { + pub fn values(mut self, v: ExtensionVec) -> Self { self.values = v; self } + + pub fn old_values(mut self, v: ExtensionVec) -> Self { + self.old_values = v; + self + } + + pub fn proof(mut self, v: Bytes) -> Self { + self.proof = v; + self + } } -impl molecule::prelude::Builder for SocialEntryBuilder { - type Entity = SocialEntry; +impl molecule::prelude::Builder for ExtensionLeavesBuilder { + type Entity = ExtensionLeaves; - const NAME: &'static str = "SocialEntryBuilder"; + const NAME: &'static str = "ExtensionLeavesBuilder"; fn expected_length(&self) -> usize { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.keys.as_slice().len() + self.values.as_slice().len() + + self.old_values.as_slice().len() + + self.proof.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { @@ -5762,12 +1296,18 @@ impl molecule::prelude::Builder for SocialEntryBuilder { total_size += self.keys.as_slice().len(); offsets.push(total_size); total_size += self.values.as_slice().len(); + offsets.push(total_size); + total_size += self.old_values.as_slice().len(); + offsets.push(total_size); + total_size += self.proof.as_slice().len(); writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; for offset in offsets.into_iter() { writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } writer.write_all(self.keys.as_slice())?; writer.write_all(self.values.as_slice())?; + writer.write_all(self.old_values.as_slice())?; + writer.write_all(self.proof.as_slice())?; Ok(()) } @@ -5775,192 +1315,7 @@ impl molecule::prelude::Builder for SocialEntryBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialEntry::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialEntryOpt(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialEntryOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl ::core::default::Default for SocialEntryOpt { - fn default() -> Self { - let v: Vec = vec![]; - SocialEntryOpt::new_unchecked(v.into()) - } -} -impl SocialEntryOpt { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - - pub fn to_opt(&self) -> Option { - if self.is_none() { - None - } else { - Some(SocialEntry::new_unchecked(self.0.clone())) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialEntryOptReader<'r> { - SocialEntryOptReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialEntryOpt { - type Builder = SocialEntryOptBuilder; - - const NAME: &'static str = "SocialEntryOpt"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialEntryOpt(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntryOptReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntryOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set(self.to_opt()) - } -} -#[derive(Clone, Copy)] -pub struct SocialEntryOptReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialEntryOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl<'r> SocialEntryOptReader<'r> { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - - pub fn to_opt(&self) -> Option> { - if self.is_none() { - None - } else { - Some(SocialEntryReader::new_unchecked(self.as_slice())) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialEntryOptReader<'r> { - type Entity = SocialEntryOpt; - - const NAME: &'static str = "SocialEntryOptReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialEntryOptReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - if !slice.is_empty() { - SocialEntryReader::verify(&slice[..], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialEntryOptBuilder(pub(crate) Option); -impl SocialEntryOptBuilder { - pub fn set(mut self, v: Option) -> Self { - self.0 = v; - self - } -} -impl molecule::prelude::Builder for SocialEntryOptBuilder { - type Entity = SocialEntryOpt; - - const NAME: &'static str = "SocialEntryOptBuilder"; - - fn expected_length(&self) -> usize { - self.0 - .as_ref() - .map(|ref inner| inner.as_slice().len()) - .unwrap_or(0) - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - self.0 - .as_ref() - .map(|ref inner| writer.write_all(inner.as_slice())) - .unwrap_or(Ok(())) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialEntryOpt::new_unchecked(inner.into()) + ExtensionLeaves::new_unchecked(inner.into()) } } #[derive(Clone)] @@ -5983,8 +1338,8 @@ impl ::core::fmt::Display for ExtensionEntries { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "leaves", self.leaves())?; - write!(f, ", {}: {}", "subkey", self.subkey())?; - write!(f, ", {}: {}", "social", self.social())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "raw_data", self.raw_data())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -5995,8 +1350,9 @@ impl ::core::fmt::Display for ExtensionEntries { impl ::core::default::Default for ExtensionEntries { fn default() -> Self { let v: Vec = vec![ - 52, 0, 0, 0, 16, 0, 0, 0, 52, 0, 0, 0, 52, 0, 0, 0, 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, - 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 62, 0, 0, 0, 16, 0, 0, 0, 52, 0, 0, 0, 58, 0, 0, 0, 36, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, + 0, 28, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, ]; ExtensionEntries::new_unchecked(v.into()) } @@ -6031,21 +1387,21 @@ impl ExtensionEntries { ExtensionLeaves::new_unchecked(self.0.slice(start..end)) } - pub fn subkey(&self) -> SubKeyEntryOpt { + pub fn sub_type(&self) -> Byte6 { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; - SubKeyEntryOpt::new_unchecked(self.0.slice(start..end)) + Byte6::new_unchecked(self.0.slice(start..end)) } - pub fn social(&self) -> SocialEntryOpt { + pub fn raw_data(&self) -> Bytes { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; if self.has_extra_fields() { let end = molecule::unpack_number(&slice[16..]) as usize; - SocialEntryOpt::new_unchecked(self.0.slice(start..end)) + Bytes::new_unchecked(self.0.slice(start..end)) } else { - SocialEntryOpt::new_unchecked(self.0.slice(start..)) + Bytes::new_unchecked(self.0.slice(start..)) } } @@ -6085,8 +1441,8 @@ impl molecule::prelude::Entity for ExtensionEntries { fn as_builder(self) -> Self::Builder { Self::new_builder() .leaves(self.leaves()) - .subkey(self.subkey()) - .social(self.social()) + .sub_type(self.sub_type()) + .raw_data(self.raw_data()) } } #[derive(Clone, Copy)] @@ -6109,8 +1465,8 @@ impl<'r> ::core::fmt::Display for ExtensionEntriesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "leaves", self.leaves())?; - write!(f, ", {}: {}", "subkey", self.subkey())?; - write!(f, ", {}: {}", "social", self.social())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "raw_data", self.raw_data())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -6148,21 +1504,21 @@ impl<'r> ExtensionEntriesReader<'r> { ExtensionLeavesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn subkey(&self) -> SubKeyEntryOptReader<'r> { + pub fn sub_type(&self) -> Byte6Reader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; - SubKeyEntryOptReader::new_unchecked(&self.as_slice()[start..end]) + Byte6Reader::new_unchecked(&self.as_slice()[start..end]) } - pub fn social(&self) -> SocialEntryOptReader<'r> { + pub fn raw_data(&self) -> BytesReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; if self.has_extra_fields() { let end = molecule::unpack_number(&slice[16..]) as usize; - SocialEntryOptReader::new_unchecked(&self.as_slice()[start..end]) + BytesReader::new_unchecked(&self.as_slice()[start..end]) } else { - SocialEntryOptReader::new_unchecked(&self.as_slice()[start..]) + BytesReader::new_unchecked(&self.as_slice()[start..]) } } } @@ -6221,16 +1577,16 @@ impl<'r> molecule::prelude::Reader<'r> for ExtensionEntriesReader<'r> { return ve!(Self, OffsetsNotMatch); } ExtensionLeavesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SubKeyEntryOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - SocialEntryOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + Byte6Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; Ok(()) } } #[derive(Debug, Default)] pub struct ExtensionEntriesBuilder { - pub(crate) leaves: ExtensionLeaves, - pub(crate) subkey: SubKeyEntryOpt, - pub(crate) social: SocialEntryOpt, + pub(crate) leaves: ExtensionLeaves, + pub(crate) sub_type: Byte6, + pub(crate) raw_data: Bytes, } impl ExtensionEntriesBuilder { pub const FIELD_COUNT: usize = 3; @@ -6240,13 +1596,13 @@ impl ExtensionEntriesBuilder { self } - pub fn subkey(mut self, v: SubKeyEntryOpt) -> Self { - self.subkey = v; + pub fn sub_type(mut self, v: Byte6) -> Self { + self.sub_type = v; self } - pub fn social(mut self, v: SocialEntryOpt) -> Self { - self.social = v; + pub fn raw_data(mut self, v: Bytes) -> Self { + self.raw_data = v; self } } @@ -6258,8 +1614,8 @@ impl molecule::prelude::Builder for ExtensionEntriesBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.leaves.as_slice().len() - + self.subkey.as_slice().len() - + self.social.as_slice().len() + + self.sub_type.as_slice().len() + + self.raw_data.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { @@ -6268,16 +1624,16 @@ impl molecule::prelude::Builder for ExtensionEntriesBuilder { offsets.push(total_size); total_size += self.leaves.as_slice().len(); offsets.push(total_size); - total_size += self.subkey.as_slice().len(); + total_size += self.sub_type.as_slice().len(); offsets.push(total_size); - total_size += self.social.as_slice().len(); + total_size += self.raw_data.as_slice().len(); writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; for offset in offsets.into_iter() { writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } writer.write_all(self.leaves.as_slice())?; - writer.write_all(self.subkey.as_slice())?; - writer.write_all(self.social.as_slice())?; + writer.write_all(self.sub_type.as_slice())?; + writer.write_all(self.raw_data.as_slice())?; Ok(()) } From b3742562f3f8893e185db342228e788c7b0435ab Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 20 Oct 2022 14:29:07 +0800 Subject: [PATCH 06/19] chore: Update rust toolchain --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- rust-toolchain | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 998ac39..296d780 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,9 +28,9 @@ checksum = "95916998c798756098a4eb1b3f2cd510659705a9817bf203d61abd30fbec3e7b" [[package]] name = "blake2b-ref" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88756126a7634ad9c13ed0a6da9aa3376a6b7000eb5fcdbb89d153196326bbcd" +checksum = "294d17c72e0ba59fad763caa112368d0672083779cdebbb97164f4bb4c1e339a" [[package]] name = "blake2b-rs" @@ -227,7 +227,7 @@ dependencies = [ name = "cota-smt" version = "0.6.0" dependencies = [ - "blake2b-ref 0.3.0", + "blake2b-ref 0.3.1", "cfg-if 1.0.0", "ckb-std", "ckb-types", diff --git a/Cargo.toml b/Cargo.toml index 3148e46..70b5faa 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2018" [dependencies] cfg-if = "1.0" -blake2b-ref = "0.3" +blake2b-ref = "0.3.1" ckb-std = {version = "0.9.0", optional = true} ckb-types = {version = "0.101.1", optional = true} molecule = {version = "0.7.2", default_features = false, optional = true} diff --git a/rust-toolchain b/rust-toolchain index 15a53c9..7e461e2 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-12-25 \ No newline at end of file +nightly-2022-08-01 \ No newline at end of file From b2b33ca35556cb1371e6a34c2cf4e93095f56a33 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 20 Oct 2022 14:33:13 +0800 Subject: [PATCH 07/19] chore: Update github actions --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 36eb67d..6e4731d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,7 +13,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: nightly-2021-12-25 + toolchain: nightly-2022-08-01 override: true - uses: docker-practice/actions-setup-docker@master From deca4466c8103d97ab9d66245c16e2da3abddc84 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 20 Oct 2022 15:40:20 +0800 Subject: [PATCH 08/19] feat: Add joyid mol --- src/joyid.mol | 62 + src/joyid.rs | 7614 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 7676 insertions(+) create mode 100644 src/joyid.mol create mode 100644 src/joyid.rs diff --git a/src/joyid.mol b/src/joyid.mol new file mode 100644 index 0000000..1f111b5 --- /dev/null +++ b/src/joyid.mol @@ -0,0 +1,62 @@ +import common; + +array Byte6 [byte; 6]; +array Byte9 [byte; 9]; +array Byte20 [byte; 20]; +array Byte24 [byte; 24]; +array Byte64 [byte; 64]; +vector LockScriptVec ; + + +struct SubKey { + smt_type: Uint16, + sub_type: Byte6, + ext_data: Uint32, + reserved: Byte20, +} +table SubValue { + key_type: Uint16, + pubkey_hash: Byte20, + reserved: Byte9, + padding: byte, +} +vector SubKeyVec ; +vector SubValueVec; +table SubKeyEntries { + keys: SubKeyVec, + values: SubValueVec, +} + +table SubKeyUnlockEntries { + ext_data: Uint32, + key_type: Uint16, + subkey_proof: Bytes, +} + + +struct SocialKey { + smt_type: Uint16, + sub_type: Byte6, + reserved: Byte24, +} +table SocialValue { + recovery_mode: byte, + must: byte, + total: byte, + signers: LockScriptVec, +} +vector SocialKeyVec ; +vector SocialValueVec ; +table SocialEntries { + keys: SocialKeyVec, + values: SocialValueVec, +} + +vector PubkeyVec ; +vector SignatureVec ; +table SocialUnlockEntries { + social_value: SocialValue, + social_proof: Bytes, + pubkeys: PubkeyVec, + signatures: SignatureVec, +} \ No newline at end of file diff --git a/src/joyid.rs b/src/joyid.rs new file mode 100644 index 0000000..94d16da --- /dev/null +++ b/src/joyid.rs @@ -0,0 +1,7614 @@ +// Generated by Molecule 0.7.3 + +use super::common::*; +use molecule::prelude::*; +#[derive(Clone)] +pub struct Byte6(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte6 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte6 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0]; + Byte6::new_unchecked(v.into()) + } +} +impl Byte6 { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte6 { + type Builder = Byte6Builder; + + const NAME: &'static str = "Byte6"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte6(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte6Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte6Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte6Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte6Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte6Reader<'r> { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte6Reader<'r> { + type Entity = Byte6; + + const NAME: &'static str = "Byte6Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte6Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte6Builder(pub(crate) [Byte; 6]); +impl ::core::fmt::Debug for Byte6Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte6Builder { + fn default() -> Self { + Byte6Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte6Builder { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; + + pub fn set(mut self, v: [Byte; 6]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } +} +impl molecule::prelude::Builder for Byte6Builder { + type Entity = Byte6; + + const NAME: &'static str = "Byte6Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte6::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte9(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte9 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte9::new_unchecked(v.into()) + } +} +impl Byte9 { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { + Byte9Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte9 { + type Builder = Byte9Builder; + + const NAME: &'static str = "Byte9"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte9(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte9Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte9Reader<'r> { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte9Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte9Builder(pub(crate) [Byte; 9]); +impl ::core::fmt::Debug for Byte9Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte9Builder { + fn default() -> Self { + Byte9Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte9Builder { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn set(mut self, v: [Byte; 9]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } +} +impl molecule::prelude::Builder for Byte9Builder { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte9::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte20(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte20 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte20::new_unchecked(v.into()) + } +} +impl Byte20 { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte20 { + type Builder = Byte20Builder; + + const NAME: &'static str = "Byte20"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte20(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte20Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte20Reader<'r> { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte20Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte20Builder(pub(crate) [Byte; 20]); +impl ::core::fmt::Debug for Byte20Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte20Builder { + fn default() -> Self { + Byte20Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte20Builder { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn set(mut self, v: [Byte; 20]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } +} +impl molecule::prelude::Builder for Byte20Builder { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte20::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte24(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte24 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + Byte24::new_unchecked(v.into()) + } +} +impl Byte24 { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { + Byte24Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte24 { + type Builder = Byte24Builder; + + const NAME: &'static str = "Byte24"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte24(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte24Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte24Reader<'r> { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte24Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte24Builder(pub(crate) [Byte; 24]); +impl ::core::fmt::Debug for Byte24Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte24Builder { + fn default() -> Self { + Byte24Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte24Builder { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn set(mut self, v: [Byte; 24]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } +} +impl molecule::prelude::Builder for Byte24Builder { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte24::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte64(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte64 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; + Byte64::new_unchecked(v.into()) + } +} +impl Byte64 { + pub const ITEM_COUNT: usize = 64; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 64; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn nth32(&self) -> Byte { + Byte::new_unchecked(self.0.slice(32..33)) + } + + pub fn nth33(&self) -> Byte { + Byte::new_unchecked(self.0.slice(33..34)) + } + + pub fn nth34(&self) -> Byte { + Byte::new_unchecked(self.0.slice(34..35)) + } + + pub fn nth35(&self) -> Byte { + Byte::new_unchecked(self.0.slice(35..36)) + } + + pub fn nth36(&self) -> Byte { + Byte::new_unchecked(self.0.slice(36..37)) + } + + pub fn nth37(&self) -> Byte { + Byte::new_unchecked(self.0.slice(37..38)) + } + + pub fn nth38(&self) -> Byte { + Byte::new_unchecked(self.0.slice(38..39)) + } + + pub fn nth39(&self) -> Byte { + Byte::new_unchecked(self.0.slice(39..40)) + } + + pub fn nth40(&self) -> Byte { + Byte::new_unchecked(self.0.slice(40..41)) + } + + pub fn nth41(&self) -> Byte { + Byte::new_unchecked(self.0.slice(41..42)) + } + + pub fn nth42(&self) -> Byte { + Byte::new_unchecked(self.0.slice(42..43)) + } + + pub fn nth43(&self) -> Byte { + Byte::new_unchecked(self.0.slice(43..44)) + } + + pub fn nth44(&self) -> Byte { + Byte::new_unchecked(self.0.slice(44..45)) + } + + pub fn nth45(&self) -> Byte { + Byte::new_unchecked(self.0.slice(45..46)) + } + + pub fn nth46(&self) -> Byte { + Byte::new_unchecked(self.0.slice(46..47)) + } + + pub fn nth47(&self) -> Byte { + Byte::new_unchecked(self.0.slice(47..48)) + } + + pub fn nth48(&self) -> Byte { + Byte::new_unchecked(self.0.slice(48..49)) + } + + pub fn nth49(&self) -> Byte { + Byte::new_unchecked(self.0.slice(49..50)) + } + + pub fn nth50(&self) -> Byte { + Byte::new_unchecked(self.0.slice(50..51)) + } + + pub fn nth51(&self) -> Byte { + Byte::new_unchecked(self.0.slice(51..52)) + } + + pub fn nth52(&self) -> Byte { + Byte::new_unchecked(self.0.slice(52..53)) + } + + pub fn nth53(&self) -> Byte { + Byte::new_unchecked(self.0.slice(53..54)) + } + + pub fn nth54(&self) -> Byte { + Byte::new_unchecked(self.0.slice(54..55)) + } + + pub fn nth55(&self) -> Byte { + Byte::new_unchecked(self.0.slice(55..56)) + } + + pub fn nth56(&self) -> Byte { + Byte::new_unchecked(self.0.slice(56..57)) + } + + pub fn nth57(&self) -> Byte { + Byte::new_unchecked(self.0.slice(57..58)) + } + + pub fn nth58(&self) -> Byte { + Byte::new_unchecked(self.0.slice(58..59)) + } + + pub fn nth59(&self) -> Byte { + Byte::new_unchecked(self.0.slice(59..60)) + } + + pub fn nth60(&self) -> Byte { + Byte::new_unchecked(self.0.slice(60..61)) + } + + pub fn nth61(&self) -> Byte { + Byte::new_unchecked(self.0.slice(61..62)) + } + + pub fn nth62(&self) -> Byte { + Byte::new_unchecked(self.0.slice(62..63)) + } + + pub fn nth63(&self) -> Byte { + Byte::new_unchecked(self.0.slice(63..64)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte64Reader<'r> { + Byte64Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte64 { + type Builder = Byte64Builder; + + const NAME: &'static str = "Byte64"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte64(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte64Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + self.nth32(), + self.nth33(), + self.nth34(), + self.nth35(), + self.nth36(), + self.nth37(), + self.nth38(), + self.nth39(), + self.nth40(), + self.nth41(), + self.nth42(), + self.nth43(), + self.nth44(), + self.nth45(), + self.nth46(), + self.nth47(), + self.nth48(), + self.nth49(), + self.nth50(), + self.nth51(), + self.nth52(), + self.nth53(), + self.nth54(), + self.nth55(), + self.nth56(), + self.nth57(), + self.nth58(), + self.nth59(), + self.nth60(), + self.nth61(), + self.nth62(), + self.nth63(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte64Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte64Reader<'r> { + pub const ITEM_COUNT: usize = 64; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 64; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn nth32(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[32..33]) + } + + pub fn nth33(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[33..34]) + } + + pub fn nth34(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[34..35]) + } + + pub fn nth35(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[35..36]) + } + + pub fn nth36(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[36..37]) + } + + pub fn nth37(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[37..38]) + } + + pub fn nth38(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[38..39]) + } + + pub fn nth39(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[39..40]) + } + + pub fn nth40(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[40..41]) + } + + pub fn nth41(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[41..42]) + } + + pub fn nth42(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[42..43]) + } + + pub fn nth43(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[43..44]) + } + + pub fn nth44(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[44..45]) + } + + pub fn nth45(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[45..46]) + } + + pub fn nth46(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[46..47]) + } + + pub fn nth47(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[47..48]) + } + + pub fn nth48(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[48..49]) + } + + pub fn nth49(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[49..50]) + } + + pub fn nth50(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[50..51]) + } + + pub fn nth51(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[51..52]) + } + + pub fn nth52(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[52..53]) + } + + pub fn nth53(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[53..54]) + } + + pub fn nth54(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[54..55]) + } + + pub fn nth55(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[55..56]) + } + + pub fn nth56(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[56..57]) + } + + pub fn nth57(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[57..58]) + } + + pub fn nth58(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[58..59]) + } + + pub fn nth59(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[59..60]) + } + + pub fn nth60(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[60..61]) + } + + pub fn nth61(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[61..62]) + } + + pub fn nth62(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[62..63]) + } + + pub fn nth63(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[63..64]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte64Reader<'r> { + type Entity = Byte64; + + const NAME: &'static str = "Byte64Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte64Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte64Builder(pub(crate) [Byte; 64]); +impl ::core::fmt::Debug for Byte64Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte64Builder { + fn default() -> Self { + Byte64Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte64Builder { + pub const ITEM_COUNT: usize = 64; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 64; + + pub fn set(mut self, v: [Byte; 64]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self + } + + pub fn nth26(mut self, v: Byte) -> Self { + self.0[26] = v; + self + } + + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } + + pub fn nth32(mut self, v: Byte) -> Self { + self.0[32] = v; + self + } + + pub fn nth33(mut self, v: Byte) -> Self { + self.0[33] = v; + self + } + + pub fn nth34(mut self, v: Byte) -> Self { + self.0[34] = v; + self + } + + pub fn nth35(mut self, v: Byte) -> Self { + self.0[35] = v; + self + } + + pub fn nth36(mut self, v: Byte) -> Self { + self.0[36] = v; + self + } + + pub fn nth37(mut self, v: Byte) -> Self { + self.0[37] = v; + self + } + + pub fn nth38(mut self, v: Byte) -> Self { + self.0[38] = v; + self + } + + pub fn nth39(mut self, v: Byte) -> Self { + self.0[39] = v; + self + } + + pub fn nth40(mut self, v: Byte) -> Self { + self.0[40] = v; + self + } + + pub fn nth41(mut self, v: Byte) -> Self { + self.0[41] = v; + self + } + + pub fn nth42(mut self, v: Byte) -> Self { + self.0[42] = v; + self + } + + pub fn nth43(mut self, v: Byte) -> Self { + self.0[43] = v; + self + } + + pub fn nth44(mut self, v: Byte) -> Self { + self.0[44] = v; + self + } + + pub fn nth45(mut self, v: Byte) -> Self { + self.0[45] = v; + self + } + + pub fn nth46(mut self, v: Byte) -> Self { + self.0[46] = v; + self + } + + pub fn nth47(mut self, v: Byte) -> Self { + self.0[47] = v; + self + } + + pub fn nth48(mut self, v: Byte) -> Self { + self.0[48] = v; + self + } + + pub fn nth49(mut self, v: Byte) -> Self { + self.0[49] = v; + self + } + + pub fn nth50(mut self, v: Byte) -> Self { + self.0[50] = v; + self + } + + pub fn nth51(mut self, v: Byte) -> Self { + self.0[51] = v; + self + } + + pub fn nth52(mut self, v: Byte) -> Self { + self.0[52] = v; + self + } + + pub fn nth53(mut self, v: Byte) -> Self { + self.0[53] = v; + self + } + + pub fn nth54(mut self, v: Byte) -> Self { + self.0[54] = v; + self + } + + pub fn nth55(mut self, v: Byte) -> Self { + self.0[55] = v; + self + } + + pub fn nth56(mut self, v: Byte) -> Self { + self.0[56] = v; + self + } + + pub fn nth57(mut self, v: Byte) -> Self { + self.0[57] = v; + self + } + + pub fn nth58(mut self, v: Byte) -> Self { + self.0[58] = v; + self + } + + pub fn nth59(mut self, v: Byte) -> Self { + self.0[59] = v; + self + } + + pub fn nth60(mut self, v: Byte) -> Self { + self.0[60] = v; + self + } + + pub fn nth61(mut self, v: Byte) -> Self { + self.0[61] = v; + self + } + + pub fn nth62(mut self, v: Byte) -> Self { + self.0[62] = v; + self + } + + pub fn nth63(mut self, v: Byte) -> Self { + self.0[63] = v; + self + } +} +impl molecule::prelude::Builder for Byte64Builder { + type Entity = Byte64; + + const NAME: &'static str = "Byte64Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + writer.write_all(self.0[32].as_slice())?; + writer.write_all(self.0[33].as_slice())?; + writer.write_all(self.0[34].as_slice())?; + writer.write_all(self.0[35].as_slice())?; + writer.write_all(self.0[36].as_slice())?; + writer.write_all(self.0[37].as_slice())?; + writer.write_all(self.0[38].as_slice())?; + writer.write_all(self.0[39].as_slice())?; + writer.write_all(self.0[40].as_slice())?; + writer.write_all(self.0[41].as_slice())?; + writer.write_all(self.0[42].as_slice())?; + writer.write_all(self.0[43].as_slice())?; + writer.write_all(self.0[44].as_slice())?; + writer.write_all(self.0[45].as_slice())?; + writer.write_all(self.0[46].as_slice())?; + writer.write_all(self.0[47].as_slice())?; + writer.write_all(self.0[48].as_slice())?; + writer.write_all(self.0[49].as_slice())?; + writer.write_all(self.0[50].as_slice())?; + writer.write_all(self.0[51].as_slice())?; + writer.write_all(self.0[52].as_slice())?; + writer.write_all(self.0[53].as_slice())?; + writer.write_all(self.0[54].as_slice())?; + writer.write_all(self.0[55].as_slice())?; + writer.write_all(self.0[56].as_slice())?; + writer.write_all(self.0[57].as_slice())?; + writer.write_all(self.0[58].as_slice())?; + writer.write_all(self.0[59].as_slice())?; + writer.write_all(self.0[60].as_slice())?; + writer.write_all(self.0[61].as_slice())?; + writer.write_all(self.0[62].as_slice())?; + writer.write_all(self.0[63].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte64::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct LockScriptVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for LockScriptVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for LockScriptVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + LockScriptVec::new_unchecked(v.into()) + } +} +impl LockScriptVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Bytes { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + Bytes::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> LockScriptVecReader<'r> { + LockScriptVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for LockScriptVec { + type Builder = LockScriptVecBuilder; + + const NAME: &'static str = "LockScriptVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + LockScriptVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + LockScriptVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + LockScriptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct LockScriptVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for LockScriptVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> LockScriptVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for LockScriptVecReader<'r> { + type Entity = LockScriptVec; + + const NAME: &'static str = "LockScriptVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + LockScriptVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + BytesReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct LockScriptVecBuilder(pub(crate) Vec); +impl LockScriptVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Bytes) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Bytes) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for LockScriptVecBuilder { + type Entity = LockScriptVec; + + const NAME: &'static str = "LockScriptVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + LockScriptVec::new_unchecked(inner.into()) + } +} +pub struct LockScriptVecIterator(LockScriptVec, usize, usize); +impl ::core::iter::Iterator for LockScriptVecIterator { + type Item = Bytes; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for LockScriptVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for LockScriptVec { + type IntoIter = LockScriptVecIterator; + type Item = Bytes; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + LockScriptVecIterator(self, 0, len) + } +} +impl<'r> LockScriptVecReader<'r> { + pub fn iter<'t>(&'t self) -> LockScriptVecReaderIterator<'t, 'r> { + LockScriptVecReaderIterator(&self, 0, self.len()) + } +} +pub struct LockScriptVecReaderIterator<'t, 'r>(&'t LockScriptVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for LockScriptVecReaderIterator<'t, 'r> { + type Item = BytesReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for LockScriptVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SubKey(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl ::core::default::Default for SubKey { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + SubKey::new_unchecked(v.into()) + } +} +impl SubKey { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) + } + + pub fn sub_type(&self) -> Byte6 { + Byte6::new_unchecked(self.0.slice(2..8)) + } + + pub fn ext_data(&self) -> Uint32 { + Uint32::new_unchecked(self.0.slice(8..12)) + } + + pub fn reserved(&self) -> Byte20 { + Byte20::new_unchecked(self.0.slice(12..32)) + } + + pub fn as_reader<'r>(&'r self) -> SubKeyReader<'r> { + SubKeyReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKey { + type Builder = SubKeyBuilder; + + const NAME: &'static str = "SubKey"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKey(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .smt_type(self.smt_type()) + .sub_type(self.sub_type()) + .ext_data(self.ext_data()) + .reserved(self.reserved()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl<'r> SubKeyReader<'r> { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + } + + pub fn sub_type(&self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(&self.as_slice()[2..8]) + } + + pub fn ext_data(&self) -> Uint32Reader<'r> { + Uint32Reader::new_unchecked(&self.as_slice()[8..12]) + } + + pub fn reserved(&self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(&self.as_slice()[12..32]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyReader<'r> { + type Entity = SubKey; + + const NAME: &'static str = "SubKeyReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyBuilder { + pub(crate) smt_type: Uint16, + pub(crate) sub_type: Byte6, + pub(crate) ext_data: Uint32, + pub(crate) reserved: Byte20, +} +impl SubKeyBuilder { + pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(mut self, v: Uint16) -> Self { + self.smt_type = v; + self + } + + pub fn sub_type(mut self, v: Byte6) -> Self { + self.sub_type = v; + self + } + + pub fn ext_data(mut self, v: Uint32) -> Self { + self.ext_data = v; + self + } + + pub fn reserved(mut self, v: Byte20) -> Self { + self.reserved = v; + self + } +} +impl molecule::prelude::Builder for SubKeyBuilder { + type Entity = SubKey; + + const NAME: &'static str = "SubKeyBuilder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.smt_type.as_slice())?; + writer.write_all(self.sub_type.as_slice())?; + writer.write_all(self.ext_data.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKey::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubValue(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, ", {}: {}", "padding", self.padding())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SubValue { + fn default() -> Self { + let v: Vec = vec![ + 52, 0, 0, 0, 20, 0, 0, 0, 22, 0, 0, 0, 42, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + SubValue::new_unchecked(v.into()) + } +} +impl SubValue { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn key_type(&self) -> Uint16 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint16::new_unchecked(self.0.slice(start..end)) + } + + pub fn pubkey_hash(&self) -> Byte20 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Byte20::new_unchecked(self.0.slice(start..end)) + } + + pub fn reserved(&self) -> Byte9 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + Byte9::new_unchecked(self.0.slice(start..end)) + } + + pub fn padding(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } else { + Byte::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SubValueReader<'r> { + SubValueReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubValue { + type Builder = SubValueBuilder; + + const NAME: &'static str = "SubValue"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubValue(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .key_type(self.key_type()) + .pubkey_hash(self.pubkey_hash()) + .reserved(self.reserved()) + .padding(self.padding()) + } +} +#[derive(Clone, Copy)] +pub struct SubValueReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, ", {}: {}", "padding", self.padding())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SubValueReader<'r> { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn key_type(&self) -> Uint16Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint16Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn pubkey_hash(&self) -> Byte20Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Byte20Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn reserved(&self) -> Byte9Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + Byte9Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn padding(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } else { + ByteReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { + type Entity = SubValue; + + const NAME: &'static str = "SubValueReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubValueReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + Uint16Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + Byte20Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Byte9Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + ByteReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubValueBuilder { + pub(crate) key_type: Uint16, + pub(crate) pubkey_hash: Byte20, + pub(crate) reserved: Byte9, + pub(crate) padding: Byte, +} +impl SubValueBuilder { + pub const FIELD_COUNT: usize = 4; + + pub fn key_type(mut self, v: Uint16) -> Self { + self.key_type = v; + self + } + + pub fn pubkey_hash(mut self, v: Byte20) -> Self { + self.pubkey_hash = v; + self + } + + pub fn reserved(mut self, v: Byte9) -> Self { + self.reserved = v; + self + } + + pub fn padding(mut self, v: Byte) -> Self { + self.padding = v; + self + } +} +impl molecule::prelude::Builder for SubValueBuilder { + type Entity = SubValue; + + const NAME: &'static str = "SubValueBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.key_type.as_slice().len() + + self.pubkey_hash.as_slice().len() + + self.reserved.as_slice().len() + + self.padding.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.key_type.as_slice().len(); + offsets.push(total_size); + total_size += self.pubkey_hash.as_slice().len(); + offsets.push(total_size); + total_size += self.reserved.as_slice().len(); + offsets.push(total_size); + total_size += self.padding.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.key_type.as_slice())?; + writer.write_all(self.pubkey_hash.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + writer.write_all(self.padding.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubValue::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubKeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SubKeyVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SubKeyVec::new_unchecked(v.into()) + } +} +impl SubKeyVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubKey { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubKey::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SubKeyVecReader<'r> { + SubKeyVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyVec { + type Builder = SubKeyVecBuilder; + + const NAME: &'static str = "SubKeyVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SubKeyVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubKeyReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubKeyReader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyVecReader<'r> { + type Entity = SubKeyVec; + + const NAME: &'static str = "SubKeyVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyVecBuilder(pub(crate) Vec); +impl SubKeyVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SubKey) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SubKey) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SubKeyVecBuilder { + type Entity = SubKeyVec; + + const NAME: &'static str = "SubKeyVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyVec::new_unchecked(inner.into()) + } +} +pub struct SubKeyVecIterator(SubKeyVec, usize, usize); +impl ::core::iter::Iterator for SubKeyVecIterator { + type Item = SubKey; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SubKeyVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SubKeyVec { + type IntoIter = SubKeyVecIterator; + type Item = SubKey; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SubKeyVecIterator(self, 0, len) + } +} +impl<'r> SubKeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> SubKeyVecReaderIterator<'t, 'r> { + SubKeyVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SubKeyVecReaderIterator<'t, 'r>(&'t SubKeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SubKeyVecReaderIterator<'t, 'r> { + type Item = SubKeyReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubKeyVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SubValueVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SubValueVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + SubValueVec::new_unchecked(v.into()) + } +} +impl SubValueVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubValue { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SubValue::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SubValue::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> SubValueVecReader<'r> { + SubValueVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubValueVec { + type Builder = SubValueVecBuilder; + + const NAME: &'static str = "SubValueVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubValueVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SubValueVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SubValueVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SubValueReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SubValueReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SubValueReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { + type Entity = SubValueVec; + + const NAME: &'static str = "SubValueVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubValueVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + SubValueReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubValueVecBuilder(pub(crate) Vec); +impl SubValueVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SubValue) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SubValue) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SubValueVecBuilder { + type Entity = SubValueVec; + + const NAME: &'static str = "SubValueVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubValueVec::new_unchecked(inner.into()) + } +} +pub struct SubValueVecIterator(SubValueVec, usize, usize); +impl ::core::iter::Iterator for SubValueVecIterator { + type Item = SubValue; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SubValueVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SubValueVec { + type IntoIter = SubValueVecIterator; + type Item = SubValue; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SubValueVecIterator(self, 0, len) + } +} +impl<'r> SubValueVecReader<'r> { + pub fn iter<'t>(&'t self) -> SubValueVecReaderIterator<'t, 'r> { + SubValueVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SubValueVecReaderIterator<'t, 'r>(&'t SubValueVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SubValueVecReaderIterator<'t, 'r> { + type Item = SubValueReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubValueVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SubKeyEntries(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SubKeyEntries { + fn default() -> Self { + let v: Vec = vec![ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; + SubKeyEntries::new_unchecked(v.into()) + } +} +impl SubKeyEntries { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SubKeyVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SubKeyVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn values(&self) -> SubValueVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SubValueVec::new_unchecked(self.0.slice(start..end)) + } else { + SubValueVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SubKeyEntriesReader<'r> { + SubKeyEntriesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyEntries { + type Builder = SubKeyEntriesBuilder; + + const NAME: &'static str = "SubKeyEntries"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyEntries(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().keys(self.keys()).values(self.values()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyEntriesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SubKeyEntriesReader<'r> { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SubKeyVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SubKeyVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn values(&self) -> SubValueVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SubValueVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SubValueVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyEntriesReader<'r> { + type Entity = SubKeyEntries; + + const NAME: &'static str = "SubKeyEntriesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyEntriesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + SubKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + SubValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyEntriesBuilder { + pub(crate) keys: SubKeyVec, + pub(crate) values: SubValueVec, +} +impl SubKeyEntriesBuilder { + pub const FIELD_COUNT: usize = 2; + + pub fn keys(mut self, v: SubKeyVec) -> Self { + self.keys = v; + self + } + + pub fn values(mut self, v: SubValueVec) -> Self { + self.values = v; + self + } +} +impl molecule::prelude::Builder for SubKeyEntriesBuilder { + type Entity = SubKeyEntries; + + const NAME: &'static str = "SubKeyEntriesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.keys.as_slice().len() + + self.values.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.keys.as_slice().len(); + offsets.push(total_size); + total_size += self.values.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.keys.as_slice())?; + writer.write_all(self.values.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyEntries::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SubKeyUnlockEntries(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SubKeyUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SubKeyUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SubKeyUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SubKeyUnlockEntries { + fn default() -> Self { + let v: Vec = vec![ + 26, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + SubKeyUnlockEntries::new_unchecked(v.into()) + } +} +impl SubKeyUnlockEntries { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn ext_data(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint32::new_unchecked(self.0.slice(start..end)) + } + + pub fn key_type(&self) -> Uint16 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Uint16::new_unchecked(self.0.slice(start..end)) + } + + pub fn subkey_proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } else { + Bytes::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SubKeyUnlockEntriesReader<'r> { + SubKeyUnlockEntriesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SubKeyUnlockEntries { + type Builder = SubKeyUnlockEntriesBuilder; + + const NAME: &'static str = "SubKeyUnlockEntries"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SubKeyUnlockEntries(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyUnlockEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SubKeyUnlockEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .ext_data(self.ext_data()) + .key_type(self.key_type()) + .subkey_proof(self.subkey_proof()) + } +} +#[derive(Clone, Copy)] +pub struct SubKeyUnlockEntriesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SubKeyUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SubKeyUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SubKeyUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SubKeyUnlockEntriesReader<'r> { + pub const FIELD_COUNT: usize = 3; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn ext_data(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint32Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn key_type(&self) -> Uint16Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Uint16Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn subkey_proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[16..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } else { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SubKeyUnlockEntriesReader<'r> { + type Entity = SubKeyUnlockEntries; + + const NAME: &'static str = "SubKeyUnlockEntriesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SubKeyUnlockEntriesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + Uint16Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SubKeyUnlockEntriesBuilder { + pub(crate) ext_data: Uint32, + pub(crate) key_type: Uint16, + pub(crate) subkey_proof: Bytes, +} +impl SubKeyUnlockEntriesBuilder { + pub const FIELD_COUNT: usize = 3; + + pub fn ext_data(mut self, v: Uint32) -> Self { + self.ext_data = v; + self + } + + pub fn key_type(mut self, v: Uint16) -> Self { + self.key_type = v; + self + } + + pub fn subkey_proof(mut self, v: Bytes) -> Self { + self.subkey_proof = v; + self + } +} +impl molecule::prelude::Builder for SubKeyUnlockEntriesBuilder { + type Entity = SubKeyUnlockEntries; + + const NAME: &'static str = "SubKeyUnlockEntriesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.ext_data.as_slice().len() + + self.key_type.as_slice().len() + + self.subkey_proof.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.ext_data.as_slice().len(); + offsets.push(total_size); + total_size += self.key_type.as_slice().len(); + offsets.push(total_size); + total_size += self.subkey_proof.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.ext_data.as_slice())?; + writer.write_all(self.key_type.as_slice())?; + writer.write_all(self.subkey_proof.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SubKeyUnlockEntries::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialKey(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl ::core::default::Default for SocialKey { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + SocialKey::new_unchecked(v.into()) + } +} +impl SocialKey { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) + } + + pub fn sub_type(&self) -> Byte6 { + Byte6::new_unchecked(self.0.slice(2..8)) + } + + pub fn reserved(&self) -> Byte24 { + Byte24::new_unchecked(self.0.slice(8..32)) + } + + pub fn as_reader<'r>(&'r self) -> SocialKeyReader<'r> { + SocialKeyReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialKey { + type Builder = SocialKeyBuilder; + + const NAME: &'static str = "SocialKey"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialKey(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .smt_type(self.smt_type()) + .sub_type(self.sub_type()) + .reserved(self.reserved()) + } +} +#[derive(Clone, Copy)] +pub struct SocialKeyReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialKeyReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "sub_type", self.sub_type())?; + write!(f, ", {}: {}", "reserved", self.reserved())?; + write!(f, " }}") + } +} +impl<'r> SocialKeyReader<'r> { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + } + + pub fn sub_type(&self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(&self.as_slice()[2..8]) + } + + pub fn reserved(&self) -> Byte24Reader<'r> { + Byte24Reader::new_unchecked(&self.as_slice()[8..32]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialKeyReader<'r> { + type Entity = SocialKey; + + const NAME: &'static str = "SocialKeyReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialKeyReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialKeyBuilder { + pub(crate) smt_type: Uint16, + pub(crate) sub_type: Byte6, + pub(crate) reserved: Byte24, +} +impl SocialKeyBuilder { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; + pub const TOTAL_SIZE: usize = 32; + + pub fn smt_type(mut self, v: Uint16) -> Self { + self.smt_type = v; + self + } + + pub fn sub_type(mut self, v: Byte6) -> Self { + self.sub_type = v; + self + } + + pub fn reserved(mut self, v: Byte24) -> Self { + self.reserved = v; + self + } +} +impl molecule::prelude::Builder for SocialKeyBuilder { + type Entity = SocialKey; + + const NAME: &'static str = "SocialKeyBuilder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.smt_type.as_slice())?; + writer.write_all(self.sub_type.as_slice())?; + writer.write_all(self.reserved.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialKey::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialValue(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialValue { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; + write!(f, ", {}: {}", "must", self.must())?; + write!(f, ", {}: {}", "total", self.total())?; + write!(f, ", {}: {}", "signers", self.signers())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SocialValue { + fn default() -> Self { + let v: Vec = vec![ + 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; + SocialValue::new_unchecked(v.into()) + } +} +impl SocialValue { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn recovery_mode(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn must(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn total(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn signers(&self) -> LockScriptVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + LockScriptVec::new_unchecked(self.0.slice(start..end)) + } else { + LockScriptVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialValueReader<'r> { + SocialValueReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialValue { + type Builder = SocialValueBuilder; + + const NAME: &'static str = "SocialValue"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialValue(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .recovery_mode(self.recovery_mode()) + .must(self.must()) + .total(self.total()) + .signers(self.signers()) + } +} +#[derive(Clone, Copy)] +pub struct SocialValueReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialValueReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; + write!(f, ", {}: {}", "must", self.must())?; + write!(f, ", {}: {}", "total", self.total())?; + write!(f, ", {}: {}", "signers", self.signers())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SocialValueReader<'r> { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn recovery_mode(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn must(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn total(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn signers(&self) -> LockScriptVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + LockScriptVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + LockScriptVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialValueReader<'r> { + type Entity = SocialValue; + + const NAME: &'static str = "SocialValueReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialValueReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + LockScriptVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialValueBuilder { + pub(crate) recovery_mode: Byte, + pub(crate) must: Byte, + pub(crate) total: Byte, + pub(crate) signers: LockScriptVec, +} +impl SocialValueBuilder { + pub const FIELD_COUNT: usize = 4; + + pub fn recovery_mode(mut self, v: Byte) -> Self { + self.recovery_mode = v; + self + } + + pub fn must(mut self, v: Byte) -> Self { + self.must = v; + self + } + + pub fn total(mut self, v: Byte) -> Self { + self.total = v; + self + } + + pub fn signers(mut self, v: LockScriptVec) -> Self { + self.signers = v; + self + } +} +impl molecule::prelude::Builder for SocialValueBuilder { + type Entity = SocialValue; + + const NAME: &'static str = "SocialValueBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.recovery_mode.as_slice().len() + + self.must.as_slice().len() + + self.total.as_slice().len() + + self.signers.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.recovery_mode.as_slice().len(); + offsets.push(total_size); + total_size += self.must.as_slice().len(); + offsets.push(total_size); + total_size += self.total.as_slice().len(); + offsets.push(total_size); + total_size += self.signers.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.recovery_mode.as_slice())?; + writer.write_all(self.must.as_slice())?; + writer.write_all(self.total.as_slice())?; + writer.write_all(self.signers.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialValue::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct SocialKeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialKeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SocialKeyVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SocialKeyVec::new_unchecked(v.into()) + } +} +impl SocialKeyVec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialKey { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SocialKey::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SocialKeyVecReader<'r> { + SocialKeyVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialKeyVec { + type Builder = SocialKeyVecBuilder; + + const NAME: &'static str = "SocialKeyVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialKeyVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SocialKeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialKeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SocialKeyVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialKeyReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SocialKeyReader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialKeyVecReader<'r> { + type Entity = SocialKeyVec; + + const NAME: &'static str = "SocialKeyVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialKeyVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialKeyVecBuilder(pub(crate) Vec); +impl SocialKeyVecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SocialKey) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SocialKey) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SocialKeyVecBuilder { + type Entity = SocialKeyVec; + + const NAME: &'static str = "SocialKeyVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialKeyVec::new_unchecked(inner.into()) + } +} +pub struct SocialKeyVecIterator(SocialKeyVec, usize, usize); +impl ::core::iter::Iterator for SocialKeyVecIterator { + type Item = SocialKey; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SocialKeyVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SocialKeyVec { + type IntoIter = SocialKeyVecIterator; + type Item = SocialKey; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SocialKeyVecIterator(self, 0, len) + } +} +impl<'r> SocialKeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> SocialKeyVecReaderIterator<'t, 'r> { + SocialKeyVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SocialKeyVecReaderIterator<'t, 'r>(&'t SocialKeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SocialKeyVecReaderIterator<'t, 'r> { + type Item = SocialKeyReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialKeyVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SocialValueVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialValueVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SocialValueVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + SocialValueVec::new_unchecked(v.into()) + } +} +impl SocialValueVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialValue { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SocialValue::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SocialValue::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialValueVecReader<'r> { + SocialValueVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialValueVec { + type Builder = SocialValueVecBuilder; + + const NAME: &'static str = "SocialValueVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialValueVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SocialValueVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialValueVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SocialValueVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> SocialValueReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + SocialValueReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + SocialValueReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialValueVecReader<'r> { + type Entity = SocialValueVec; + + const NAME: &'static str = "SocialValueVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialValueVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + SocialValueReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialValueVecBuilder(pub(crate) Vec); +impl SocialValueVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: SocialValue) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: SocialValue) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SocialValueVecBuilder { + type Entity = SocialValueVec; + + const NAME: &'static str = "SocialValueVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialValueVec::new_unchecked(inner.into()) + } +} +pub struct SocialValueVecIterator(SocialValueVec, usize, usize); +impl ::core::iter::Iterator for SocialValueVecIterator { + type Item = SocialValue; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SocialValueVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SocialValueVec { + type IntoIter = SocialValueVecIterator; + type Item = SocialValue; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SocialValueVecIterator(self, 0, len) + } +} +impl<'r> SocialValueVecReader<'r> { + pub fn iter<'t>(&'t self) -> SocialValueVecReaderIterator<'t, 'r> { + SocialValueVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SocialValueVecReaderIterator<'t, 'r>(&'t SocialValueVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SocialValueVecReaderIterator<'t, 'r> { + type Item = SocialValueReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialValueVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SocialEntries(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SocialEntries { + fn default() -> Self { + let v: Vec = vec![ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; + SocialEntries::new_unchecked(v.into()) + } +} +impl SocialEntries { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SocialKeyVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialKeyVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn values(&self) -> SocialValueVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SocialValueVec::new_unchecked(self.0.slice(start..end)) + } else { + SocialValueVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialEntriesReader<'r> { + SocialEntriesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialEntries { + type Builder = SocialEntriesBuilder; + + const NAME: &'static str = "SocialEntries"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialEntries(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().keys(self.keys()).values(self.values()) + } +} +#[derive(Clone, Copy)] +pub struct SocialEntriesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "keys", self.keys())?; + write!(f, ", {}: {}", "values", self.values())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SocialEntriesReader<'r> { + pub const FIELD_COUNT: usize = 2; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn keys(&self) -> SocialKeyVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialKeyVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn values(&self) -> SocialValueVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[12..]) as usize; + SocialValueVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SocialValueVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialEntriesReader<'r> { + type Entity = SocialEntries; + + const NAME: &'static str = "SocialEntriesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialEntriesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + SocialKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + SocialValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialEntriesBuilder { + pub(crate) keys: SocialKeyVec, + pub(crate) values: SocialValueVec, +} +impl SocialEntriesBuilder { + pub const FIELD_COUNT: usize = 2; + + pub fn keys(mut self, v: SocialKeyVec) -> Self { + self.keys = v; + self + } + + pub fn values(mut self, v: SocialValueVec) -> Self { + self.values = v; + self + } +} +impl molecule::prelude::Builder for SocialEntriesBuilder { + type Entity = SocialEntries; + + const NAME: &'static str = "SocialEntriesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.keys.as_slice().len() + + self.values.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.keys.as_slice().len(); + offsets.push(total_size); + total_size += self.values.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.keys.as_slice())?; + writer.write_all(self.values.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialEntries::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct PubkeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for PubkeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for PubkeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for PubkeyVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for PubkeyVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + PubkeyVec::new_unchecked(v.into()) + } +} +impl PubkeyVec { + pub const ITEM_SIZE: usize = 64; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte64 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte64::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> PubkeyVecReader<'r> { + PubkeyVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for PubkeyVec { + type Builder = PubkeyVecBuilder; + + const NAME: &'static str = "PubkeyVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + PubkeyVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + PubkeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + PubkeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct PubkeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for PubkeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for PubkeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for PubkeyVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> PubkeyVecReader<'r> { + pub const ITEM_SIZE: usize = 64; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte64Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte64Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for PubkeyVecReader<'r> { + type Entity = PubkeyVec; + + const NAME: &'static str = "PubkeyVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + PubkeyVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct PubkeyVecBuilder(pub(crate) Vec); +impl PubkeyVecBuilder { + pub const ITEM_SIZE: usize = 64; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Byte64) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Byte64) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for PubkeyVecBuilder { + type Entity = PubkeyVec; + + const NAME: &'static str = "PubkeyVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + PubkeyVec::new_unchecked(inner.into()) + } +} +pub struct PubkeyVecIterator(PubkeyVec, usize, usize); +impl ::core::iter::Iterator for PubkeyVecIterator { + type Item = Byte64; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for PubkeyVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for PubkeyVec { + type IntoIter = PubkeyVecIterator; + type Item = Byte64; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + PubkeyVecIterator(self, 0, len) + } +} +impl<'r> PubkeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> PubkeyVecReaderIterator<'t, 'r> { + PubkeyVecReaderIterator(&self, 0, self.len()) + } +} +pub struct PubkeyVecReaderIterator<'t, 'r>(&'t PubkeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for PubkeyVecReaderIterator<'t, 'r> { + type Item = Byte64Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for PubkeyVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SignatureVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SignatureVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SignatureVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SignatureVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for SignatureVec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + SignatureVec::new_unchecked(v.into()) + } +} +impl SignatureVec { + pub const ITEM_SIZE: usize = 64; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte64 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte64::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> SignatureVecReader<'r> { + SignatureVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SignatureVec { + type Builder = SignatureVecBuilder; + + const NAME: &'static str = "SignatureVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SignatureVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SignatureVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SignatureVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct SignatureVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SignatureVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SignatureVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SignatureVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> SignatureVecReader<'r> { + pub const ITEM_SIZE: usize = 64; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte64Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte64Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for SignatureVecReader<'r> { + type Entity = SignatureVec; + + const NAME: &'static str = "SignatureVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SignatureVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SignatureVecBuilder(pub(crate) Vec); +impl SignatureVecBuilder { + pub const ITEM_SIZE: usize = 64; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Byte64) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Byte64) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for SignatureVecBuilder { + type Entity = SignatureVec; + + const NAME: &'static str = "SignatureVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SignatureVec::new_unchecked(inner.into()) + } +} +pub struct SignatureVecIterator(SignatureVec, usize, usize); +impl ::core::iter::Iterator for SignatureVecIterator { + type Item = Byte64; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for SignatureVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for SignatureVec { + type IntoIter = SignatureVecIterator; + type Item = Byte64; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + SignatureVecIterator(self, 0, len) + } +} +impl<'r> SignatureVecReader<'r> { + pub fn iter<'t>(&'t self) -> SignatureVecReaderIterator<'t, 'r> { + SignatureVecReaderIterator(&self, 0, self.len()) + } +} +pub struct SignatureVecReaderIterator<'t, 'r>(&'t SignatureVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for SignatureVecReaderIterator<'t, 'r> { + type Item = Byte64Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SignatureVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct SocialUnlockEntries(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for SocialUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for SocialUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for SocialUnlockEntries { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "social_value", self.social_value())?; + write!(f, ", {}: {}", "social_proof", self.social_proof())?; + write!(f, ", {}: {}", "pubkeys", self.pubkeys())?; + write!(f, ", {}: {}", "signatures", self.signatures())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl ::core::default::Default for SocialUnlockEntries { + fn default() -> Self { + let v: Vec = vec![ + 59, 0, 0, 0, 20, 0, 0, 0, 47, 0, 0, 0, 51, 0, 0, 0, 55, 0, 0, 0, 27, 0, 0, 0, 20, 0, 0, + 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, + ]; + SocialUnlockEntries::new_unchecked(v.into()) + } +} +impl SocialUnlockEntries { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn social_value(&self) -> SocialValue { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialValue::new_unchecked(self.0.slice(start..end)) + } + + pub fn social_proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn pubkeys(&self) -> PubkeyVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + PubkeyVec::new_unchecked(self.0.slice(start..end)) + } + + pub fn signatures(&self) -> SignatureVec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + SignatureVec::new_unchecked(self.0.slice(start..end)) + } else { + SignatureVec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> SocialUnlockEntriesReader<'r> { + SocialUnlockEntriesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for SocialUnlockEntries { + type Builder = SocialUnlockEntriesBuilder; + + const NAME: &'static str = "SocialUnlockEntries"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + SocialUnlockEntries(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialUnlockEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + SocialUnlockEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .social_value(self.social_value()) + .social_proof(self.social_proof()) + .pubkeys(self.pubkeys()) + .signatures(self.signatures()) + } +} +#[derive(Clone, Copy)] +pub struct SocialUnlockEntriesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for SocialUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for SocialUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for SocialUnlockEntriesReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "social_value", self.social_value())?; + write!(f, ", {}: {}", "social_proof", self.social_proof())?; + write!(f, ", {}: {}", "pubkeys", self.pubkeys())?; + write!(f, ", {}: {}", "signatures", self.signatures())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } + write!(f, " }}") + } +} +impl<'r> SocialUnlockEntriesReader<'r> { + pub const FIELD_COUNT: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT + } + + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn social_value(&self) -> SocialValueReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + SocialValueReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn social_proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn pubkeys(&self) -> PubkeyVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + PubkeyVecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn signatures(&self) -> SignatureVecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[20..]) as usize; + SignatureVecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + SignatureVecReader::new_unchecked(&self.as_slice()[start..]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for SocialUnlockEntriesReader<'r> { + type Entity = SocialUnlockEntries; + + const NAME: &'static str = "SocialUnlockEntriesReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + SocialUnlockEntriesReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + SocialValueReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + PubkeyVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + SignatureVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct SocialUnlockEntriesBuilder { + pub(crate) social_value: SocialValue, + pub(crate) social_proof: Bytes, + pub(crate) pubkeys: PubkeyVec, + pub(crate) signatures: SignatureVec, +} +impl SocialUnlockEntriesBuilder { + pub const FIELD_COUNT: usize = 4; + + pub fn social_value(mut self, v: SocialValue) -> Self { + self.social_value = v; + self + } + + pub fn social_proof(mut self, v: Bytes) -> Self { + self.social_proof = v; + self + } + + pub fn pubkeys(mut self, v: PubkeyVec) -> Self { + self.pubkeys = v; + self + } + + pub fn signatures(mut self, v: SignatureVec) -> Self { + self.signatures = v; + self + } +} +impl molecule::prelude::Builder for SocialUnlockEntriesBuilder { + type Entity = SocialUnlockEntries; + + const NAME: &'static str = "SocialUnlockEntriesBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.social_value.as_slice().len() + + self.social_proof.as_slice().len() + + self.pubkeys.as_slice().len() + + self.signatures.as_slice().len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.social_value.as_slice().len(); + offsets.push(total_size); + total_size += self.social_proof.as_slice().len(); + offsets.push(total_size); + total_size += self.pubkeys.as_slice().len(); + offsets.push(total_size); + total_size += self.signatures.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + writer.write_all(self.social_value.as_slice())?; + writer.write_all(self.social_proof.as_slice())?; + writer.write_all(self.pubkeys.as_slice())?; + writer.write_all(self.signatures.as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + SocialUnlockEntries::new_unchecked(inner.into()) + } +} From 7f7b4b9267fde973021ba021465ab3f69c40f01f Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 09:54:11 +0800 Subject: [PATCH 09/19] chore: Update rust toolchain --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6e4731d..36eb67d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,7 +13,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: nightly-2022-08-01 + toolchain: nightly-2021-12-25 override: true - uses: docker-practice/actions-setup-docker@master From aeff634f66f46341dd3dc74849df1aa33ad2dc45 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 09:55:15 +0800 Subject: [PATCH 10/19] chore: Add joyid to lib --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 70b7206..688b5e1 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod smt; pub mod transfer; pub mod transfer_update; pub mod update; +pub mod joyid; cfg_if::cfg_if! { if #[cfg(feature = "std")] { From 80318f92452c0fe5efbe48b654951003e022a74c Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 09:57:22 +0800 Subject: [PATCH 11/19] Fix github ci error --- rust-toolchain | 2 +- src/joyid.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index 7e461e2..15a53c9 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2022-08-01 \ No newline at end of file +nightly-2021-12-25 \ No newline at end of file diff --git a/src/joyid.rs b/src/joyid.rs index 94d16da..d540672 100644 --- a/src/joyid.rs +++ b/src/joyid.rs @@ -1,5 +1,15 @@ // Generated by Molecule 0.7.3 +#![allow(unused_imports)] +#![allow(dead_code)] +#![allow(clippy::if_same_then_else)] + +use super::ckb_types::prelude::*; +use super::molecule::{self, prelude::*}; +extern crate alloc; +pub use alloc::vec::*; +// these lines above are manually added + use super::common::*; use molecule::prelude::*; #[derive(Clone)] From 9bde0f183b48204230f1eda33c05208bfc28a0b5 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 10:01:35 +0800 Subject: [PATCH 12/19] Format code --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 688b5e1..c24d86c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,13 +6,13 @@ pub mod common; pub mod define; pub mod extension; +pub mod joyid; pub mod mint; pub mod registry; pub mod smt; pub mod transfer; pub mod transfer_update; pub mod update; -pub mod joyid; cfg_if::cfg_if! { if #[cfg(feature = "std")] { From 176c2f9096ab692f99e6e0dd8216841396135a1f Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 12:16:13 +0800 Subject: [PATCH 13/19] chore: Update rust toolchain --- .github/workflows/rust.yml | 2 +- Cargo.lock | 4 ++-- Cargo.toml | 2 +- rust-toolchain | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 36eb67d..6e4731d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,7 +13,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: nightly-2021-12-25 + toolchain: nightly-2022-08-01 override: true - uses: docker-practice/actions-setup-docker@master diff --git a/Cargo.lock b/Cargo.lock index 296d780..7c6829d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,9 +191,9 @@ dependencies = [ [[package]] name = "ckb-std" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5761f11372e794d77b8e8a8059b22c5d6ccef907588dd5302a669f056ecce6b" +checksum = "47a6ad40455c446ad6fbb303dae24827fc309f43558f59d1f1b863a9de3e9f81" dependencies = [ "buddy-alloc", "cc", diff --git a/Cargo.toml b/Cargo.toml index 70b5faa..b952479 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] cfg-if = "1.0" blake2b-ref = "0.3.1" -ckb-std = {version = "0.9.0", optional = true} +ckb-std = {version = "0.10.0", optional = true} ckb-types = {version = "0.101.1", optional = true} molecule = {version = "0.7.2", default_features = false, optional = true} sparse-merkle-tree = {version = "0.5.3", default_features = false} diff --git a/rust-toolchain b/rust-toolchain index 15a53c9..7e461e2 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-12-25 \ No newline at end of file +nightly-2022-08-01 \ No newline at end of file From 924d294caa6deceaf89bafbd08183081afb6d162 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 21 Oct 2022 16:05:48 +0800 Subject: [PATCH 14/19] refactor: Add Byte type to common --- src/common.mol | 5 + src/common.rs | 3294 ++++++++++++++++++++++++++++++++++++++++++--- src/extension.mol | 2 - src/extension.rs | 273 ---- src/joyid.mol | 6 - src/joyid.rs | 2855 --------------------------------------- 6 files changed, 3079 insertions(+), 3356 deletions(-) diff --git a/src/common.mol b/src/common.mol index 30fe590..cd2cac0 100644 --- a/src/common.mol +++ b/src/common.mol @@ -4,7 +4,12 @@ array Uint16 [byte; 2]; array Uint32 [byte; 4]; array Uint64 [byte; 8]; +array Byte6 [byte; 6]; +array Byte9 [byte; 9]; +array Byte20 [byte; 20]; +array Byte24 [byte; 24]; array Byte32 [byte; 32]; +array Byte64 [byte; 64]; array CotaId [byte; 20]; array Characteristic [byte; 20]; diff --git a/src/common.rs b/src/common.rs index 631fe2e..d2fea8b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,5 +1,4 @@ // Generated by Molecule 0.7.3 - #![allow(unused_imports)] #![allow(dead_code)] #![allow(clippy::if_same_then_else)] @@ -10,6 +9,7 @@ extern crate alloc; pub use alloc::vec::*; // these lines above are manually added +use molecule::prelude::*; #[derive(Clone)] pub struct Uint16(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Uint16 { @@ -752,8 +752,8 @@ impl molecule::prelude::Builder for Uint64Builder { } } #[derive(Clone)] -pub struct Byte32(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte32 { +pub struct Byte6(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte6 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -762,31 +762,28 @@ impl ::core::fmt::LowerHex for Byte32 { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Byte32 { +impl ::core::fmt::Debug for Byte6 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Byte32 { +impl ::core::fmt::Display for Byte6 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; let raw_data = hex_string(&self.raw_data()); write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl ::core::default::Default for Byte32 { +impl ::core::default::Default for Byte6 { fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - Byte32::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0, 0, 0]; + Byte6::new_unchecked(v.into()) } } -impl Byte32 { - pub const ITEM_COUNT: usize = 32; +impl Byte6 { + pub const ITEM_COUNT: usize = 6; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 6; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -812,125 +809,21 @@ impl Byte32 { Byte::new_unchecked(self.0.slice(5..6)) } - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn nth24(&self) -> Byte { - Byte::new_unchecked(self.0.slice(24..25)) - } - - pub fn nth25(&self) -> Byte { - Byte::new_unchecked(self.0.slice(25..26)) - } - - pub fn nth26(&self) -> Byte { - Byte::new_unchecked(self.0.slice(26..27)) - } - - pub fn nth27(&self) -> Byte { - Byte::new_unchecked(self.0.slice(27..28)) - } - - pub fn nth28(&self) -> Byte { - Byte::new_unchecked(self.0.slice(28..29)) - } - - pub fn nth29(&self) -> Byte { - Byte::new_unchecked(self.0.slice(29..30)) - } - - pub fn nth30(&self) -> Byte { - Byte::new_unchecked(self.0.slice(30..31)) - } - - pub fn nth31(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) - } - pub fn raw_data(&self) -> molecule::bytes::Bytes { self.as_bytes() } - pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> { - Byte32Reader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> Byte6Reader<'r> { + Byte6Reader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Byte32 { - type Builder = Byte32Builder; +impl molecule::prelude::Entity for Byte6 { + type Builder = Byte6Builder; - const NAME: &'static str = "Byte32"; + const NAME: &'static str = "Byte6"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte32(data) + Byte6(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -942,11 +835,11 @@ impl molecule::prelude::Entity for Byte32 { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32Reader::from_slice(slice).map(|reader| reader.to_entity()) + Byte6Reader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + Byte6Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -961,38 +854,12 @@ impl molecule::prelude::Entity for Byte32 { self.nth3(), self.nth4(), self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - self.nth24(), - self.nth25(), - self.nth26(), - self.nth27(), - self.nth28(), - self.nth29(), - self.nth30(), - self.nth31(), ]) } } #[derive(Clone, Copy)] -pub struct Byte32Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> { +pub struct Byte6Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte6Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1001,22 +868,22 @@ impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for Byte32Reader<'r> { +impl<'r> ::core::fmt::Debug for Byte6Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for Byte32Reader<'r> { +impl<'r> ::core::fmt::Display for Byte6Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; let raw_data = hex_string(&self.raw_data()); write!(f, "{}(0x{})", Self::NAME, raw_data) } } -impl<'r> Byte32Reader<'r> { - pub const ITEM_COUNT: usize = 32; +impl<'r> Byte6Reader<'r> { + pub const ITEM_COUNT: usize = 6; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 6; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -1042,81 +909,2716 @@ impl<'r> Byte32Reader<'r> { ByteReader::new_unchecked(&self.as_slice()[5..6]) } - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() } +} +impl<'r> molecule::prelude::Reader<'r> for Byte6Reader<'r> { + type Entity = Byte6; - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } + const NAME: &'static str = "Byte6Reader"; - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte6Reader(slice) } - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) +} +pub struct Byte6Builder(pub(crate) [Byte; 6]); +impl ::core::fmt::Debug for Byte6Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) +} +impl ::core::default::Default for Byte6Builder { + fn default() -> Self { + Byte6Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) } +} +impl Byte6Builder { + pub const ITEM_COUNT: usize = 6; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 6; - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) + pub fn set(mut self, v: [Byte; 6]) -> Self { + self.0 = v; + self } - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self } - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self } - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self } - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self } - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self } - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self } +} +impl molecule::prelude::Builder for Byte6Builder { + type Entity = Byte6; - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } + const NAME: &'static str = "Byte6Builder"; - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE } - pub fn nth24(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[24..25]) - } + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte6::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte9(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte9 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte9 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte9::new_unchecked(v.into()) + } +} +impl Byte9 { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { + Byte9Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte9 { + type Builder = Byte9Builder; + + const NAME: &'static str = "Byte9"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte9(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte9Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte9Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte9Reader<'r> { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte9Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte9Builder(pub(crate) [Byte; 9]); +impl ::core::fmt::Debug for Byte9Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte9Builder { + fn default() -> Self { + Byte9Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte9Builder { + pub const ITEM_COUNT: usize = 9; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 9; + + pub fn set(mut self, v: [Byte; 9]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } +} +impl molecule::prelude::Builder for Byte9Builder { + type Entity = Byte9; + + const NAME: &'static str = "Byte9Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte9::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte20(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte20 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte20 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + Byte20::new_unchecked(v.into()) + } +} +impl Byte20 { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { + Byte20Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte20 { + type Builder = Byte20Builder; + + const NAME: &'static str = "Byte20"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte20(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte20Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte20Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte20Reader<'r> { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte20Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte20Builder(pub(crate) [Byte; 20]); +impl ::core::fmt::Debug for Byte20Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte20Builder { + fn default() -> Self { + Byte20Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte20Builder { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; + + pub fn set(mut self, v: [Byte; 20]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } +} +impl molecule::prelude::Builder for Byte20Builder { + type Entity = Byte20; + + const NAME: &'static str = "Byte20Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte20::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte24(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte24 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte24 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + Byte24::new_unchecked(v.into()) + } +} +impl Byte24 { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { + Byte24Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte24 { + type Builder = Byte24Builder; + + const NAME: &'static str = "Byte24"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte24(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte24Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte24Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte24Reader<'r> { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte24Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte24Builder(pub(crate) [Byte; 24]); +impl ::core::fmt::Debug for Byte24Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte24Builder { + fn default() -> Self { + Byte24Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte24Builder { + pub const ITEM_COUNT: usize = 24; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 24; + + pub fn set(mut self, v: [Byte; 24]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } +} +impl molecule::prelude::Builder for Byte24Builder { + type Entity = Byte24; + + const NAME: &'static str = "Byte24Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte24::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte32(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte32 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + Byte32::new_unchecked(v.into()) + } +} +impl Byte32 { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> { + Byte32Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte32 { + type Builder = Byte32Builder; + + const NAME: &'static str = "Byte32"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte32(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte32Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte32Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte32Reader<'r> { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> { + type Entity = Byte32; + + const NAME: &'static str = "Byte32Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte32Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Byte32Builder(pub(crate) [Byte; 32]); +impl ::core::fmt::Debug for Byte32Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Byte32Builder { + fn default() -> Self { + Byte32Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Byte32Builder { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn set(mut self, v: [Byte; 32]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self + } + + pub fn nth26(mut self, v: Byte) -> Self { + self.0[26] = v; + self + } + + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } +} +impl molecule::prelude::Builder for Byte32Builder { + type Entity = Byte32; + + const NAME: &'static str = "Byte32Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte32::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Byte64(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Byte64 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; + Byte64::new_unchecked(v.into()) + } +} +impl Byte64 { + pub const ITEM_COUNT: usize = 64; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 64; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn nth32(&self) -> Byte { + Byte::new_unchecked(self.0.slice(32..33)) + } + + pub fn nth33(&self) -> Byte { + Byte::new_unchecked(self.0.slice(33..34)) + } + + pub fn nth34(&self) -> Byte { + Byte::new_unchecked(self.0.slice(34..35)) + } + + pub fn nth35(&self) -> Byte { + Byte::new_unchecked(self.0.slice(35..36)) + } + + pub fn nth36(&self) -> Byte { + Byte::new_unchecked(self.0.slice(36..37)) + } + + pub fn nth37(&self) -> Byte { + Byte::new_unchecked(self.0.slice(37..38)) + } + + pub fn nth38(&self) -> Byte { + Byte::new_unchecked(self.0.slice(38..39)) + } + + pub fn nth39(&self) -> Byte { + Byte::new_unchecked(self.0.slice(39..40)) + } + + pub fn nth40(&self) -> Byte { + Byte::new_unchecked(self.0.slice(40..41)) + } + + pub fn nth41(&self) -> Byte { + Byte::new_unchecked(self.0.slice(41..42)) + } + + pub fn nth42(&self) -> Byte { + Byte::new_unchecked(self.0.slice(42..43)) + } + + pub fn nth43(&self) -> Byte { + Byte::new_unchecked(self.0.slice(43..44)) + } + + pub fn nth44(&self) -> Byte { + Byte::new_unchecked(self.0.slice(44..45)) + } + + pub fn nth45(&self) -> Byte { + Byte::new_unchecked(self.0.slice(45..46)) + } + + pub fn nth46(&self) -> Byte { + Byte::new_unchecked(self.0.slice(46..47)) + } + + pub fn nth47(&self) -> Byte { + Byte::new_unchecked(self.0.slice(47..48)) + } + + pub fn nth48(&self) -> Byte { + Byte::new_unchecked(self.0.slice(48..49)) + } + + pub fn nth49(&self) -> Byte { + Byte::new_unchecked(self.0.slice(49..50)) + } + + pub fn nth50(&self) -> Byte { + Byte::new_unchecked(self.0.slice(50..51)) + } + + pub fn nth51(&self) -> Byte { + Byte::new_unchecked(self.0.slice(51..52)) + } + + pub fn nth52(&self) -> Byte { + Byte::new_unchecked(self.0.slice(52..53)) + } + + pub fn nth53(&self) -> Byte { + Byte::new_unchecked(self.0.slice(53..54)) + } + + pub fn nth54(&self) -> Byte { + Byte::new_unchecked(self.0.slice(54..55)) + } + + pub fn nth55(&self) -> Byte { + Byte::new_unchecked(self.0.slice(55..56)) + } + + pub fn nth56(&self) -> Byte { + Byte::new_unchecked(self.0.slice(56..57)) + } + + pub fn nth57(&self) -> Byte { + Byte::new_unchecked(self.0.slice(57..58)) + } + + pub fn nth58(&self) -> Byte { + Byte::new_unchecked(self.0.slice(58..59)) + } + + pub fn nth59(&self) -> Byte { + Byte::new_unchecked(self.0.slice(59..60)) + } + + pub fn nth60(&self) -> Byte { + Byte::new_unchecked(self.0.slice(60..61)) + } + + pub fn nth61(&self) -> Byte { + Byte::new_unchecked(self.0.slice(61..62)) + } + + pub fn nth62(&self) -> Byte { + Byte::new_unchecked(self.0.slice(62..63)) + } + + pub fn nth63(&self) -> Byte { + Byte::new_unchecked(self.0.slice(63..64)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Byte64Reader<'r> { + Byte64Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte64 { + type Builder = Byte64Builder; + + const NAME: &'static str = "Byte64"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte64(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte64Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + self.nth32(), + self.nth33(), + self.nth34(), + self.nth35(), + self.nth36(), + self.nth37(), + self.nth38(), + self.nth39(), + self.nth40(), + self.nth41(), + self.nth42(), + self.nth43(), + self.nth44(), + self.nth45(), + self.nth46(), + self.nth47(), + self.nth48(), + self.nth49(), + self.nth50(), + self.nth51(), + self.nth52(), + self.nth53(), + self.nth54(), + self.nth55(), + self.nth56(), + self.nth57(), + self.nth58(), + self.nth59(), + self.nth60(), + self.nth61(), + self.nth62(), + self.nth63(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Byte64Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Byte64Reader<'r> { + pub const ITEM_COUNT: usize = 64; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 64; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } pub fn nth25(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[25..26]) @@ -1146,21 +3648,149 @@ impl<'r> Byte32Reader<'r> { ByteReader::new_unchecked(&self.as_slice()[31..32]) } + pub fn nth32(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[32..33]) + } + + pub fn nth33(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[33..34]) + } + + pub fn nth34(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[34..35]) + } + + pub fn nth35(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[35..36]) + } + + pub fn nth36(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[36..37]) + } + + pub fn nth37(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[37..38]) + } + + pub fn nth38(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[38..39]) + } + + pub fn nth39(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[39..40]) + } + + pub fn nth40(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[40..41]) + } + + pub fn nth41(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[41..42]) + } + + pub fn nth42(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[42..43]) + } + + pub fn nth43(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[43..44]) + } + + pub fn nth44(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[44..45]) + } + + pub fn nth45(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[45..46]) + } + + pub fn nth46(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[46..47]) + } + + pub fn nth47(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[47..48]) + } + + pub fn nth48(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[48..49]) + } + + pub fn nth49(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[49..50]) + } + + pub fn nth50(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[50..51]) + } + + pub fn nth51(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[51..52]) + } + + pub fn nth52(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[52..53]) + } + + pub fn nth53(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[53..54]) + } + + pub fn nth54(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[54..55]) + } + + pub fn nth55(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[55..56]) + } + + pub fn nth56(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[56..57]) + } + + pub fn nth57(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[57..58]) + } + + pub fn nth58(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[58..59]) + } + + pub fn nth59(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[59..60]) + } + + pub fn nth60(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[60..61]) + } + + pub fn nth61(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[61..62]) + } + + pub fn nth62(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[62..63]) + } + + pub fn nth63(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[63..64]) + } + pub fn raw_data(&self) -> &'r [u8] { self.as_slice() } } -impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> { - type Entity = Byte32; +impl<'r> molecule::prelude::Reader<'r> for Byte64Reader<'r> { + type Entity = Byte64; - const NAME: &'static str = "Byte32Reader"; + const NAME: &'static str = "Byte64Reader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - Byte32Reader(slice) + Byte64Reader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -1176,15 +3806,47 @@ impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> { Ok(()) } } -pub struct Byte32Builder(pub(crate) [Byte; 32]); -impl ::core::fmt::Debug for Byte32Builder { +pub struct Byte64Builder(pub(crate) [Byte; 64]); +impl ::core::fmt::Debug for Byte64Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:?})", Self::NAME, &self.0[..]) } } -impl ::core::default::Default for Byte32Builder { +impl ::core::default::Default for Byte64Builder { fn default() -> Self { - Byte32Builder([ + Byte64Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), Byte::default(), Byte::default(), Byte::default(), @@ -1220,12 +3882,12 @@ impl ::core::default::Default for Byte32Builder { ]) } } -impl Byte32Builder { - pub const ITEM_COUNT: usize = 32; +impl Byte64Builder { + pub const ITEM_COUNT: usize = 64; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 64; - pub fn set(mut self, v: [Byte; 32]) -> Self { + pub fn set(mut self, v: [Byte; 64]) -> Self { self.0 = v; self } @@ -1389,11 +4051,171 @@ impl Byte32Builder { self.0[31] = v; self } + + pub fn nth32(mut self, v: Byte) -> Self { + self.0[32] = v; + self + } + + pub fn nth33(mut self, v: Byte) -> Self { + self.0[33] = v; + self + } + + pub fn nth34(mut self, v: Byte) -> Self { + self.0[34] = v; + self + } + + pub fn nth35(mut self, v: Byte) -> Self { + self.0[35] = v; + self + } + + pub fn nth36(mut self, v: Byte) -> Self { + self.0[36] = v; + self + } + + pub fn nth37(mut self, v: Byte) -> Self { + self.0[37] = v; + self + } + + pub fn nth38(mut self, v: Byte) -> Self { + self.0[38] = v; + self + } + + pub fn nth39(mut self, v: Byte) -> Self { + self.0[39] = v; + self + } + + pub fn nth40(mut self, v: Byte) -> Self { + self.0[40] = v; + self + } + + pub fn nth41(mut self, v: Byte) -> Self { + self.0[41] = v; + self + } + + pub fn nth42(mut self, v: Byte) -> Self { + self.0[42] = v; + self + } + + pub fn nth43(mut self, v: Byte) -> Self { + self.0[43] = v; + self + } + + pub fn nth44(mut self, v: Byte) -> Self { + self.0[44] = v; + self + } + + pub fn nth45(mut self, v: Byte) -> Self { + self.0[45] = v; + self + } + + pub fn nth46(mut self, v: Byte) -> Self { + self.0[46] = v; + self + } + + pub fn nth47(mut self, v: Byte) -> Self { + self.0[47] = v; + self + } + + pub fn nth48(mut self, v: Byte) -> Self { + self.0[48] = v; + self + } + + pub fn nth49(mut self, v: Byte) -> Self { + self.0[49] = v; + self + } + + pub fn nth50(mut self, v: Byte) -> Self { + self.0[50] = v; + self + } + + pub fn nth51(mut self, v: Byte) -> Self { + self.0[51] = v; + self + } + + pub fn nth52(mut self, v: Byte) -> Self { + self.0[52] = v; + self + } + + pub fn nth53(mut self, v: Byte) -> Self { + self.0[53] = v; + self + } + + pub fn nth54(mut self, v: Byte) -> Self { + self.0[54] = v; + self + } + + pub fn nth55(mut self, v: Byte) -> Self { + self.0[55] = v; + self + } + + pub fn nth56(mut self, v: Byte) -> Self { + self.0[56] = v; + self + } + + pub fn nth57(mut self, v: Byte) -> Self { + self.0[57] = v; + self + } + + pub fn nth58(mut self, v: Byte) -> Self { + self.0[58] = v; + self + } + + pub fn nth59(mut self, v: Byte) -> Self { + self.0[59] = v; + self + } + + pub fn nth60(mut self, v: Byte) -> Self { + self.0[60] = v; + self + } + + pub fn nth61(mut self, v: Byte) -> Self { + self.0[61] = v; + self + } + + pub fn nth62(mut self, v: Byte) -> Self { + self.0[62] = v; + self + } + + pub fn nth63(mut self, v: Byte) -> Self { + self.0[63] = v; + self + } } -impl molecule::prelude::Builder for Byte32Builder { - type Entity = Byte32; +impl molecule::prelude::Builder for Byte64Builder { + type Entity = Byte64; - const NAME: &'static str = "Byte32Builder"; + const NAME: &'static str = "Byte64Builder"; fn expected_length(&self) -> usize { Self::TOTAL_SIZE @@ -1432,6 +4254,38 @@ impl molecule::prelude::Builder for Byte32Builder { writer.write_all(self.0[29].as_slice())?; writer.write_all(self.0[30].as_slice())?; writer.write_all(self.0[31].as_slice())?; + writer.write_all(self.0[32].as_slice())?; + writer.write_all(self.0[33].as_slice())?; + writer.write_all(self.0[34].as_slice())?; + writer.write_all(self.0[35].as_slice())?; + writer.write_all(self.0[36].as_slice())?; + writer.write_all(self.0[37].as_slice())?; + writer.write_all(self.0[38].as_slice())?; + writer.write_all(self.0[39].as_slice())?; + writer.write_all(self.0[40].as_slice())?; + writer.write_all(self.0[41].as_slice())?; + writer.write_all(self.0[42].as_slice())?; + writer.write_all(self.0[43].as_slice())?; + writer.write_all(self.0[44].as_slice())?; + writer.write_all(self.0[45].as_slice())?; + writer.write_all(self.0[46].as_slice())?; + writer.write_all(self.0[47].as_slice())?; + writer.write_all(self.0[48].as_slice())?; + writer.write_all(self.0[49].as_slice())?; + writer.write_all(self.0[50].as_slice())?; + writer.write_all(self.0[51].as_slice())?; + writer.write_all(self.0[52].as_slice())?; + writer.write_all(self.0[53].as_slice())?; + writer.write_all(self.0[54].as_slice())?; + writer.write_all(self.0[55].as_slice())?; + writer.write_all(self.0[56].as_slice())?; + writer.write_all(self.0[57].as_slice())?; + writer.write_all(self.0[58].as_slice())?; + writer.write_all(self.0[59].as_slice())?; + writer.write_all(self.0[60].as_slice())?; + writer.write_all(self.0[61].as_slice())?; + writer.write_all(self.0[62].as_slice())?; + writer.write_all(self.0[63].as_slice())?; Ok(()) } @@ -1439,7 +4293,7 @@ impl molecule::prelude::Builder for Byte32Builder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte32::new_unchecked(inner.into()) + Byte64::new_unchecked(inner.into()) } } #[derive(Clone)] diff --git a/src/extension.mol b/src/extension.mol index 0629c26..e3deec3 100644 --- a/src/extension.mol +++ b/src/extension.mol @@ -1,7 +1,5 @@ import common; -array Byte6 [byte; 6]; - vector LockScriptVec ; vector ExtensionVec ; diff --git a/src/extension.rs b/src/extension.rs index 1f8177d..b41da32 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -1,5 +1,4 @@ // Generated by Molecule 0.7.3 - #![allow(unused_imports)] #![allow(dead_code)] #![allow(clippy::if_same_then_else)] @@ -13,278 +12,6 @@ pub use alloc::vec::*; use super::common::*; use molecule::prelude::*; #[derive(Clone)] -pub struct Byte6(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte6 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0]; - Byte6::new_unchecked(v.into()) - } -} -impl Byte6 { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte6 { - type Builder = Byte6Builder; - - const NAME: &'static str = "Byte6"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte6(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte6Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte6Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte6Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte6Reader<'r> { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte6Reader<'r> { - type Entity = Byte6; - - const NAME: &'static str = "Byte6Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte6Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte6Builder(pub(crate) [Byte; 6]); -impl ::core::fmt::Debug for Byte6Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte6Builder { - fn default() -> Self { - Byte6Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte6Builder { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn set(mut self, v: [Byte; 6]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } -} -impl molecule::prelude::Builder for Byte6Builder { - type Entity = Byte6; - - const NAME: &'static str = "Byte6Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte6::new_unchecked(inner.into()) - } -} -#[derive(Clone)] pub struct LockScriptVec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for LockScriptVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { diff --git a/src/joyid.mol b/src/joyid.mol index 1f111b5..b4a6c84 100644 --- a/src/joyid.mol +++ b/src/joyid.mol @@ -1,13 +1,7 @@ import common; -array Byte6 [byte; 6]; -array Byte9 [byte; 9]; -array Byte20 [byte; 20]; -array Byte24 [byte; 24]; -array Byte64 [byte; 64]; vector LockScriptVec ; - struct SubKey { smt_type: Uint16, sub_type: Byte6, diff --git a/src/joyid.rs b/src/joyid.rs index d540672..ce9fa41 100644 --- a/src/joyid.rs +++ b/src/joyid.rs @@ -1,5 +1,4 @@ // Generated by Molecule 0.7.3 - #![allow(unused_imports)] #![allow(dead_code)] #![allow(clippy::if_same_then_else)] @@ -13,2860 +12,6 @@ pub use alloc::vec::*; use super::common::*; use molecule::prelude::*; #[derive(Clone)] -pub struct Byte6(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte6 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte6 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0]; - Byte6::new_unchecked(v.into()) - } -} -impl Byte6 { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte6 { - type Builder = Byte6Builder; - - const NAME: &'static str = "Byte6"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte6(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte6Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte6Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte6Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte6Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte6Reader<'r> { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte6Reader<'r> { - type Entity = Byte6; - - const NAME: &'static str = "Byte6Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte6Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte6Builder(pub(crate) [Byte; 6]); -impl ::core::fmt::Debug for Byte6Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte6Builder { - fn default() -> Self { - Byte6Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte6Builder { - pub const ITEM_COUNT: usize = 6; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 6; - - pub fn set(mut self, v: [Byte; 6]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } -} -impl molecule::prelude::Builder for Byte6Builder { - type Entity = Byte6; - - const NAME: &'static str = "Byte6Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte6::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte9(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte9 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte9::new_unchecked(v.into()) - } -} -impl Byte9 { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { - Byte9Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte9 { - type Builder = Byte9Builder; - - const NAME: &'static str = "Byte9"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte9(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte9Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte9Reader<'r> { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte9Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte9Builder(pub(crate) [Byte; 9]); -impl ::core::fmt::Debug for Byte9Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte9Builder { - fn default() -> Self { - Byte9Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte9Builder { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn set(mut self, v: [Byte; 9]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } -} -impl molecule::prelude::Builder for Byte9Builder { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte9::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte20(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte20 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte20::new_unchecked(v.into()) - } -} -impl Byte20 { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte20 { - type Builder = Byte20Builder; - - const NAME: &'static str = "Byte20"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte20(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte20Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte20Reader<'r> { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte20Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte20Builder(pub(crate) [Byte; 20]); -impl ::core::fmt::Debug for Byte20Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte20Builder { - fn default() -> Self { - Byte20Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte20Builder { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn set(mut self, v: [Byte; 20]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } -} -impl molecule::prelude::Builder for Byte20Builder { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte20::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte24(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte24 { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Byte24::new_unchecked(v.into()) - } -} -impl Byte24 { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { - Byte24Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte24 { - type Builder = Byte24Builder; - - const NAME: &'static str = "Byte24"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte24(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte24Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte24Reader<'r> { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte24Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte24Builder(pub(crate) [Byte; 24]); -impl ::core::fmt::Debug for Byte24Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte24Builder { - fn default() -> Self { - Byte24Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte24Builder { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn set(mut self, v: [Byte; 24]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } - - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self - } - - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self - } - - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self - } -} -impl molecule::prelude::Builder for Byte24Builder { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte24::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte64(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte64 { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - ]; - Byte64::new_unchecked(v.into()) - } -} -impl Byte64 { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn nth24(&self) -> Byte { - Byte::new_unchecked(self.0.slice(24..25)) - } - - pub fn nth25(&self) -> Byte { - Byte::new_unchecked(self.0.slice(25..26)) - } - - pub fn nth26(&self) -> Byte { - Byte::new_unchecked(self.0.slice(26..27)) - } - - pub fn nth27(&self) -> Byte { - Byte::new_unchecked(self.0.slice(27..28)) - } - - pub fn nth28(&self) -> Byte { - Byte::new_unchecked(self.0.slice(28..29)) - } - - pub fn nth29(&self) -> Byte { - Byte::new_unchecked(self.0.slice(29..30)) - } - - pub fn nth30(&self) -> Byte { - Byte::new_unchecked(self.0.slice(30..31)) - } - - pub fn nth31(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) - } - - pub fn nth32(&self) -> Byte { - Byte::new_unchecked(self.0.slice(32..33)) - } - - pub fn nth33(&self) -> Byte { - Byte::new_unchecked(self.0.slice(33..34)) - } - - pub fn nth34(&self) -> Byte { - Byte::new_unchecked(self.0.slice(34..35)) - } - - pub fn nth35(&self) -> Byte { - Byte::new_unchecked(self.0.slice(35..36)) - } - - pub fn nth36(&self) -> Byte { - Byte::new_unchecked(self.0.slice(36..37)) - } - - pub fn nth37(&self) -> Byte { - Byte::new_unchecked(self.0.slice(37..38)) - } - - pub fn nth38(&self) -> Byte { - Byte::new_unchecked(self.0.slice(38..39)) - } - - pub fn nth39(&self) -> Byte { - Byte::new_unchecked(self.0.slice(39..40)) - } - - pub fn nth40(&self) -> Byte { - Byte::new_unchecked(self.0.slice(40..41)) - } - - pub fn nth41(&self) -> Byte { - Byte::new_unchecked(self.0.slice(41..42)) - } - - pub fn nth42(&self) -> Byte { - Byte::new_unchecked(self.0.slice(42..43)) - } - - pub fn nth43(&self) -> Byte { - Byte::new_unchecked(self.0.slice(43..44)) - } - - pub fn nth44(&self) -> Byte { - Byte::new_unchecked(self.0.slice(44..45)) - } - - pub fn nth45(&self) -> Byte { - Byte::new_unchecked(self.0.slice(45..46)) - } - - pub fn nth46(&self) -> Byte { - Byte::new_unchecked(self.0.slice(46..47)) - } - - pub fn nth47(&self) -> Byte { - Byte::new_unchecked(self.0.slice(47..48)) - } - - pub fn nth48(&self) -> Byte { - Byte::new_unchecked(self.0.slice(48..49)) - } - - pub fn nth49(&self) -> Byte { - Byte::new_unchecked(self.0.slice(49..50)) - } - - pub fn nth50(&self) -> Byte { - Byte::new_unchecked(self.0.slice(50..51)) - } - - pub fn nth51(&self) -> Byte { - Byte::new_unchecked(self.0.slice(51..52)) - } - - pub fn nth52(&self) -> Byte { - Byte::new_unchecked(self.0.slice(52..53)) - } - - pub fn nth53(&self) -> Byte { - Byte::new_unchecked(self.0.slice(53..54)) - } - - pub fn nth54(&self) -> Byte { - Byte::new_unchecked(self.0.slice(54..55)) - } - - pub fn nth55(&self) -> Byte { - Byte::new_unchecked(self.0.slice(55..56)) - } - - pub fn nth56(&self) -> Byte { - Byte::new_unchecked(self.0.slice(56..57)) - } - - pub fn nth57(&self) -> Byte { - Byte::new_unchecked(self.0.slice(57..58)) - } - - pub fn nth58(&self) -> Byte { - Byte::new_unchecked(self.0.slice(58..59)) - } - - pub fn nth59(&self) -> Byte { - Byte::new_unchecked(self.0.slice(59..60)) - } - - pub fn nth60(&self) -> Byte { - Byte::new_unchecked(self.0.slice(60..61)) - } - - pub fn nth61(&self) -> Byte { - Byte::new_unchecked(self.0.slice(61..62)) - } - - pub fn nth62(&self) -> Byte { - Byte::new_unchecked(self.0.slice(62..63)) - } - - pub fn nth63(&self) -> Byte { - Byte::new_unchecked(self.0.slice(63..64)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte64Reader<'r> { - Byte64Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte64 { - type Builder = Byte64Builder; - - const NAME: &'static str = "Byte64"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte64(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte64Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - self.nth24(), - self.nth25(), - self.nth26(), - self.nth27(), - self.nth28(), - self.nth29(), - self.nth30(), - self.nth31(), - self.nth32(), - self.nth33(), - self.nth34(), - self.nth35(), - self.nth36(), - self.nth37(), - self.nth38(), - self.nth39(), - self.nth40(), - self.nth41(), - self.nth42(), - self.nth43(), - self.nth44(), - self.nth45(), - self.nth46(), - self.nth47(), - self.nth48(), - self.nth49(), - self.nth50(), - self.nth51(), - self.nth52(), - self.nth53(), - self.nth54(), - self.nth55(), - self.nth56(), - self.nth57(), - self.nth58(), - self.nth59(), - self.nth60(), - self.nth61(), - self.nth62(), - self.nth63(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte64Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte64Reader<'r> { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn nth24(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[24..25]) - } - - pub fn nth25(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[25..26]) - } - - pub fn nth26(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[26..27]) - } - - pub fn nth27(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[27..28]) - } - - pub fn nth28(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[28..29]) - } - - pub fn nth29(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[29..30]) - } - - pub fn nth30(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[30..31]) - } - - pub fn nth31(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[31..32]) - } - - pub fn nth32(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[32..33]) - } - - pub fn nth33(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[33..34]) - } - - pub fn nth34(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[34..35]) - } - - pub fn nth35(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[35..36]) - } - - pub fn nth36(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[36..37]) - } - - pub fn nth37(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[37..38]) - } - - pub fn nth38(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[38..39]) - } - - pub fn nth39(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[39..40]) - } - - pub fn nth40(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[40..41]) - } - - pub fn nth41(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[41..42]) - } - - pub fn nth42(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[42..43]) - } - - pub fn nth43(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[43..44]) - } - - pub fn nth44(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[44..45]) - } - - pub fn nth45(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[45..46]) - } - - pub fn nth46(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[46..47]) - } - - pub fn nth47(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[47..48]) - } - - pub fn nth48(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[48..49]) - } - - pub fn nth49(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[49..50]) - } - - pub fn nth50(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[50..51]) - } - - pub fn nth51(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[51..52]) - } - - pub fn nth52(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[52..53]) - } - - pub fn nth53(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[53..54]) - } - - pub fn nth54(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[54..55]) - } - - pub fn nth55(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[55..56]) - } - - pub fn nth56(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[56..57]) - } - - pub fn nth57(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[57..58]) - } - - pub fn nth58(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[58..59]) - } - - pub fn nth59(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[59..60]) - } - - pub fn nth60(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[60..61]) - } - - pub fn nth61(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[61..62]) - } - - pub fn nth62(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[62..63]) - } - - pub fn nth63(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[63..64]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte64Reader<'r> { - type Entity = Byte64; - - const NAME: &'static str = "Byte64Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte64Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte64Builder(pub(crate) [Byte; 64]); -impl ::core::fmt::Debug for Byte64Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte64Builder { - fn default() -> Self { - Byte64Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte64Builder { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn set(mut self, v: [Byte; 64]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } - - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self - } - - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self - } - - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self - } - - pub fn nth24(mut self, v: Byte) -> Self { - self.0[24] = v; - self - } - - pub fn nth25(mut self, v: Byte) -> Self { - self.0[25] = v; - self - } - - pub fn nth26(mut self, v: Byte) -> Self { - self.0[26] = v; - self - } - - pub fn nth27(mut self, v: Byte) -> Self { - self.0[27] = v; - self - } - - pub fn nth28(mut self, v: Byte) -> Self { - self.0[28] = v; - self - } - - pub fn nth29(mut self, v: Byte) -> Self { - self.0[29] = v; - self - } - - pub fn nth30(mut self, v: Byte) -> Self { - self.0[30] = v; - self - } - - pub fn nth31(mut self, v: Byte) -> Self { - self.0[31] = v; - self - } - - pub fn nth32(mut self, v: Byte) -> Self { - self.0[32] = v; - self - } - - pub fn nth33(mut self, v: Byte) -> Self { - self.0[33] = v; - self - } - - pub fn nth34(mut self, v: Byte) -> Self { - self.0[34] = v; - self - } - - pub fn nth35(mut self, v: Byte) -> Self { - self.0[35] = v; - self - } - - pub fn nth36(mut self, v: Byte) -> Self { - self.0[36] = v; - self - } - - pub fn nth37(mut self, v: Byte) -> Self { - self.0[37] = v; - self - } - - pub fn nth38(mut self, v: Byte) -> Self { - self.0[38] = v; - self - } - - pub fn nth39(mut self, v: Byte) -> Self { - self.0[39] = v; - self - } - - pub fn nth40(mut self, v: Byte) -> Self { - self.0[40] = v; - self - } - - pub fn nth41(mut self, v: Byte) -> Self { - self.0[41] = v; - self - } - - pub fn nth42(mut self, v: Byte) -> Self { - self.0[42] = v; - self - } - - pub fn nth43(mut self, v: Byte) -> Self { - self.0[43] = v; - self - } - - pub fn nth44(mut self, v: Byte) -> Self { - self.0[44] = v; - self - } - - pub fn nth45(mut self, v: Byte) -> Self { - self.0[45] = v; - self - } - - pub fn nth46(mut self, v: Byte) -> Self { - self.0[46] = v; - self - } - - pub fn nth47(mut self, v: Byte) -> Self { - self.0[47] = v; - self - } - - pub fn nth48(mut self, v: Byte) -> Self { - self.0[48] = v; - self - } - - pub fn nth49(mut self, v: Byte) -> Self { - self.0[49] = v; - self - } - - pub fn nth50(mut self, v: Byte) -> Self { - self.0[50] = v; - self - } - - pub fn nth51(mut self, v: Byte) -> Self { - self.0[51] = v; - self - } - - pub fn nth52(mut self, v: Byte) -> Self { - self.0[52] = v; - self - } - - pub fn nth53(mut self, v: Byte) -> Self { - self.0[53] = v; - self - } - - pub fn nth54(mut self, v: Byte) -> Self { - self.0[54] = v; - self - } - - pub fn nth55(mut self, v: Byte) -> Self { - self.0[55] = v; - self - } - - pub fn nth56(mut self, v: Byte) -> Self { - self.0[56] = v; - self - } - - pub fn nth57(mut self, v: Byte) -> Self { - self.0[57] = v; - self - } - - pub fn nth58(mut self, v: Byte) -> Self { - self.0[58] = v; - self - } - - pub fn nth59(mut self, v: Byte) -> Self { - self.0[59] = v; - self - } - - pub fn nth60(mut self, v: Byte) -> Self { - self.0[60] = v; - self - } - - pub fn nth61(mut self, v: Byte) -> Self { - self.0[61] = v; - self - } - - pub fn nth62(mut self, v: Byte) -> Self { - self.0[62] = v; - self - } - - pub fn nth63(mut self, v: Byte) -> Self { - self.0[63] = v; - self - } -} -impl molecule::prelude::Builder for Byte64Builder { - type Entity = Byte64; - - const NAME: &'static str = "Byte64Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - writer.write_all(self.0[24].as_slice())?; - writer.write_all(self.0[25].as_slice())?; - writer.write_all(self.0[26].as_slice())?; - writer.write_all(self.0[27].as_slice())?; - writer.write_all(self.0[28].as_slice())?; - writer.write_all(self.0[29].as_slice())?; - writer.write_all(self.0[30].as_slice())?; - writer.write_all(self.0[31].as_slice())?; - writer.write_all(self.0[32].as_slice())?; - writer.write_all(self.0[33].as_slice())?; - writer.write_all(self.0[34].as_slice())?; - writer.write_all(self.0[35].as_slice())?; - writer.write_all(self.0[36].as_slice())?; - writer.write_all(self.0[37].as_slice())?; - writer.write_all(self.0[38].as_slice())?; - writer.write_all(self.0[39].as_slice())?; - writer.write_all(self.0[40].as_slice())?; - writer.write_all(self.0[41].as_slice())?; - writer.write_all(self.0[42].as_slice())?; - writer.write_all(self.0[43].as_slice())?; - writer.write_all(self.0[44].as_slice())?; - writer.write_all(self.0[45].as_slice())?; - writer.write_all(self.0[46].as_slice())?; - writer.write_all(self.0[47].as_slice())?; - writer.write_all(self.0[48].as_slice())?; - writer.write_all(self.0[49].as_slice())?; - writer.write_all(self.0[50].as_slice())?; - writer.write_all(self.0[51].as_slice())?; - writer.write_all(self.0[52].as_slice())?; - writer.write_all(self.0[53].as_slice())?; - writer.write_all(self.0[54].as_slice())?; - writer.write_all(self.0[55].as_slice())?; - writer.write_all(self.0[56].as_slice())?; - writer.write_all(self.0[57].as_slice())?; - writer.write_all(self.0[58].as_slice())?; - writer.write_all(self.0[59].as_slice())?; - writer.write_all(self.0[60].as_slice())?; - writer.write_all(self.0[61].as_slice())?; - writer.write_all(self.0[62].as_slice())?; - writer.write_all(self.0[63].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte64::new_unchecked(inner.into()) - } -} -#[derive(Clone)] pub struct LockScriptVec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for LockScriptVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { From 97276d8a017c4b440a92c2b89794f73dac2aaa08 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 25 Oct 2022 17:01:48 +0800 Subject: [PATCH 15/19] refactor: Update SoicalUnlockEntries --- src/common.mol | 1 - src/common.rs | 1204 ------------------------------------------------ src/joyid.mol | 21 +- src/joyid.rs | 735 +++++++++++++++++------------ 4 files changed, 451 insertions(+), 1510 deletions(-) diff --git a/src/common.mol b/src/common.mol index cd2cac0..141ba43 100644 --- a/src/common.mol +++ b/src/common.mol @@ -9,7 +9,6 @@ array Byte9 [byte; 9]; array Byte20 [byte; 20]; array Byte24 [byte; 24]; array Byte32 [byte; 32]; -array Byte64 [byte; 64]; array CotaId [byte; 20]; array Characteristic [byte; 20]; diff --git a/src/common.rs b/src/common.rs index d2fea8b..c9a9bb4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -3093,1210 +3093,6 @@ impl molecule::prelude::Builder for Byte32Builder { } } #[derive(Clone)] -pub struct Byte64(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte64 { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, - ]; - Byte64::new_unchecked(v.into()) - } -} -impl Byte64 { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn nth24(&self) -> Byte { - Byte::new_unchecked(self.0.slice(24..25)) - } - - pub fn nth25(&self) -> Byte { - Byte::new_unchecked(self.0.slice(25..26)) - } - - pub fn nth26(&self) -> Byte { - Byte::new_unchecked(self.0.slice(26..27)) - } - - pub fn nth27(&self) -> Byte { - Byte::new_unchecked(self.0.slice(27..28)) - } - - pub fn nth28(&self) -> Byte { - Byte::new_unchecked(self.0.slice(28..29)) - } - - pub fn nth29(&self) -> Byte { - Byte::new_unchecked(self.0.slice(29..30)) - } - - pub fn nth30(&self) -> Byte { - Byte::new_unchecked(self.0.slice(30..31)) - } - - pub fn nth31(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) - } - - pub fn nth32(&self) -> Byte { - Byte::new_unchecked(self.0.slice(32..33)) - } - - pub fn nth33(&self) -> Byte { - Byte::new_unchecked(self.0.slice(33..34)) - } - - pub fn nth34(&self) -> Byte { - Byte::new_unchecked(self.0.slice(34..35)) - } - - pub fn nth35(&self) -> Byte { - Byte::new_unchecked(self.0.slice(35..36)) - } - - pub fn nth36(&self) -> Byte { - Byte::new_unchecked(self.0.slice(36..37)) - } - - pub fn nth37(&self) -> Byte { - Byte::new_unchecked(self.0.slice(37..38)) - } - - pub fn nth38(&self) -> Byte { - Byte::new_unchecked(self.0.slice(38..39)) - } - - pub fn nth39(&self) -> Byte { - Byte::new_unchecked(self.0.slice(39..40)) - } - - pub fn nth40(&self) -> Byte { - Byte::new_unchecked(self.0.slice(40..41)) - } - - pub fn nth41(&self) -> Byte { - Byte::new_unchecked(self.0.slice(41..42)) - } - - pub fn nth42(&self) -> Byte { - Byte::new_unchecked(self.0.slice(42..43)) - } - - pub fn nth43(&self) -> Byte { - Byte::new_unchecked(self.0.slice(43..44)) - } - - pub fn nth44(&self) -> Byte { - Byte::new_unchecked(self.0.slice(44..45)) - } - - pub fn nth45(&self) -> Byte { - Byte::new_unchecked(self.0.slice(45..46)) - } - - pub fn nth46(&self) -> Byte { - Byte::new_unchecked(self.0.slice(46..47)) - } - - pub fn nth47(&self) -> Byte { - Byte::new_unchecked(self.0.slice(47..48)) - } - - pub fn nth48(&self) -> Byte { - Byte::new_unchecked(self.0.slice(48..49)) - } - - pub fn nth49(&self) -> Byte { - Byte::new_unchecked(self.0.slice(49..50)) - } - - pub fn nth50(&self) -> Byte { - Byte::new_unchecked(self.0.slice(50..51)) - } - - pub fn nth51(&self) -> Byte { - Byte::new_unchecked(self.0.slice(51..52)) - } - - pub fn nth52(&self) -> Byte { - Byte::new_unchecked(self.0.slice(52..53)) - } - - pub fn nth53(&self) -> Byte { - Byte::new_unchecked(self.0.slice(53..54)) - } - - pub fn nth54(&self) -> Byte { - Byte::new_unchecked(self.0.slice(54..55)) - } - - pub fn nth55(&self) -> Byte { - Byte::new_unchecked(self.0.slice(55..56)) - } - - pub fn nth56(&self) -> Byte { - Byte::new_unchecked(self.0.slice(56..57)) - } - - pub fn nth57(&self) -> Byte { - Byte::new_unchecked(self.0.slice(57..58)) - } - - pub fn nth58(&self) -> Byte { - Byte::new_unchecked(self.0.slice(58..59)) - } - - pub fn nth59(&self) -> Byte { - Byte::new_unchecked(self.0.slice(59..60)) - } - - pub fn nth60(&self) -> Byte { - Byte::new_unchecked(self.0.slice(60..61)) - } - - pub fn nth61(&self) -> Byte { - Byte::new_unchecked(self.0.slice(61..62)) - } - - pub fn nth62(&self) -> Byte { - Byte::new_unchecked(self.0.slice(62..63)) - } - - pub fn nth63(&self) -> Byte { - Byte::new_unchecked(self.0.slice(63..64)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte64Reader<'r> { - Byte64Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte64 { - type Builder = Byte64Builder; - - const NAME: &'static str = "Byte64"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte64(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte64Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - self.nth24(), - self.nth25(), - self.nth26(), - self.nth27(), - self.nth28(), - self.nth29(), - self.nth30(), - self.nth31(), - self.nth32(), - self.nth33(), - self.nth34(), - self.nth35(), - self.nth36(), - self.nth37(), - self.nth38(), - self.nth39(), - self.nth40(), - self.nth41(), - self.nth42(), - self.nth43(), - self.nth44(), - self.nth45(), - self.nth46(), - self.nth47(), - self.nth48(), - self.nth49(), - self.nth50(), - self.nth51(), - self.nth52(), - self.nth53(), - self.nth54(), - self.nth55(), - self.nth56(), - self.nth57(), - self.nth58(), - self.nth59(), - self.nth60(), - self.nth61(), - self.nth62(), - self.nth63(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte64Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte64Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte64Reader<'r> { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn nth24(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[24..25]) - } - - pub fn nth25(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[25..26]) - } - - pub fn nth26(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[26..27]) - } - - pub fn nth27(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[27..28]) - } - - pub fn nth28(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[28..29]) - } - - pub fn nth29(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[29..30]) - } - - pub fn nth30(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[30..31]) - } - - pub fn nth31(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[31..32]) - } - - pub fn nth32(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[32..33]) - } - - pub fn nth33(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[33..34]) - } - - pub fn nth34(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[34..35]) - } - - pub fn nth35(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[35..36]) - } - - pub fn nth36(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[36..37]) - } - - pub fn nth37(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[37..38]) - } - - pub fn nth38(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[38..39]) - } - - pub fn nth39(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[39..40]) - } - - pub fn nth40(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[40..41]) - } - - pub fn nth41(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[41..42]) - } - - pub fn nth42(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[42..43]) - } - - pub fn nth43(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[43..44]) - } - - pub fn nth44(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[44..45]) - } - - pub fn nth45(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[45..46]) - } - - pub fn nth46(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[46..47]) - } - - pub fn nth47(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[47..48]) - } - - pub fn nth48(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[48..49]) - } - - pub fn nth49(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[49..50]) - } - - pub fn nth50(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[50..51]) - } - - pub fn nth51(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[51..52]) - } - - pub fn nth52(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[52..53]) - } - - pub fn nth53(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[53..54]) - } - - pub fn nth54(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[54..55]) - } - - pub fn nth55(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[55..56]) - } - - pub fn nth56(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[56..57]) - } - - pub fn nth57(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[57..58]) - } - - pub fn nth58(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[58..59]) - } - - pub fn nth59(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[59..60]) - } - - pub fn nth60(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[60..61]) - } - - pub fn nth61(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[61..62]) - } - - pub fn nth62(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[62..63]) - } - - pub fn nth63(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[63..64]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte64Reader<'r> { - type Entity = Byte64; - - const NAME: &'static str = "Byte64Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte64Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte64Builder(pub(crate) [Byte; 64]); -impl ::core::fmt::Debug for Byte64Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte64Builder { - fn default() -> Self { - Byte64Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte64Builder { - pub const ITEM_COUNT: usize = 64; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 64; - - pub fn set(mut self, v: [Byte; 64]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } - - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self - } - - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self - } - - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self - } - - pub fn nth24(mut self, v: Byte) -> Self { - self.0[24] = v; - self - } - - pub fn nth25(mut self, v: Byte) -> Self { - self.0[25] = v; - self - } - - pub fn nth26(mut self, v: Byte) -> Self { - self.0[26] = v; - self - } - - pub fn nth27(mut self, v: Byte) -> Self { - self.0[27] = v; - self - } - - pub fn nth28(mut self, v: Byte) -> Self { - self.0[28] = v; - self - } - - pub fn nth29(mut self, v: Byte) -> Self { - self.0[29] = v; - self - } - - pub fn nth30(mut self, v: Byte) -> Self { - self.0[30] = v; - self - } - - pub fn nth31(mut self, v: Byte) -> Self { - self.0[31] = v; - self - } - - pub fn nth32(mut self, v: Byte) -> Self { - self.0[32] = v; - self - } - - pub fn nth33(mut self, v: Byte) -> Self { - self.0[33] = v; - self - } - - pub fn nth34(mut self, v: Byte) -> Self { - self.0[34] = v; - self - } - - pub fn nth35(mut self, v: Byte) -> Self { - self.0[35] = v; - self - } - - pub fn nth36(mut self, v: Byte) -> Self { - self.0[36] = v; - self - } - - pub fn nth37(mut self, v: Byte) -> Self { - self.0[37] = v; - self - } - - pub fn nth38(mut self, v: Byte) -> Self { - self.0[38] = v; - self - } - - pub fn nth39(mut self, v: Byte) -> Self { - self.0[39] = v; - self - } - - pub fn nth40(mut self, v: Byte) -> Self { - self.0[40] = v; - self - } - - pub fn nth41(mut self, v: Byte) -> Self { - self.0[41] = v; - self - } - - pub fn nth42(mut self, v: Byte) -> Self { - self.0[42] = v; - self - } - - pub fn nth43(mut self, v: Byte) -> Self { - self.0[43] = v; - self - } - - pub fn nth44(mut self, v: Byte) -> Self { - self.0[44] = v; - self - } - - pub fn nth45(mut self, v: Byte) -> Self { - self.0[45] = v; - self - } - - pub fn nth46(mut self, v: Byte) -> Self { - self.0[46] = v; - self - } - - pub fn nth47(mut self, v: Byte) -> Self { - self.0[47] = v; - self - } - - pub fn nth48(mut self, v: Byte) -> Self { - self.0[48] = v; - self - } - - pub fn nth49(mut self, v: Byte) -> Self { - self.0[49] = v; - self - } - - pub fn nth50(mut self, v: Byte) -> Self { - self.0[50] = v; - self - } - - pub fn nth51(mut self, v: Byte) -> Self { - self.0[51] = v; - self - } - - pub fn nth52(mut self, v: Byte) -> Self { - self.0[52] = v; - self - } - - pub fn nth53(mut self, v: Byte) -> Self { - self.0[53] = v; - self - } - - pub fn nth54(mut self, v: Byte) -> Self { - self.0[54] = v; - self - } - - pub fn nth55(mut self, v: Byte) -> Self { - self.0[55] = v; - self - } - - pub fn nth56(mut self, v: Byte) -> Self { - self.0[56] = v; - self - } - - pub fn nth57(mut self, v: Byte) -> Self { - self.0[57] = v; - self - } - - pub fn nth58(mut self, v: Byte) -> Self { - self.0[58] = v; - self - } - - pub fn nth59(mut self, v: Byte) -> Self { - self.0[59] = v; - self - } - - pub fn nth60(mut self, v: Byte) -> Self { - self.0[60] = v; - self - } - - pub fn nth61(mut self, v: Byte) -> Self { - self.0[61] = v; - self - } - - pub fn nth62(mut self, v: Byte) -> Self { - self.0[62] = v; - self - } - - pub fn nth63(mut self, v: Byte) -> Self { - self.0[63] = v; - self - } -} -impl molecule::prelude::Builder for Byte64Builder { - type Entity = Byte64; - - const NAME: &'static str = "Byte64Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - writer.write_all(self.0[24].as_slice())?; - writer.write_all(self.0[25].as_slice())?; - writer.write_all(self.0[26].as_slice())?; - writer.write_all(self.0[27].as_slice())?; - writer.write_all(self.0[28].as_slice())?; - writer.write_all(self.0[29].as_slice())?; - writer.write_all(self.0[30].as_slice())?; - writer.write_all(self.0[31].as_slice())?; - writer.write_all(self.0[32].as_slice())?; - writer.write_all(self.0[33].as_slice())?; - writer.write_all(self.0[34].as_slice())?; - writer.write_all(self.0[35].as_slice())?; - writer.write_all(self.0[36].as_slice())?; - writer.write_all(self.0[37].as_slice())?; - writer.write_all(self.0[38].as_slice())?; - writer.write_all(self.0[39].as_slice())?; - writer.write_all(self.0[40].as_slice())?; - writer.write_all(self.0[41].as_slice())?; - writer.write_all(self.0[42].as_slice())?; - writer.write_all(self.0[43].as_slice())?; - writer.write_all(self.0[44].as_slice())?; - writer.write_all(self.0[45].as_slice())?; - writer.write_all(self.0[46].as_slice())?; - writer.write_all(self.0[47].as_slice())?; - writer.write_all(self.0[48].as_slice())?; - writer.write_all(self.0[49].as_slice())?; - writer.write_all(self.0[50].as_slice())?; - writer.write_all(self.0[51].as_slice())?; - writer.write_all(self.0[52].as_slice())?; - writer.write_all(self.0[53].as_slice())?; - writer.write_all(self.0[54].as_slice())?; - writer.write_all(self.0[55].as_slice())?; - writer.write_all(self.0[56].as_slice())?; - writer.write_all(self.0[57].as_slice())?; - writer.write_all(self.0[58].as_slice())?; - writer.write_all(self.0[59].as_slice())?; - writer.write_all(self.0[60].as_slice())?; - writer.write_all(self.0[61].as_slice())?; - writer.write_all(self.0[62].as_slice())?; - writer.write_all(self.0[63].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte64::new_unchecked(inner.into()) - } -} -#[derive(Clone)] pub struct CotaId(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for CotaId { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { diff --git a/src/joyid.mol b/src/joyid.mol index b4a6c84..bc67dc3 100644 --- a/src/joyid.mol +++ b/src/joyid.mol @@ -46,11 +46,20 @@ table SocialEntries { values: SocialValueVec, } -vector PubkeyVec ; -vector SignatureVec ; + +table FriendPubkey { + unlock_mode: byte, + alg_index: Uint16, + pubkey: Bytes, + signature: Bytes, + + // only for subkey + ext_data: Uint32, + subkey_proof: Bytes, +} +vector FriendPubkeyVec ; table SocialUnlockEntries { social_value: SocialValue, - social_proof: Bytes, - pubkeys: PubkeyVec, - signatures: SignatureVec, -} \ No newline at end of file + social_proof: Bytes, + social_friends: FriendPubkeyVec, +} diff --git a/src/joyid.rs b/src/joyid.rs index ce9fa41..dbcd769 100644 --- a/src/joyid.rs +++ b/src/joyid.rs @@ -3798,8 +3798,8 @@ impl molecule::prelude::Builder for SocialEntriesBuilder { } } #[derive(Clone)] -pub struct PubkeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for PubkeyVec { +pub struct FriendPubkey(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for FriendPubkey { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3808,74 +3808,116 @@ impl ::core::fmt::LowerHex for PubkeyVec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for PubkeyVec { +impl ::core::fmt::Debug for FriendPubkey { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for PubkeyVec { +impl ::core::fmt::Display for FriendPubkey { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "unlock_mode", self.unlock_mode())?; + write!(f, ", {}: {}", "alg_index", self.alg_index())?; + write!(f, ", {}: {}", "pubkey", self.pubkey())?; + write!(f, ", {}: {}", "signature", self.signature())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; } - write!(f, "]") + write!(f, " }}") } } -impl ::core::default::Default for PubkeyVec { +impl ::core::default::Default for FriendPubkey { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - PubkeyVec::new_unchecked(v.into()) + let v: Vec = vec![ + 47, 0, 0, 0, 28, 0, 0, 0, 29, 0, 0, 0, 31, 0, 0, 0, 35, 0, 0, 0, 39, 0, 0, 0, 43, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + FriendPubkey::new_unchecked(v.into()) } } -impl PubkeyVec { - pub const ITEM_SIZE: usize = 64; +impl FriendPubkey { + pub const FIELD_COUNT: usize = 6; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } - pub fn len(&self) -> usize { - self.item_count() + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT } - pub fn is_empty(&self) -> bool { - self.len() == 0 + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() } - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } + pub fn unlock_mode(&self) -> Byte { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Byte::new_unchecked(self.0.slice(start..end)) } - pub fn get_unchecked(&self, idx: usize) -> Byte64 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte64::new_unchecked(self.0.slice(start..end)) + pub fn alg_index(&self) -> Uint16 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Uint16::new_unchecked(self.0.slice(start..end)) + } + + pub fn pubkey(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn signature(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + let end = molecule::unpack_number(&slice[20..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + + pub fn ext_data(&self) -> Uint32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[20..]) as usize; + let end = molecule::unpack_number(&slice[24..]) as usize; + Uint32::new_unchecked(self.0.slice(start..end)) } - pub fn as_reader<'r>(&'r self) -> PubkeyVecReader<'r> { - PubkeyVecReader::new_unchecked(self.as_slice()) + pub fn subkey_proof(&self) -> Bytes { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[24..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[28..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } else { + Bytes::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> FriendPubkeyReader<'r> { + FriendPubkeyReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for PubkeyVec { - type Builder = PubkeyVecBuilder; +impl molecule::prelude::Entity for FriendPubkey { + type Builder = FriendPubkeyBuilder; - const NAME: &'static str = "PubkeyVec"; + const NAME: &'static str = "FriendPubkey"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - PubkeyVec(data) + FriendPubkey(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -3887,11 +3929,11 @@ impl molecule::prelude::Entity for PubkeyVec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - PubkeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) + FriendPubkeyReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - PubkeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + FriendPubkeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -3899,12 +3941,18 @@ impl molecule::prelude::Entity for PubkeyVec { } fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) + Self::new_builder() + .unlock_mode(self.unlock_mode()) + .alg_index(self.alg_index()) + .pubkey(self.pubkey()) + .signature(self.signature()) + .ext_data(self.ext_data()) + .subkey_proof(self.subkey_proof()) } } #[derive(Clone, Copy)] -pub struct PubkeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for PubkeyVecReader<'r> { +pub struct FriendPubkeyReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for FriendPubkeyReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3913,136 +3961,241 @@ impl<'r> ::core::fmt::LowerHex for PubkeyVecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for PubkeyVecReader<'r> { +impl<'r> ::core::fmt::Debug for FriendPubkeyReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for PubkeyVecReader<'r> { +impl<'r> ::core::fmt::Display for FriendPubkeyReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "unlock_mode", self.unlock_mode())?; + write!(f, ", {}: {}", "alg_index", self.alg_index())?; + write!(f, ", {}: {}", "pubkey", self.pubkey())?; + write!(f, ", {}: {}", "signature", self.signature())?; + write!(f, ", {}: {}", "ext_data", self.ext_data())?; + write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; } - write!(f, "]") + write!(f, " }}") } } -impl<'r> PubkeyVecReader<'r> { - pub const ITEM_SIZE: usize = 64; +impl<'r> FriendPubkeyReader<'r> { + pub const FIELD_COUNT: usize = 6; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + pub fn field_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } - pub fn len(&self) -> usize { - self.item_count() + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT } - pub fn is_empty(&self) -> bool { - self.len() == 0 + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() } - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } + pub fn unlock_mode(&self) -> ByteReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + ByteReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn get_unchecked(&self, idx: usize) -> Byte64Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte64Reader::new_unchecked(&self.as_slice()[start..end]) + pub fn alg_index(&self) -> Uint16Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[8..]) as usize; + let end = molecule::unpack_number(&slice[12..]) as usize; + Uint16Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn pubkey(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[12..]) as usize; + let end = molecule::unpack_number(&slice[16..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn signature(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[16..]) as usize; + let end = molecule::unpack_number(&slice[20..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn ext_data(&self) -> Uint32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[20..]) as usize; + let end = molecule::unpack_number(&slice[24..]) as usize; + Uint32Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn subkey_proof(&self) -> BytesReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[24..]) as usize; + if self.has_extra_fields() { + let end = molecule::unpack_number(&slice[28..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } else { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } } } -impl<'r> molecule::prelude::Reader<'r> for PubkeyVecReader<'r> { - type Entity = PubkeyVec; +impl<'r> molecule::prelude::Reader<'r> for FriendPubkeyReader<'r> { + type Entity = FriendPubkey; - const NAME: &'static str = "PubkeyVecReader"; + const NAME: &'static str = "FriendPubkeyReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - PubkeyVecReader(slice) + FriendPubkeyReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + let total_size = molecule::unpack_number(slice) as usize; if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } + if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let field_count = offset_first / molecule::NUMBER_SIZE - 1; + if field_count < Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + } else if !compatible && field_count > Self::FIELD_COUNT { + return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); + }; + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + Uint16Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; + BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + Uint32Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?; + BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; Ok(()) } } #[derive(Debug, Default)] -pub struct PubkeyVecBuilder(pub(crate) Vec); -impl PubkeyVecBuilder { - pub const ITEM_SIZE: usize = 64; +pub struct FriendPubkeyBuilder { + pub(crate) unlock_mode: Byte, + pub(crate) alg_index: Uint16, + pub(crate) pubkey: Bytes, + pub(crate) signature: Bytes, + pub(crate) ext_data: Uint32, + pub(crate) subkey_proof: Bytes, +} +impl FriendPubkeyBuilder { + pub const FIELD_COUNT: usize = 6; - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; + pub fn unlock_mode(mut self, v: Byte) -> Self { + self.unlock_mode = v; self } - pub fn push(mut self, v: Byte64) -> Self { - self.0.push(v); + pub fn alg_index(mut self, v: Uint16) -> Self { + self.alg_index = v; self } - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + pub fn pubkey(mut self, v: Bytes) -> Self { + self.pubkey = v; self } - pub fn replace(&mut self, index: usize, v: Byte64) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) + pub fn signature(mut self, v: Bytes) -> Self { + self.signature = v; + self + } + + pub fn ext_data(mut self, v: Uint32) -> Self { + self.ext_data = v; + self + } + + pub fn subkey_proof(mut self, v: Bytes) -> Self { + self.subkey_proof = v; + self } } -impl molecule::prelude::Builder for PubkeyVecBuilder { - type Entity = PubkeyVec; +impl molecule::prelude::Builder for FriendPubkeyBuilder { + type Entity = FriendPubkey; - const NAME: &'static str = "PubkeyVecBuilder"; + const NAME: &'static str = "FriendPubkeyBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.unlock_mode.as_slice().len() + + self.alg_index.as_slice().len() + + self.pubkey.as_slice().len() + + self.signature.as_slice().len() + + self.ext_data.as_slice().len() + + self.subkey_proof.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; + let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); + let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); + offsets.push(total_size); + total_size += self.unlock_mode.as_slice().len(); + offsets.push(total_size); + total_size += self.alg_index.as_slice().len(); + offsets.push(total_size); + total_size += self.pubkey.as_slice().len(); + offsets.push(total_size); + total_size += self.signature.as_slice().len(); + offsets.push(total_size); + total_size += self.ext_data.as_slice().len(); + offsets.push(total_size); + total_size += self.subkey_proof.as_slice().len(); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } + writer.write_all(self.unlock_mode.as_slice())?; + writer.write_all(self.alg_index.as_slice())?; + writer.write_all(self.pubkey.as_slice())?; + writer.write_all(self.signature.as_slice())?; + writer.write_all(self.ext_data.as_slice())?; + writer.write_all(self.subkey_proof.as_slice())?; Ok(()) } @@ -4050,64 +4203,12 @@ impl molecule::prelude::Builder for PubkeyVecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - PubkeyVec::new_unchecked(inner.into()) - } -} -pub struct PubkeyVecIterator(PubkeyVec, usize, usize); -impl ::core::iter::Iterator for PubkeyVecIterator { - type Item = Byte64; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for PubkeyVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for PubkeyVec { - type IntoIter = PubkeyVecIterator; - type Item = Byte64; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - PubkeyVecIterator(self, 0, len) - } -} -impl<'r> PubkeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> PubkeyVecReaderIterator<'t, 'r> { - PubkeyVecReaderIterator(&self, 0, self.len()) - } -} -pub struct PubkeyVecReaderIterator<'t, 'r>(&'t PubkeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for PubkeyVecReaderIterator<'t, 'r> { - type Item = Byte64Reader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for PubkeyVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 + FriendPubkey::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct SignatureVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SignatureVec { +pub struct FriendPubkeyVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for FriendPubkeyVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -4116,12 +4217,12 @@ impl ::core::fmt::LowerHex for SignatureVec { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for SignatureVec { +impl ::core::fmt::Debug for FriendPubkeyVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for SignatureVec { +impl ::core::fmt::Display for FriendPubkeyVec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -4134,21 +4235,23 @@ impl ::core::fmt::Display for SignatureVec { write!(f, "]") } } -impl ::core::default::Default for SignatureVec { +impl ::core::default::Default for FriendPubkeyVec { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SignatureVec::new_unchecked(v.into()) + let v: Vec = vec![4, 0, 0, 0]; + FriendPubkeyVec::new_unchecked(v.into()) } } -impl SignatureVec { - pub const ITEM_SIZE: usize = 64; - +impl FriendPubkeyVec { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -4159,7 +4262,7 @@ impl SignatureVec { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option { + pub fn get(&self, idx: usize) -> Option { if idx >= self.len() { None } else { @@ -4167,23 +4270,30 @@ impl SignatureVec { } } - pub fn get_unchecked(&self, idx: usize) -> Byte64 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte64::new_unchecked(self.0.slice(start..end)) + pub fn get_unchecked(&self, idx: usize) -> FriendPubkey { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + FriendPubkey::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + FriendPubkey::new_unchecked(self.0.slice(start..end)) + } } - pub fn as_reader<'r>(&'r self) -> SignatureVecReader<'r> { - SignatureVecReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> FriendPubkeyVecReader<'r> { + FriendPubkeyVecReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for SignatureVec { - type Builder = SignatureVecBuilder; +impl molecule::prelude::Entity for FriendPubkeyVec { + type Builder = FriendPubkeyVecBuilder; - const NAME: &'static str = "SignatureVec"; + const NAME: &'static str = "FriendPubkeyVec"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SignatureVec(data) + FriendPubkeyVec(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -4195,11 +4305,11 @@ impl molecule::prelude::Entity for SignatureVec { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SignatureVecReader::from_slice(slice).map(|reader| reader.to_entity()) + FriendPubkeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SignatureVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + FriendPubkeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -4211,8 +4321,8 @@ impl molecule::prelude::Entity for SignatureVec { } } #[derive(Clone, Copy)] -pub struct SignatureVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SignatureVecReader<'r> { +pub struct FriendPubkeyVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for FriendPubkeyVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -4221,12 +4331,12 @@ impl<'r> ::core::fmt::LowerHex for SignatureVecReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for SignatureVecReader<'r> { +impl<'r> ::core::fmt::Debug for FriendPubkeyVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for SignatureVecReader<'r> { +impl<'r> ::core::fmt::Display for FriendPubkeyVecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} [", Self::NAME)?; for i in 0..self.len() { @@ -4239,15 +4349,17 @@ impl<'r> ::core::fmt::Display for SignatureVecReader<'r> { write!(f, "]") } } -impl<'r> SignatureVecReader<'r> { - pub const ITEM_SIZE: usize = 64; - +impl<'r> FriendPubkeyVecReader<'r> { pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + molecule::unpack_number(self.as_slice()) as usize } pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } } pub fn len(&self) -> usize { @@ -4258,7 +4370,7 @@ impl<'r> SignatureVecReader<'r> { self.len() == 0 } - pub fn get(&self, idx: usize) -> Option> { + pub fn get(&self, idx: usize) -> Option> { if idx >= self.len() { None } else { @@ -4266,90 +4378,144 @@ impl<'r> SignatureVecReader<'r> { } } - pub fn get_unchecked(&self, idx: usize) -> Byte64Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte64Reader::new_unchecked(&self.as_slice()[start..end]) + pub fn get_unchecked(&self, idx: usize) -> FriendPubkeyReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + FriendPubkeyReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + FriendPubkeyReader::new_unchecked(&self.as_slice()[start..end]) + } } } -impl<'r> molecule::prelude::Reader<'r> for SignatureVecReader<'r> { - type Entity = SignatureVec; +impl<'r> molecule::prelude::Reader<'r> for FriendPubkeyVecReader<'r> { + type Entity = FriendPubkeyVec; - const NAME: &'static str = "SignatureVecReader"; + const NAME: &'static str = "FriendPubkeyVecReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - SignatureVecReader(slice) + FriendPubkeyVecReader(slice) } fn as_slice(&self) -> &'r [u8] { self.0 } - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + let total_size = molecule::unpack_number(slice) as usize; if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + FriendPubkeyReader::verify(&slice[start..end], compatible)?; + } Ok(()) } } #[derive(Debug, Default)] -pub struct SignatureVecBuilder(pub(crate) Vec); -impl SignatureVecBuilder { - pub const ITEM_SIZE: usize = 64; - - pub fn set(mut self, v: Vec) -> Self { +pub struct FriendPubkeyVecBuilder(pub(crate) Vec); +impl FriendPubkeyVecBuilder { + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: Byte64) -> Self { + pub fn push(mut self, v: FriendPubkey) -> Self { self.0.push(v); self } - pub fn extend>(mut self, iter: T) -> Self { + pub fn extend>(mut self, iter: T) -> Self { for elem in iter { self.0.push(elem); } self } - pub fn replace(&mut self, index: usize, v: Byte64) -> Option { + pub fn replace(&mut self, index: usize, v: FriendPubkey) -> Option { self.0 .get_mut(index) .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for SignatureVecBuilder { - type Entity = SignatureVec; +impl molecule::prelude::Builder for FriendPubkeyVecBuilder { + type Entity = FriendPubkeyVec; - const NAME: &'static str = "SignatureVecBuilder"; + const NAME: &'static str = "FriendPubkeyVecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } } Ok(()) } @@ -4358,12 +4524,12 @@ impl molecule::prelude::Builder for SignatureVecBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SignatureVec::new_unchecked(inner.into()) + FriendPubkeyVec::new_unchecked(inner.into()) } } -pub struct SignatureVecIterator(SignatureVec, usize, usize); -impl ::core::iter::Iterator for SignatureVecIterator { - type Item = Byte64; +pub struct FriendPubkeyVecIterator(FriendPubkeyVec, usize, usize); +impl ::core::iter::Iterator for FriendPubkeyVecIterator { + type Item = FriendPubkey; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -4375,28 +4541,28 @@ impl ::core::iter::Iterator for SignatureVecIterator { } } } -impl ::core::iter::ExactSizeIterator for SignatureVecIterator { +impl ::core::iter::ExactSizeIterator for FriendPubkeyVecIterator { fn len(&self) -> usize { self.2 - self.1 } } -impl ::core::iter::IntoIterator for SignatureVec { - type IntoIter = SignatureVecIterator; - type Item = Byte64; +impl ::core::iter::IntoIterator for FriendPubkeyVec { + type IntoIter = FriendPubkeyVecIterator; + type Item = FriendPubkey; fn into_iter(self) -> Self::IntoIter { let len = self.len(); - SignatureVecIterator(self, 0, len) + FriendPubkeyVecIterator(self, 0, len) } } -impl<'r> SignatureVecReader<'r> { - pub fn iter<'t>(&'t self) -> SignatureVecReaderIterator<'t, 'r> { - SignatureVecReaderIterator(&self, 0, self.len()) +impl<'r> FriendPubkeyVecReader<'r> { + pub fn iter<'t>(&'t self) -> FriendPubkeyVecReaderIterator<'t, 'r> { + FriendPubkeyVecReaderIterator(&self, 0, self.len()) } } -pub struct SignatureVecReaderIterator<'t, 'r>(&'t SignatureVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SignatureVecReaderIterator<'t, 'r> { - type Item = Byte64Reader<'t>; +pub struct FriendPubkeyVecReaderIterator<'t, 'r>(&'t FriendPubkeyVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for FriendPubkeyVecReaderIterator<'t, 'r> { + type Item = FriendPubkeyReader<'t>; fn next(&mut self) -> Option { if self.1 >= self.2 { @@ -4408,7 +4574,7 @@ impl<'t: 'r, 'r> ::core::iter::Iterator for SignatureVecReaderIterator<'t, 'r> { } } } -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SignatureVecReaderIterator<'t, 'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for FriendPubkeyVecReaderIterator<'t, 'r> { fn len(&self) -> usize { self.2 - self.1 } @@ -4434,8 +4600,7 @@ impl ::core::fmt::Display for SocialUnlockEntries { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "social_value", self.social_value())?; write!(f, ", {}: {}", "social_proof", self.social_proof())?; - write!(f, ", {}: {}", "pubkeys", self.pubkeys())?; - write!(f, ", {}: {}", "signatures", self.signatures())?; + write!(f, ", {}: {}", "social_friends", self.social_friends())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -4446,15 +4611,14 @@ impl ::core::fmt::Display for SocialUnlockEntries { impl ::core::default::Default for SocialUnlockEntries { fn default() -> Self { let v: Vec = vec![ - 59, 0, 0, 0, 20, 0, 0, 0, 47, 0, 0, 0, 51, 0, 0, 0, 55, 0, 0, 0, 27, 0, 0, 0, 20, 0, 0, - 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, + 51, 0, 0, 0, 16, 0, 0, 0, 43, 0, 0, 0, 47, 0, 0, 0, 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, + 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, ]; SocialUnlockEntries::new_unchecked(v.into()) } } impl SocialUnlockEntries { - pub const FIELD_COUNT: usize = 4; + pub const FIELD_COUNT: usize = 3; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -4490,21 +4654,14 @@ impl SocialUnlockEntries { Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn pubkeys(&self) -> PubkeyVec { + pub fn social_friends(&self) -> FriendPubkeyVec { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - PubkeyVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn signatures(&self) -> SignatureVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - SignatureVec::new_unchecked(self.0.slice(start..end)) + let end = molecule::unpack_number(&slice[16..]) as usize; + FriendPubkeyVec::new_unchecked(self.0.slice(start..end)) } else { - SignatureVec::new_unchecked(self.0.slice(start..)) + FriendPubkeyVec::new_unchecked(self.0.slice(start..)) } } @@ -4545,8 +4702,7 @@ impl molecule::prelude::Entity for SocialUnlockEntries { Self::new_builder() .social_value(self.social_value()) .social_proof(self.social_proof()) - .pubkeys(self.pubkeys()) - .signatures(self.signatures()) + .social_friends(self.social_friends()) } } #[derive(Clone, Copy)] @@ -4570,8 +4726,7 @@ impl<'r> ::core::fmt::Display for SocialUnlockEntriesReader<'r> { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "social_value", self.social_value())?; write!(f, ", {}: {}", "social_proof", self.social_proof())?; - write!(f, ", {}: {}", "pubkeys", self.pubkeys())?; - write!(f, ", {}: {}", "signatures", self.signatures())?; + write!(f, ", {}: {}", "social_friends", self.social_friends())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { write!(f, ", .. ({} fields)", extra_count)?; @@ -4580,7 +4735,7 @@ impl<'r> ::core::fmt::Display for SocialUnlockEntriesReader<'r> { } } impl<'r> SocialUnlockEntriesReader<'r> { - pub const FIELD_COUNT: usize = 4; + pub const FIELD_COUNT: usize = 3; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -4616,21 +4771,14 @@ impl<'r> SocialUnlockEntriesReader<'r> { BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn pubkeys(&self) -> PubkeyVecReader<'r> { + pub fn social_friends(&self) -> FriendPubkeyVecReader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - PubkeyVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn signatures(&self) -> SignatureVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - SignatureVecReader::new_unchecked(&self.as_slice()[start..end]) + let end = molecule::unpack_number(&slice[16..]) as usize; + FriendPubkeyVecReader::new_unchecked(&self.as_slice()[start..end]) } else { - SignatureVecReader::new_unchecked(&self.as_slice()[start..]) + FriendPubkeyVecReader::new_unchecked(&self.as_slice()[start..]) } } } @@ -4690,20 +4838,18 @@ impl<'r> molecule::prelude::Reader<'r> for SocialUnlockEntriesReader<'r> { } SocialValueReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - PubkeyVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - SignatureVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; + FriendPubkeyVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; Ok(()) } } #[derive(Debug, Default)] pub struct SocialUnlockEntriesBuilder { - pub(crate) social_value: SocialValue, - pub(crate) social_proof: Bytes, - pub(crate) pubkeys: PubkeyVec, - pub(crate) signatures: SignatureVec, + pub(crate) social_value: SocialValue, + pub(crate) social_proof: Bytes, + pub(crate) social_friends: FriendPubkeyVec, } impl SocialUnlockEntriesBuilder { - pub const FIELD_COUNT: usize = 4; + pub const FIELD_COUNT: usize = 3; pub fn social_value(mut self, v: SocialValue) -> Self { self.social_value = v; @@ -4715,13 +4861,8 @@ impl SocialUnlockEntriesBuilder { self } - pub fn pubkeys(mut self, v: PubkeyVec) -> Self { - self.pubkeys = v; - self - } - - pub fn signatures(mut self, v: SignatureVec) -> Self { - self.signatures = v; + pub fn social_friends(mut self, v: FriendPubkeyVec) -> Self { + self.social_friends = v; self } } @@ -4734,8 +4875,7 @@ impl molecule::prelude::Builder for SocialUnlockEntriesBuilder { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.social_value.as_slice().len() + self.social_proof.as_slice().len() - + self.pubkeys.as_slice().len() - + self.signatures.as_slice().len() + + self.social_friends.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { @@ -4746,17 +4886,14 @@ impl molecule::prelude::Builder for SocialUnlockEntriesBuilder { offsets.push(total_size); total_size += self.social_proof.as_slice().len(); offsets.push(total_size); - total_size += self.pubkeys.as_slice().len(); - offsets.push(total_size); - total_size += self.signatures.as_slice().len(); + total_size += self.social_friends.as_slice().len(); writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; for offset in offsets.into_iter() { writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } writer.write_all(self.social_value.as_slice())?; writer.write_all(self.social_proof.as_slice())?; - writer.write_all(self.pubkeys.as_slice())?; - writer.write_all(self.signatures.as_slice())?; + writer.write_all(self.social_friends.as_slice())?; Ok(()) } From ea527595290b06fbeb681c2bd82820429e05f707 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 25 Oct 2022 17:15:56 +0800 Subject: [PATCH 16/19] refactor: Rename key_type to alg_index --- src/joyid.mol | 4 ++-- src/joyid.rs | 44 ++++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/joyid.mol b/src/joyid.mol index bc67dc3..15cb61c 100644 --- a/src/joyid.mol +++ b/src/joyid.mol @@ -9,7 +9,7 @@ struct SubKey { reserved: Byte20, } table SubValue { - key_type: Uint16, + alg_index: Uint16, pubkey_hash: Byte20, reserved: Byte9, padding: byte, @@ -23,7 +23,7 @@ table SubKeyEntries { table SubKeyUnlockEntries { ext_data: Uint32, - key_type: Uint16, + alg_index: Uint16, subkey_proof: Bytes, } diff --git a/src/joyid.rs b/src/joyid.rs index dbcd769..858c4ce 100644 --- a/src/joyid.rs +++ b/src/joyid.rs @@ -629,7 +629,7 @@ impl ::core::fmt::Debug for SubValue { impl ::core::fmt::Display for SubValue { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, "{}: {}", "alg_index", self.alg_index())?; write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; write!(f, ", {}: {}", "reserved", self.reserved())?; write!(f, ", {}: {}", "padding", self.padding())?; @@ -672,7 +672,7 @@ impl SubValue { Self::FIELD_COUNT != self.field_count() } - pub fn key_type(&self) -> Uint16 { + pub fn alg_index(&self) -> Uint16 { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[4..]) as usize; let end = molecule::unpack_number(&slice[8..]) as usize; @@ -739,7 +739,7 @@ impl molecule::prelude::Entity for SubValue { fn as_builder(self) -> Self::Builder { Self::new_builder() - .key_type(self.key_type()) + .alg_index(self.alg_index()) .pubkey_hash(self.pubkey_hash()) .reserved(self.reserved()) .padding(self.padding()) @@ -764,7 +764,7 @@ impl<'r> ::core::fmt::Debug for SubValueReader<'r> { impl<'r> ::core::fmt::Display for SubValueReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "key_type", self.key_type())?; + write!(f, "{}: {}", "alg_index", self.alg_index())?; write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; write!(f, ", {}: {}", "reserved", self.reserved())?; write!(f, ", {}: {}", "padding", self.padding())?; @@ -798,7 +798,7 @@ impl<'r> SubValueReader<'r> { Self::FIELD_COUNT != self.field_count() } - pub fn key_type(&self) -> Uint16Reader<'r> { + pub fn alg_index(&self) -> Uint16Reader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[4..]) as usize; let end = molecule::unpack_number(&slice[8..]) as usize; @@ -893,7 +893,7 @@ impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { } #[derive(Debug, Default)] pub struct SubValueBuilder { - pub(crate) key_type: Uint16, + pub(crate) alg_index: Uint16, pub(crate) pubkey_hash: Byte20, pub(crate) reserved: Byte9, pub(crate) padding: Byte, @@ -901,8 +901,8 @@ pub struct SubValueBuilder { impl SubValueBuilder { pub const FIELD_COUNT: usize = 4; - pub fn key_type(mut self, v: Uint16) -> Self { - self.key_type = v; + pub fn alg_index(mut self, v: Uint16) -> Self { + self.alg_index = v; self } @@ -928,7 +928,7 @@ impl molecule::prelude::Builder for SubValueBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.key_type.as_slice().len() + + self.alg_index.as_slice().len() + self.pubkey_hash.as_slice().len() + self.reserved.as_slice().len() + self.padding.as_slice().len() @@ -938,7 +938,7 @@ impl molecule::prelude::Builder for SubValueBuilder { let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); offsets.push(total_size); - total_size += self.key_type.as_slice().len(); + total_size += self.alg_index.as_slice().len(); offsets.push(total_size); total_size += self.pubkey_hash.as_slice().len(); offsets.push(total_size); @@ -949,7 +949,7 @@ impl molecule::prelude::Builder for SubValueBuilder { for offset in offsets.into_iter() { writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } - writer.write_all(self.key_type.as_slice())?; + writer.write_all(self.alg_index.as_slice())?; writer.write_all(self.pubkey_hash.as_slice())?; writer.write_all(self.reserved.as_slice())?; writer.write_all(self.padding.as_slice())?; @@ -1958,7 +1958,7 @@ impl ::core::fmt::Display for SubKeyUnlockEntries { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "alg_index", self.alg_index())?; write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { @@ -2005,7 +2005,7 @@ impl SubKeyUnlockEntries { Uint32::new_unchecked(self.0.slice(start..end)) } - pub fn key_type(&self) -> Uint16 { + pub fn alg_index(&self) -> Uint16 { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; @@ -2059,7 +2059,7 @@ impl molecule::prelude::Entity for SubKeyUnlockEntries { fn as_builder(self) -> Self::Builder { Self::new_builder() .ext_data(self.ext_data()) - .key_type(self.key_type()) + .alg_index(self.alg_index()) .subkey_proof(self.subkey_proof()) } } @@ -2083,7 +2083,7 @@ impl<'r> ::core::fmt::Display for SubKeyUnlockEntriesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; write!(f, "{}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "key_type", self.key_type())?; + write!(f, ", {}: {}", "alg_index", self.alg_index())?; write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; let extra_count = self.count_extra_fields(); if extra_count != 0 { @@ -2122,7 +2122,7 @@ impl<'r> SubKeyUnlockEntriesReader<'r> { Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } - pub fn key_type(&self) -> Uint16Reader<'r> { + pub fn alg_index(&self) -> Uint16Reader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; @@ -2203,7 +2203,7 @@ impl<'r> molecule::prelude::Reader<'r> for SubKeyUnlockEntriesReader<'r> { #[derive(Debug, Default)] pub struct SubKeyUnlockEntriesBuilder { pub(crate) ext_data: Uint32, - pub(crate) key_type: Uint16, + pub(crate) alg_index: Uint16, pub(crate) subkey_proof: Bytes, } impl SubKeyUnlockEntriesBuilder { @@ -2214,8 +2214,8 @@ impl SubKeyUnlockEntriesBuilder { self } - pub fn key_type(mut self, v: Uint16) -> Self { - self.key_type = v; + pub fn alg_index(mut self, v: Uint16) -> Self { + self.alg_index = v; self } @@ -2232,7 +2232,7 @@ impl molecule::prelude::Builder for SubKeyUnlockEntriesBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + self.ext_data.as_slice().len() - + self.key_type.as_slice().len() + + self.alg_index.as_slice().len() + self.subkey_proof.as_slice().len() } @@ -2242,7 +2242,7 @@ impl molecule::prelude::Builder for SubKeyUnlockEntriesBuilder { offsets.push(total_size); total_size += self.ext_data.as_slice().len(); offsets.push(total_size); - total_size += self.key_type.as_slice().len(); + total_size += self.alg_index.as_slice().len(); offsets.push(total_size); total_size += self.subkey_proof.as_slice().len(); writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; @@ -2250,7 +2250,7 @@ impl molecule::prelude::Builder for SubKeyUnlockEntriesBuilder { writer.write_all(&molecule::pack_number(offset as molecule::Number))?; } writer.write_all(self.ext_data.as_slice())?; - writer.write_all(self.key_type.as_slice())?; + writer.write_all(self.alg_index.as_slice())?; writer.write_all(self.subkey_proof.as_slice())?; Ok(()) } From 4888de35e01b8064ede9d31c2c657a5b03e1a149 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Fri, 28 Oct 2022 13:47:08 +0800 Subject: [PATCH 17/19] fix: Update SubValue Struct --- src/joyid.mol | 2 +- src/joyid.rs | 294 +++++++++----------------------------------------- 2 files changed, 52 insertions(+), 244 deletions(-) diff --git a/src/joyid.mol b/src/joyid.mol index 15cb61c..f3137b4 100644 --- a/src/joyid.mol +++ b/src/joyid.mol @@ -8,7 +8,7 @@ struct SubKey { ext_data: Uint32, reserved: Byte20, } -table SubValue { +struct SubValue { alg_index: Uint16, pubkey_hash: Byte20, reserved: Byte9, diff --git a/src/joyid.rs b/src/joyid.rs index 858c4ce..3d07aa1 100644 --- a/src/joyid.rs +++ b/src/joyid.rs @@ -633,75 +633,37 @@ impl ::core::fmt::Display for SubValue { write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; write!(f, ", {}: {}", "reserved", self.reserved())?; write!(f, ", {}: {}", "padding", self.padding())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } write!(f, " }}") } } impl ::core::default::Default for SubValue { fn default() -> Self { let v: Vec = vec![ - 52, 0, 0, 0, 20, 0, 0, 0, 22, 0, 0, 0, 42, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, ]; SubValue::new_unchecked(v.into()) } } impl SubValue { pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; pub fn alg_index(&self) -> Uint16 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Uint16::new_unchecked(self.0.slice(start..end)) + Uint16::new_unchecked(self.0.slice(0..2)) } pub fn pubkey_hash(&self) -> Byte20 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Byte20::new_unchecked(self.0.slice(start..end)) + Byte20::new_unchecked(self.0.slice(2..22)) } pub fn reserved(&self) -> Byte9 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - Byte9::new_unchecked(self.0.slice(start..end)) + Byte9::new_unchecked(self.0.slice(22..31)) } pub fn padding(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } else { - Byte::new_unchecked(self.0.slice(start..)) - } + Byte::new_unchecked(self.0.slice(31..32)) } pub fn as_reader<'r>(&'r self) -> SubValueReader<'r> { @@ -768,66 +730,28 @@ impl<'r> ::core::fmt::Display for SubValueReader<'r> { write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; write!(f, ", {}: {}", "reserved", self.reserved())?; write!(f, ", {}: {}", "padding", self.padding())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } write!(f, " }}") } } impl<'r> SubValueReader<'r> { pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; pub fn alg_index(&self) -> Uint16Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Uint16Reader::new_unchecked(&self.as_slice()[start..end]) + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) } pub fn pubkey_hash(&self) -> Byte20Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Byte20Reader::new_unchecked(&self.as_slice()[start..end]) + Byte20Reader::new_unchecked(&self.as_slice()[2..22]) } pub fn reserved(&self) -> Byte9Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - Byte9Reader::new_unchecked(&self.as_slice()[start..end]) + Byte9Reader::new_unchecked(&self.as_slice()[22..31]) } pub fn padding(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } else { - ByteReader::new_unchecked(&self.as_slice()[start..]) - } + ByteReader::new_unchecked(&self.as_slice()[31..32]) } } impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { @@ -847,47 +771,12 @@ impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { self.0 } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); } - Uint16Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - Byte20Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - Byte9Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - ByteReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; Ok(()) } } @@ -900,6 +789,8 @@ pub struct SubValueBuilder { } impl SubValueBuilder { pub const FIELD_COUNT: usize = 4; + pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; + pub const TOTAL_SIZE: usize = 32; pub fn alg_index(mut self, v: Uint16) -> Self { self.alg_index = v; @@ -927,28 +818,10 @@ impl molecule::prelude::Builder for SubValueBuilder { const NAME: &'static str = "SubValueBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.alg_index.as_slice().len() - + self.pubkey_hash.as_slice().len() - + self.reserved.as_slice().len() - + self.padding.as_slice().len() + Self::TOTAL_SIZE } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.alg_index.as_slice().len(); - offsets.push(total_size); - total_size += self.pubkey_hash.as_slice().len(); - offsets.push(total_size); - total_size += self.reserved.as_slice().len(); - offsets.push(total_size); - total_size += self.padding.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } writer.write_all(self.alg_index.as_slice())?; writer.write_all(self.pubkey_hash.as_slice())?; writer.write_all(self.reserved.as_slice())?; @@ -1302,21 +1175,19 @@ impl ::core::fmt::Display for SubValueVec { } impl ::core::default::Default for SubValueVec { fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; + let v: Vec = vec![0, 0, 0, 0]; SubValueVec::new_unchecked(v.into()) } } impl SubValueVec { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -1336,16 +1207,9 @@ impl SubValueVec { } pub fn get_unchecked(&self, idx: usize) -> SubValue { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SubValue::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SubValue::new_unchecked(self.0.slice(start..end)) - } + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubValue::new_unchecked(self.0.slice(start..end)) } pub fn as_reader<'r>(&'r self) -> SubValueVecReader<'r> { @@ -1415,16 +1279,14 @@ impl<'r> ::core::fmt::Display for SubValueVecReader<'r> { } } impl<'r> SubValueVecReader<'r> { + pub const ITEM_SIZE: usize = 32; + pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } + molecule::unpack_number(self.as_slice()) as usize } pub fn len(&self) -> usize { @@ -1444,16 +1306,9 @@ impl<'r> SubValueVecReader<'r> { } pub fn get_unchecked(&self, idx: usize) -> SubValueReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SubValueReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SubValueReader::new_unchecked(&self.as_slice()[start..end]) - } + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + SubValueReader::new_unchecked(&self.as_slice()[start..end]) } } impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { @@ -1473,46 +1328,22 @@ impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { self.0 } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { use molecule::verification_error as ve; let slice_len = slice.len(); if slice_len < molecule::NUMBER_SIZE { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } return Ok(()); } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - SubValueReader::verify(&slice[start..end], compatible)?; + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } Ok(()) } @@ -1520,6 +1351,8 @@ impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { #[derive(Debug, Default)] pub struct SubValueVecBuilder(pub(crate) Vec); impl SubValueVecBuilder { + pub const ITEM_SIZE: usize = 32; + pub fn set(mut self, v: Vec) -> Self { self.0 = v; self @@ -1549,38 +1382,13 @@ impl molecule::prelude::Builder for SubValueVecBuilder { const NAME: &'static str = "SubValueVecBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; } Ok(()) } @@ -1675,7 +1483,7 @@ impl ::core::fmt::Display for SubKeyEntries { impl ::core::default::Default for SubKeyEntries { fn default() -> Self { let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; SubKeyEntries::new_unchecked(v.into()) } From e6b7da217080aed87ff3b3cd0b8d8a08bb713ccb Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 1 Nov 2022 17:12:03 +0800 Subject: [PATCH 18/19] refactor: Remove joyid and update common --- src/common.mol | 3 - src/common.rs | 1379 +------------- src/joyid.mol | 65 - src/joyid.rs | 4714 ------------------------------------------------ src/lib.rs | 1 - 5 files changed, 1 insertion(+), 6161 deletions(-) delete mode 100644 src/joyid.mol delete mode 100644 src/joyid.rs diff --git a/src/common.mol b/src/common.mol index 141ba43..9915e92 100644 --- a/src/common.mol +++ b/src/common.mol @@ -5,9 +5,6 @@ array Uint16 [byte; 2]; array Uint32 [byte; 4]; array Uint64 [byte; 8]; array Byte6 [byte; 6]; -array Byte9 [byte; 9]; -array Byte20 [byte; 20]; -array Byte24 [byte; 24]; array Byte32 [byte; 32]; array CotaId [byte; 20]; diff --git a/src/common.rs b/src/common.rs index c9a9bb4..64bfd3b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,5 @@ // Generated by Molecule 0.7.3 + #![allow(unused_imports)] #![allow(dead_code)] #![allow(clippy::if_same_then_else)] @@ -1024,1384 +1025,6 @@ impl molecule::prelude::Builder for Byte6Builder { } } #[derive(Clone)] -pub struct Byte9(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte9 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte9 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte9::new_unchecked(v.into()) - } -} -impl Byte9 { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte9Reader<'r> { - Byte9Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte9 { - type Builder = Byte9Builder; - - const NAME: &'static str = "Byte9"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte9(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte9Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte9Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte9Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte9Reader<'r> { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte9Reader<'r> { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte9Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte9Builder(pub(crate) [Byte; 9]); -impl ::core::fmt::Debug for Byte9Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte9Builder { - fn default() -> Self { - Byte9Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte9Builder { - pub const ITEM_COUNT: usize = 9; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 9; - - pub fn set(mut self, v: [Byte; 9]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } -} -impl molecule::prelude::Builder for Byte9Builder { - type Entity = Byte9; - - const NAME: &'static str = "Byte9Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte9::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte20(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte20 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte20 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - Byte20::new_unchecked(v.into()) - } -} -impl Byte20 { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte20 { - type Builder = Byte20Builder; - - const NAME: &'static str = "Byte20"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte20(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte20Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte20Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte20Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte20Reader<'r> { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte20Reader<'r> { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte20Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte20Builder(pub(crate) [Byte; 20]); -impl ::core::fmt::Debug for Byte20Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte20Builder { - fn default() -> Self { - Byte20Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte20Builder { - pub const ITEM_COUNT: usize = 20; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; - - pub fn set(mut self, v: [Byte; 20]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } -} -impl molecule::prelude::Builder for Byte20Builder { - type Entity = Byte20; - - const NAME: &'static str = "Byte20Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte20::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Byte24(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte24 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Byte24 { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Byte24::new_unchecked(v.into()) - } -} -impl Byte24 { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) - } - - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) - } - - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) - } - - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - - pub fn as_reader<'r>(&'r self) -> Byte24Reader<'r> { - Byte24Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte24 { - type Builder = Byte24Builder; - - const NAME: &'static str = "Byte24"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte24(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte24Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - self.nth21(), - self.nth22(), - self.nth23(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct Byte24Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte24Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> Byte24Reader<'r> { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) - } - - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) - } - - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) - } - - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for Byte24Reader<'r> { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte24Reader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct Byte24Builder(pub(crate) [Byte; 24]); -impl ::core::fmt::Debug for Byte24Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Byte24Builder { - fn default() -> Self { - Byte24Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Byte24Builder { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; - - pub fn set(mut self, v: [Byte; 24]) -> Self { - self.0 = v; - self - } - - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } - - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; - self - } - - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; - self - } - - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; - self - } -} -impl molecule::prelude::Builder for Byte24Builder { - type Entity = Byte24; - - const NAME: &'static str = "Byte24Builder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - writer.write_all(self.0[21].as_slice())?; - writer.write_all(self.0[22].as_slice())?; - writer.write_all(self.0[23].as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Byte24::new_unchecked(inner.into()) - } -} -#[derive(Clone)] pub struct Byte32(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Byte32 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { diff --git a/src/joyid.mol b/src/joyid.mol deleted file mode 100644 index f3137b4..0000000 --- a/src/joyid.mol +++ /dev/null @@ -1,65 +0,0 @@ -import common; - -vector LockScriptVec ; - -struct SubKey { - smt_type: Uint16, - sub_type: Byte6, - ext_data: Uint32, - reserved: Byte20, -} -struct SubValue { - alg_index: Uint16, - pubkey_hash: Byte20, - reserved: Byte9, - padding: byte, -} -vector SubKeyVec ; -vector SubValueVec; -table SubKeyEntries { - keys: SubKeyVec, - values: SubValueVec, -} - -table SubKeyUnlockEntries { - ext_data: Uint32, - alg_index: Uint16, - subkey_proof: Bytes, -} - - -struct SocialKey { - smt_type: Uint16, - sub_type: Byte6, - reserved: Byte24, -} -table SocialValue { - recovery_mode: byte, - must: byte, - total: byte, - signers: LockScriptVec, -} -vector SocialKeyVec ; -vector SocialValueVec ; -table SocialEntries { - keys: SocialKeyVec, - values: SocialValueVec, -} - - -table FriendPubkey { - unlock_mode: byte, - alg_index: Uint16, - pubkey: Bytes, - signature: Bytes, - - // only for subkey - ext_data: Uint32, - subkey_proof: Bytes, -} -vector FriendPubkeyVec ; -table SocialUnlockEntries { - social_value: SocialValue, - social_proof: Bytes, - social_friends: FriendPubkeyVec, -} diff --git a/src/joyid.rs b/src/joyid.rs deleted file mode 100644 index 3d07aa1..0000000 --- a/src/joyid.rs +++ /dev/null @@ -1,4714 +0,0 @@ -// Generated by Molecule 0.7.3 -#![allow(unused_imports)] -#![allow(dead_code)] -#![allow(clippy::if_same_then_else)] - -use super::ckb_types::prelude::*; -use super::molecule::{self, prelude::*}; -extern crate alloc; -pub use alloc::vec::*; -// these lines above are manually added - -use super::common::*; -use molecule::prelude::*; -#[derive(Clone)] -pub struct LockScriptVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for LockScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for LockScriptVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - LockScriptVec::new_unchecked(v.into()) - } -} -impl LockScriptVec { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> Bytes { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - Bytes::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } - } - - pub fn as_reader<'r>(&'r self) -> LockScriptVecReader<'r> { - LockScriptVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for LockScriptVec { - type Builder = LockScriptVecBuilder; - - const NAME: &'static str = "LockScriptVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - LockScriptVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - LockScriptVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - LockScriptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct LockScriptVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for LockScriptVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> LockScriptVecReader<'r> { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for LockScriptVecReader<'r> { - type Entity = LockScriptVec; - - const NAME: &'static str = "LockScriptVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - LockScriptVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - BytesReader::verify(&slice[start..end], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct LockScriptVecBuilder(pub(crate) Vec); -impl LockScriptVecBuilder { - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: Bytes) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: Bytes) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for LockScriptVecBuilder { - type Entity = LockScriptVec; - - const NAME: &'static str = "LockScriptVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - LockScriptVec::new_unchecked(inner.into()) - } -} -pub struct LockScriptVecIterator(LockScriptVec, usize, usize); -impl ::core::iter::Iterator for LockScriptVecIterator { - type Item = Bytes; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for LockScriptVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for LockScriptVec { - type IntoIter = LockScriptVecIterator; - type Item = Bytes; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - LockScriptVecIterator(self, 0, len) - } -} -impl<'r> LockScriptVecReader<'r> { - pub fn iter<'t>(&'t self) -> LockScriptVecReaderIterator<'t, 'r> { - LockScriptVecReaderIterator(&self, 0, self.len()) - } -} -pub struct LockScriptVecReaderIterator<'t, 'r>(&'t LockScriptVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for LockScriptVecReaderIterator<'t, 'r> { - type Item = BytesReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for LockScriptVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SubKey(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SubKey { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SubKey::new_unchecked(v.into()) - } -} -impl SubKey { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn sub_type(&self) -> Byte6 { - Byte6::new_unchecked(self.0.slice(2..8)) - } - - pub fn ext_data(&self) -> Uint32 { - Uint32::new_unchecked(self.0.slice(8..12)) - } - - pub fn reserved(&self) -> Byte20 { - Byte20::new_unchecked(self.0.slice(12..32)) - } - - pub fn as_reader<'r>(&'r self) -> SubKeyReader<'r> { - SubKeyReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKey { - type Builder = SubKeyBuilder; - - const NAME: &'static str = "SubKey"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKey(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .smt_type(self.smt_type()) - .sub_type(self.sub_type()) - .ext_data(self.ext_data()) - .reserved(self.reserved()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl<'r> SubKeyReader<'r> { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn sub_type(&self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(&self.as_slice()[2..8]) - } - - pub fn ext_data(&self) -> Uint32Reader<'r> { - Uint32Reader::new_unchecked(&self.as_slice()[8..12]) - } - - pub fn reserved(&self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(&self.as_slice()[12..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyReader<'r> { - type Entity = SubKey; - - const NAME: &'static str = "SubKeyReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyBuilder { - pub(crate) smt_type: Uint16, - pub(crate) sub_type: Byte6, - pub(crate) ext_data: Uint32, - pub(crate) reserved: Byte20, -} -impl SubKeyBuilder { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 6, 4, 20]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(mut self, v: Uint16) -> Self { - self.smt_type = v; - self - } - - pub fn sub_type(mut self, v: Byte6) -> Self { - self.sub_type = v; - self - } - - pub fn ext_data(mut self, v: Uint32) -> Self { - self.ext_data = v; - self - } - - pub fn reserved(mut self, v: Byte20) -> Self { - self.reserved = v; - self - } -} -impl molecule::prelude::Builder for SubKeyBuilder { - type Entity = SubKey; - - const NAME: &'static str = "SubKeyBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.smt_type.as_slice())?; - writer.write_all(self.sub_type.as_slice())?; - writer.write_all(self.ext_data.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKey::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubValue(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, ", {}: {}", "padding", self.padding())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SubValue { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SubValue::new_unchecked(v.into()) - } -} -impl SubValue { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn alg_index(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn pubkey_hash(&self) -> Byte20 { - Byte20::new_unchecked(self.0.slice(2..22)) - } - - pub fn reserved(&self) -> Byte9 { - Byte9::new_unchecked(self.0.slice(22..31)) - } - - pub fn padding(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) - } - - pub fn as_reader<'r>(&'r self) -> SubValueReader<'r> { - SubValueReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubValue { - type Builder = SubValueBuilder; - - const NAME: &'static str = "SubValue"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubValue(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .alg_index(self.alg_index()) - .pubkey_hash(self.pubkey_hash()) - .reserved(self.reserved()) - .padding(self.padding()) - } -} -#[derive(Clone, Copy)] -pub struct SubValueReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "pubkey_hash", self.pubkey_hash())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, ", {}: {}", "padding", self.padding())?; - write!(f, " }}") - } -} -impl<'r> SubValueReader<'r> { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn alg_index(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn pubkey_hash(&self) -> Byte20Reader<'r> { - Byte20Reader::new_unchecked(&self.as_slice()[2..22]) - } - - pub fn reserved(&self) -> Byte9Reader<'r> { - Byte9Reader::new_unchecked(&self.as_slice()[22..31]) - } - - pub fn padding(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[31..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubValueReader<'r> { - type Entity = SubValue; - - const NAME: &'static str = "SubValueReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubValueReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubValueBuilder { - pub(crate) alg_index: Uint16, - pub(crate) pubkey_hash: Byte20, - pub(crate) reserved: Byte9, - pub(crate) padding: Byte, -} -impl SubValueBuilder { - pub const FIELD_COUNT: usize = 4; - pub const FIELD_SIZES: [usize; 4] = [2, 20, 9, 1]; - pub const TOTAL_SIZE: usize = 32; - - pub fn alg_index(mut self, v: Uint16) -> Self { - self.alg_index = v; - self - } - - pub fn pubkey_hash(mut self, v: Byte20) -> Self { - self.pubkey_hash = v; - self - } - - pub fn reserved(mut self, v: Byte9) -> Self { - self.reserved = v; - self - } - - pub fn padding(mut self, v: Byte) -> Self { - self.padding = v; - self - } -} -impl molecule::prelude::Builder for SubValueBuilder { - type Entity = SubValue; - - const NAME: &'static str = "SubValueBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.alg_index.as_slice())?; - writer.write_all(self.pubkey_hash.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - writer.write_all(self.padding.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubValue::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubKeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SubKeyVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SubKeyVec::new_unchecked(v.into()) - } -} -impl SubKeyVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubKey { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubKey::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> SubKeyVecReader<'r> { - SubKeyVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyVec { - type Builder = SubKeyVecBuilder; - - const NAME: &'static str = "SubKeyVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SubKeyVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubKeyReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubKeyReader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyVecReader<'r> { - type Entity = SubKeyVec; - - const NAME: &'static str = "SubKeyVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyVecBuilder(pub(crate) Vec); -impl SubKeyVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SubKey) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SubKey) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SubKeyVecBuilder { - type Entity = SubKeyVec; - - const NAME: &'static str = "SubKeyVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyVec::new_unchecked(inner.into()) - } -} -pub struct SubKeyVecIterator(SubKeyVec, usize, usize); -impl ::core::iter::Iterator for SubKeyVecIterator { - type Item = SubKey; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SubKeyVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SubKeyVec { - type IntoIter = SubKeyVecIterator; - type Item = SubKey; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SubKeyVecIterator(self, 0, len) - } -} -impl<'r> SubKeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> SubKeyVecReaderIterator<'t, 'r> { - SubKeyVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SubKeyVecReaderIterator<'t, 'r>(&'t SubKeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SubKeyVecReaderIterator<'t, 'r> { - type Item = SubKeyReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubKeyVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SubValueVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SubValueVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SubValueVec::new_unchecked(v.into()) - } -} -impl SubValueVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubValue { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubValue::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> SubValueVecReader<'r> { - SubValueVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubValueVec { - type Builder = SubValueVecBuilder; - - const NAME: &'static str = "SubValueVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubValueVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SubValueVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SubValueVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SubValueReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SubValueReader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SubValueVecReader<'r> { - type Entity = SubValueVec; - - const NAME: &'static str = "SubValueVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubValueVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubValueVecBuilder(pub(crate) Vec); -impl SubValueVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SubValue) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SubValue) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SubValueVecBuilder { - type Entity = SubValueVec; - - const NAME: &'static str = "SubValueVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubValueVec::new_unchecked(inner.into()) - } -} -pub struct SubValueVecIterator(SubValueVec, usize, usize); -impl ::core::iter::Iterator for SubValueVecIterator { - type Item = SubValue; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SubValueVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SubValueVec { - type IntoIter = SubValueVecIterator; - type Item = SubValue; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SubValueVecIterator(self, 0, len) - } -} -impl<'r> SubValueVecReader<'r> { - pub fn iter<'t>(&'t self) -> SubValueVecReaderIterator<'t, 'r> { - SubValueVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SubValueVecReaderIterator<'t, 'r>(&'t SubValueVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SubValueVecReaderIterator<'t, 'r> { - type Item = SubValueReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SubValueVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SubKeyEntries(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SubKeyEntries { - fn default() -> Self { - let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - SubKeyEntries::new_unchecked(v.into()) - } -} -impl SubKeyEntries { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SubKeyVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SubKeyVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn values(&self) -> SubValueVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SubValueVec::new_unchecked(self.0.slice(start..end)) - } else { - SubValueVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SubKeyEntriesReader<'r> { - SubKeyEntriesReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyEntries { - type Builder = SubKeyEntriesBuilder; - - const NAME: &'static str = "SubKeyEntries"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyEntries(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().keys(self.keys()).values(self.values()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyEntriesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SubKeyEntriesReader<'r> { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SubKeyVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SubKeyVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn values(&self) -> SubValueVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SubValueVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - SubValueVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyEntriesReader<'r> { - type Entity = SubKeyEntries; - - const NAME: &'static str = "SubKeyEntriesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyEntriesReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - SubKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SubValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyEntriesBuilder { - pub(crate) keys: SubKeyVec, - pub(crate) values: SubValueVec, -} -impl SubKeyEntriesBuilder { - pub const FIELD_COUNT: usize = 2; - - pub fn keys(mut self, v: SubKeyVec) -> Self { - self.keys = v; - self - } - - pub fn values(mut self, v: SubValueVec) -> Self { - self.values = v; - self - } -} -impl molecule::prelude::Builder for SubKeyEntriesBuilder { - type Entity = SubKeyEntries; - - const NAME: &'static str = "SubKeyEntriesBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.keys.as_slice().len() - + self.values.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.keys.as_slice().len(); - offsets.push(total_size); - total_size += self.values.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.keys.as_slice())?; - writer.write_all(self.values.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyEntries::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SubKeyUnlockEntries(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SubKeyUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SubKeyUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SubKeyUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SubKeyUnlockEntries { - fn default() -> Self { - let v: Vec = vec![ - 26, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - SubKeyUnlockEntries::new_unchecked(v.into()) - } -} -impl SubKeyUnlockEntries { - pub const FIELD_COUNT: usize = 3; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn ext_data(&self) -> Uint32 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Uint32::new_unchecked(self.0.slice(start..end)) - } - - pub fn alg_index(&self) -> Uint16 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Uint16::new_unchecked(self.0.slice(start..end)) - } - - pub fn subkey_proof(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } else { - Bytes::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SubKeyUnlockEntriesReader<'r> { - SubKeyUnlockEntriesReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SubKeyUnlockEntries { - type Builder = SubKeyUnlockEntriesBuilder; - - const NAME: &'static str = "SubKeyUnlockEntries"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SubKeyUnlockEntries(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyUnlockEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SubKeyUnlockEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .ext_data(self.ext_data()) - .alg_index(self.alg_index()) - .subkey_proof(self.subkey_proof()) - } -} -#[derive(Clone, Copy)] -pub struct SubKeyUnlockEntriesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SubKeyUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SubKeyUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SubKeyUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SubKeyUnlockEntriesReader<'r> { - pub const FIELD_COUNT: usize = 3; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn ext_data(&self) -> Uint32Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Uint32Reader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn alg_index(&self) -> Uint16Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Uint16Reader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn subkey_proof(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } else { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SubKeyUnlockEntriesReader<'r> { - type Entity = SubKeyUnlockEntries; - - const NAME: &'static str = "SubKeyUnlockEntriesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SubKeyUnlockEntriesReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - Uint16Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SubKeyUnlockEntriesBuilder { - pub(crate) ext_data: Uint32, - pub(crate) alg_index: Uint16, - pub(crate) subkey_proof: Bytes, -} -impl SubKeyUnlockEntriesBuilder { - pub const FIELD_COUNT: usize = 3; - - pub fn ext_data(mut self, v: Uint32) -> Self { - self.ext_data = v; - self - } - - pub fn alg_index(mut self, v: Uint16) -> Self { - self.alg_index = v; - self - } - - pub fn subkey_proof(mut self, v: Bytes) -> Self { - self.subkey_proof = v; - self - } -} -impl molecule::prelude::Builder for SubKeyUnlockEntriesBuilder { - type Entity = SubKeyUnlockEntries; - - const NAME: &'static str = "SubKeyUnlockEntriesBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.ext_data.as_slice().len() - + self.alg_index.as_slice().len() - + self.subkey_proof.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.ext_data.as_slice().len(); - offsets.push(total_size); - total_size += self.alg_index.as_slice().len(); - offsets.push(total_size); - total_size += self.subkey_proof.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.ext_data.as_slice())?; - writer.write_all(self.alg_index.as_slice())?; - writer.write_all(self.subkey_proof.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SubKeyUnlockEntries::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialKey(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl ::core::default::Default for SocialKey { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]; - SocialKey::new_unchecked(v.into()) - } -} -impl SocialKey { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) - } - - pub fn sub_type(&self) -> Byte6 { - Byte6::new_unchecked(self.0.slice(2..8)) - } - - pub fn reserved(&self) -> Byte24 { - Byte24::new_unchecked(self.0.slice(8..32)) - } - - pub fn as_reader<'r>(&'r self) -> SocialKeyReader<'r> { - SocialKeyReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialKey { - type Builder = SocialKeyBuilder; - - const NAME: &'static str = "SocialKey"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialKey(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .smt_type(self.smt_type()) - .sub_type(self.sub_type()) - .reserved(self.reserved()) - } -} -#[derive(Clone, Copy)] -pub struct SocialKeyReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialKeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "sub_type", self.sub_type())?; - write!(f, ", {}: {}", "reserved", self.reserved())?; - write!(f, " }}") - } -} -impl<'r> SocialKeyReader<'r> { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) - } - - pub fn sub_type(&self) -> Byte6Reader<'r> { - Byte6Reader::new_unchecked(&self.as_slice()[2..8]) - } - - pub fn reserved(&self) -> Byte24Reader<'r> { - Byte24Reader::new_unchecked(&self.as_slice()[8..32]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialKeyReader<'r> { - type Entity = SocialKey; - - const NAME: &'static str = "SocialKeyReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialKeyReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialKeyBuilder { - pub(crate) smt_type: Uint16, - pub(crate) sub_type: Byte6, - pub(crate) reserved: Byte24, -} -impl SocialKeyBuilder { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 6, 24]; - pub const TOTAL_SIZE: usize = 32; - - pub fn smt_type(mut self, v: Uint16) -> Self { - self.smt_type = v; - self - } - - pub fn sub_type(mut self, v: Byte6) -> Self { - self.sub_type = v; - self - } - - pub fn reserved(mut self, v: Byte24) -> Self { - self.reserved = v; - self - } -} -impl molecule::prelude::Builder for SocialKeyBuilder { - type Entity = SocialKey; - - const NAME: &'static str = "SocialKeyBuilder"; - - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.smt_type.as_slice())?; - writer.write_all(self.sub_type.as_slice())?; - writer.write_all(self.reserved.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialKey::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialValue(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialValue { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; - write!(f, ", {}: {}", "must", self.must())?; - write!(f, ", {}: {}", "total", self.total())?; - write!(f, ", {}: {}", "signers", self.signers())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SocialValue { - fn default() -> Self { - let v: Vec = vec![ - 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - SocialValue::new_unchecked(v.into()) - } -} -impl SocialValue { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn recovery_mode(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn must(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn total(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn signers(&self) -> LockScriptVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - LockScriptVec::new_unchecked(self.0.slice(start..end)) - } else { - LockScriptVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialValueReader<'r> { - SocialValueReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialValue { - type Builder = SocialValueBuilder; - - const NAME: &'static str = "SocialValue"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialValue(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .recovery_mode(self.recovery_mode()) - .must(self.must()) - .total(self.total()) - .signers(self.signers()) - } -} -#[derive(Clone, Copy)] -pub struct SocialValueReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialValueReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "recovery_mode", self.recovery_mode())?; - write!(f, ", {}: {}", "must", self.must())?; - write!(f, ", {}: {}", "total", self.total())?; - write!(f, ", {}: {}", "signers", self.signers())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SocialValueReader<'r> { - pub const FIELD_COUNT: usize = 4; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn recovery_mode(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn must(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn total(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn signers(&self) -> LockScriptVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[20..]) as usize; - LockScriptVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - LockScriptVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialValueReader<'r> { - type Entity = SocialValue; - - const NAME: &'static str = "SocialValueReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialValueReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - ByteReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - LockScriptVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialValueBuilder { - pub(crate) recovery_mode: Byte, - pub(crate) must: Byte, - pub(crate) total: Byte, - pub(crate) signers: LockScriptVec, -} -impl SocialValueBuilder { - pub const FIELD_COUNT: usize = 4; - - pub fn recovery_mode(mut self, v: Byte) -> Self { - self.recovery_mode = v; - self - } - - pub fn must(mut self, v: Byte) -> Self { - self.must = v; - self - } - - pub fn total(mut self, v: Byte) -> Self { - self.total = v; - self - } - - pub fn signers(mut self, v: LockScriptVec) -> Self { - self.signers = v; - self - } -} -impl molecule::prelude::Builder for SocialValueBuilder { - type Entity = SocialValue; - - const NAME: &'static str = "SocialValueBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.recovery_mode.as_slice().len() - + self.must.as_slice().len() - + self.total.as_slice().len() - + self.signers.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.recovery_mode.as_slice().len(); - offsets.push(total_size); - total_size += self.must.as_slice().len(); - offsets.push(total_size); - total_size += self.total.as_slice().len(); - offsets.push(total_size); - total_size += self.signers.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.recovery_mode.as_slice())?; - writer.write_all(self.must.as_slice())?; - writer.write_all(self.total.as_slice())?; - writer.write_all(self.signers.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialValue::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct SocialKeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialKeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SocialKeyVec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - SocialKeyVec::new_unchecked(v.into()) - } -} -impl SocialKeyVec { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SocialKey { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SocialKey::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> SocialKeyVecReader<'r> { - SocialKeyVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialKeyVec { - type Builder = SocialKeyVecBuilder; - - const NAME: &'static str = "SocialKeyVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialKeyVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialKeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SocialKeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialKeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SocialKeyVecReader<'r> { - pub const ITEM_SIZE: usize = 32; - - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() - } - - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SocialKeyReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - SocialKeyReader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialKeyVecReader<'r> { - type Entity = SocialKeyVec; - - const NAME: &'static str = "SocialKeyVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialKeyVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let item_count = molecule::unpack_number(slice) as usize; - if item_count == 0 { - if slice_len != molecule::NUMBER_SIZE { - return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); - } - return Ok(()); - } - let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialKeyVecBuilder(pub(crate) Vec); -impl SocialKeyVecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SocialKey) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SocialKey) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SocialKeyVecBuilder { - type Entity = SocialKeyVec; - - const NAME: &'static str = "SocialKeyVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; - for inner in &self.0[..] { - writer.write_all(inner.as_slice())?; - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialKeyVec::new_unchecked(inner.into()) - } -} -pub struct SocialKeyVecIterator(SocialKeyVec, usize, usize); -impl ::core::iter::Iterator for SocialKeyVecIterator { - type Item = SocialKey; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SocialKeyVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SocialKeyVec { - type IntoIter = SocialKeyVecIterator; - type Item = SocialKey; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SocialKeyVecIterator(self, 0, len) - } -} -impl<'r> SocialKeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> SocialKeyVecReaderIterator<'t, 'r> { - SocialKeyVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SocialKeyVecReaderIterator<'t, 'r>(&'t SocialKeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SocialKeyVecReaderIterator<'t, 'r> { - type Item = SocialKeyReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialKeyVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SocialValueVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialValueVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for SocialValueVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - SocialValueVec::new_unchecked(v.into()) - } -} -impl SocialValueVec { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SocialValue { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SocialValue::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SocialValue::new_unchecked(self.0.slice(start..end)) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialValueVecReader<'r> { - SocialValueVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialValueVec { - type Builder = SocialValueVecBuilder; - - const NAME: &'static str = "SocialValueVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialValueVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialValueVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct SocialValueVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialValueVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> SocialValueVecReader<'r> { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> SocialValueReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - SocialValueReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - SocialValueReader::new_unchecked(&self.as_slice()[start..end]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialValueVecReader<'r> { - type Entity = SocialValueVec; - - const NAME: &'static str = "SocialValueVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialValueVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - SocialValueReader::verify(&slice[start..end], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialValueVecBuilder(pub(crate) Vec); -impl SocialValueVecBuilder { - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: SocialValue) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: SocialValue) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for SocialValueVecBuilder { - type Entity = SocialValueVec; - - const NAME: &'static str = "SocialValueVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialValueVec::new_unchecked(inner.into()) - } -} -pub struct SocialValueVecIterator(SocialValueVec, usize, usize); -impl ::core::iter::Iterator for SocialValueVecIterator { - type Item = SocialValue; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for SocialValueVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for SocialValueVec { - type IntoIter = SocialValueVecIterator; - type Item = SocialValue; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - SocialValueVecIterator(self, 0, len) - } -} -impl<'r> SocialValueVecReader<'r> { - pub fn iter<'t>(&'t self) -> SocialValueVecReaderIterator<'t, 'r> { - SocialValueVecReaderIterator(&self, 0, self.len()) - } -} -pub struct SocialValueVecReaderIterator<'t, 'r>(&'t SocialValueVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for SocialValueVecReaderIterator<'t, 'r> { - type Item = SocialValueReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for SocialValueVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SocialEntries(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SocialEntries { - fn default() -> Self { - let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - SocialEntries::new_unchecked(v.into()) - } -} -impl SocialEntries { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SocialKeyVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SocialKeyVec::new_unchecked(self.0.slice(start..end)) - } - - pub fn values(&self) -> SocialValueVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SocialValueVec::new_unchecked(self.0.slice(start..end)) - } else { - SocialValueVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialEntriesReader<'r> { - SocialEntriesReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialEntries { - type Builder = SocialEntriesBuilder; - - const NAME: &'static str = "SocialEntries"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialEntries(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().keys(self.keys()).values(self.values()) - } -} -#[derive(Clone, Copy)] -pub struct SocialEntriesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "keys", self.keys())?; - write!(f, ", {}: {}", "values", self.values())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SocialEntriesReader<'r> { - pub const FIELD_COUNT: usize = 2; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn keys(&self) -> SocialKeyVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SocialKeyVecReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn values(&self) -> SocialValueVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SocialValueVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - SocialValueVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialEntriesReader<'r> { - type Entity = SocialEntries; - - const NAME: &'static str = "SocialEntriesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialEntriesReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - SocialKeyVecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SocialValueVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialEntriesBuilder { - pub(crate) keys: SocialKeyVec, - pub(crate) values: SocialValueVec, -} -impl SocialEntriesBuilder { - pub const FIELD_COUNT: usize = 2; - - pub fn keys(mut self, v: SocialKeyVec) -> Self { - self.keys = v; - self - } - - pub fn values(mut self, v: SocialValueVec) -> Self { - self.values = v; - self - } -} -impl molecule::prelude::Builder for SocialEntriesBuilder { - type Entity = SocialEntries; - - const NAME: &'static str = "SocialEntriesBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.keys.as_slice().len() - + self.values.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.keys.as_slice().len(); - offsets.push(total_size); - total_size += self.values.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.keys.as_slice())?; - writer.write_all(self.values.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialEntries::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct FriendPubkey(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for FriendPubkey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for FriendPubkey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for FriendPubkey { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "unlock_mode", self.unlock_mode())?; - write!(f, ", {}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "pubkey", self.pubkey())?; - write!(f, ", {}: {}", "signature", self.signature())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for FriendPubkey { - fn default() -> Self { - let v: Vec = vec![ - 47, 0, 0, 0, 28, 0, 0, 0, 29, 0, 0, 0, 31, 0, 0, 0, 35, 0, 0, 0, 39, 0, 0, 0, 43, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - FriendPubkey::new_unchecked(v.into()) - } -} -impl FriendPubkey { - pub const FIELD_COUNT: usize = 6; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn unlock_mode(&self) -> Byte { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Byte::new_unchecked(self.0.slice(start..end)) - } - - pub fn alg_index(&self) -> Uint16 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Uint16::new_unchecked(self.0.slice(start..end)) - } - - pub fn pubkey(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } - - pub fn signature(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - let end = molecule::unpack_number(&slice[20..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } - - pub fn ext_data(&self) -> Uint32 { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[20..]) as usize; - let end = molecule::unpack_number(&slice[24..]) as usize; - Uint32::new_unchecked(self.0.slice(start..end)) - } - - pub fn subkey_proof(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[24..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[28..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } else { - Bytes::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> FriendPubkeyReader<'r> { - FriendPubkeyReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for FriendPubkey { - type Builder = FriendPubkeyBuilder; - - const NAME: &'static str = "FriendPubkey"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - FriendPubkey(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - FriendPubkeyReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - FriendPubkeyReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .unlock_mode(self.unlock_mode()) - .alg_index(self.alg_index()) - .pubkey(self.pubkey()) - .signature(self.signature()) - .ext_data(self.ext_data()) - .subkey_proof(self.subkey_proof()) - } -} -#[derive(Clone, Copy)] -pub struct FriendPubkeyReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for FriendPubkeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for FriendPubkeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for FriendPubkeyReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "unlock_mode", self.unlock_mode())?; - write!(f, ", {}: {}", "alg_index", self.alg_index())?; - write!(f, ", {}: {}", "pubkey", self.pubkey())?; - write!(f, ", {}: {}", "signature", self.signature())?; - write!(f, ", {}: {}", "ext_data", self.ext_data())?; - write!(f, ", {}: {}", "subkey_proof", self.subkey_proof())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> FriendPubkeyReader<'r> { - pub const FIELD_COUNT: usize = 6; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn unlock_mode(&self) -> ByteReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - ByteReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn alg_index(&self) -> Uint16Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Uint16Reader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn pubkey(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - let end = molecule::unpack_number(&slice[16..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn signature(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[16..]) as usize; - let end = molecule::unpack_number(&slice[20..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn ext_data(&self) -> Uint32Reader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[20..]) as usize; - let end = molecule::unpack_number(&slice[24..]) as usize; - Uint32Reader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn subkey_proof(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[24..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[28..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } else { - BytesReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for FriendPubkeyReader<'r> { - type Entity = FriendPubkey; - - const NAME: &'static str = "FriendPubkeyReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - FriendPubkeyReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - ByteReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - Uint16Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?; - Uint32Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?; - BytesReader::verify(&slice[offsets[5]..offsets[6]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct FriendPubkeyBuilder { - pub(crate) unlock_mode: Byte, - pub(crate) alg_index: Uint16, - pub(crate) pubkey: Bytes, - pub(crate) signature: Bytes, - pub(crate) ext_data: Uint32, - pub(crate) subkey_proof: Bytes, -} -impl FriendPubkeyBuilder { - pub const FIELD_COUNT: usize = 6; - - pub fn unlock_mode(mut self, v: Byte) -> Self { - self.unlock_mode = v; - self - } - - pub fn alg_index(mut self, v: Uint16) -> Self { - self.alg_index = v; - self - } - - pub fn pubkey(mut self, v: Bytes) -> Self { - self.pubkey = v; - self - } - - pub fn signature(mut self, v: Bytes) -> Self { - self.signature = v; - self - } - - pub fn ext_data(mut self, v: Uint32) -> Self { - self.ext_data = v; - self - } - - pub fn subkey_proof(mut self, v: Bytes) -> Self { - self.subkey_proof = v; - self - } -} -impl molecule::prelude::Builder for FriendPubkeyBuilder { - type Entity = FriendPubkey; - - const NAME: &'static str = "FriendPubkeyBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.unlock_mode.as_slice().len() - + self.alg_index.as_slice().len() - + self.pubkey.as_slice().len() - + self.signature.as_slice().len() - + self.ext_data.as_slice().len() - + self.subkey_proof.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.unlock_mode.as_slice().len(); - offsets.push(total_size); - total_size += self.alg_index.as_slice().len(); - offsets.push(total_size); - total_size += self.pubkey.as_slice().len(); - offsets.push(total_size); - total_size += self.signature.as_slice().len(); - offsets.push(total_size); - total_size += self.ext_data.as_slice().len(); - offsets.push(total_size); - total_size += self.subkey_proof.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.unlock_mode.as_slice())?; - writer.write_all(self.alg_index.as_slice())?; - writer.write_all(self.pubkey.as_slice())?; - writer.write_all(self.signature.as_slice())?; - writer.write_all(self.ext_data.as_slice())?; - writer.write_all(self.subkey_proof.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - FriendPubkey::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct FriendPubkeyVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for FriendPubkeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for FriendPubkeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for FriendPubkeyVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for FriendPubkeyVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - FriendPubkeyVec::new_unchecked(v.into()) - } -} -impl FriendPubkeyVec { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> FriendPubkey { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - FriendPubkey::new_unchecked(self.0.slice(start..)) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - FriendPubkey::new_unchecked(self.0.slice(start..end)) - } - } - - pub fn as_reader<'r>(&'r self) -> FriendPubkeyVecReader<'r> { - FriendPubkeyVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for FriendPubkeyVec { - type Builder = FriendPubkeyVecBuilder; - - const NAME: &'static str = "FriendPubkeyVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - FriendPubkeyVec(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - FriendPubkeyVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - FriendPubkeyVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct FriendPubkeyVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for FriendPubkeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for FriendPubkeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for FriendPubkeyVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl<'r> FriendPubkeyVecReader<'r> { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn len(&self) -> usize { - self.item_count() - } - - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } - } - - pub fn get_unchecked(&self, idx: usize) -> FriendPubkeyReader<'r> { - let slice = self.as_slice(); - let start_idx = molecule::NUMBER_SIZE * (1 + idx); - let start = molecule::unpack_number(&slice[start_idx..]) as usize; - if idx == self.len() - 1 { - FriendPubkeyReader::new_unchecked(&self.as_slice()[start..]) - } else { - let end_idx = start_idx + molecule::NUMBER_SIZE; - let end = molecule::unpack_number(&slice[end_idx..]) as usize; - FriendPubkeyReader::new_unchecked(&self.as_slice()[start..end]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for FriendPubkeyVecReader<'r> { - type Entity = FriendPubkeyVec; - - const NAME: &'static str = "FriendPubkeyVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - FriendPubkeyVecReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!( - Self, - TotalSizeNotMatch, - molecule::NUMBER_SIZE * 2, - slice_len - ); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - for pair in offsets.windows(2) { - let start = pair[0]; - let end = pair[1]; - FriendPubkeyReader::verify(&slice[start..end], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct FriendPubkeyVecBuilder(pub(crate) Vec); -impl FriendPubkeyVecBuilder { - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: FriendPubkey) -> Self { - self.0.push(v); - self - } - - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } - self - } - - pub fn replace(&mut self, index: usize, v: FriendPubkey) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for FriendPubkeyVecBuilder { - type Entity = FriendPubkeyVec; - - const NAME: &'static str = "FriendPubkeyVecBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (self.0.len() + 1) - + self - .0 - .iter() - .map(|inner| inner.as_slice().len()) - .sum::() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let item_count = self.0.len(); - if item_count == 0 { - writer.write_all(&molecule::pack_number( - molecule::NUMBER_SIZE as molecule::Number, - ))?; - } else { - let (total_size, offsets) = self.0.iter().fold( - ( - molecule::NUMBER_SIZE * (item_count + 1), - Vec::with_capacity(item_count), - ), - |(start, mut offsets), inner| { - offsets.push(start); - (start + inner.as_slice().len(), offsets) - }, - ); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - for inner in self.0.iter() { - writer.write_all(inner.as_slice())?; - } - } - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - FriendPubkeyVec::new_unchecked(inner.into()) - } -} -pub struct FriendPubkeyVecIterator(FriendPubkeyVec, usize, usize); -impl ::core::iter::Iterator for FriendPubkeyVecIterator { - type Item = FriendPubkey; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl ::core::iter::ExactSizeIterator for FriendPubkeyVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for FriendPubkeyVec { - type IntoIter = FriendPubkeyVecIterator; - type Item = FriendPubkey; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - FriendPubkeyVecIterator(self, 0, len) - } -} -impl<'r> FriendPubkeyVecReader<'r> { - pub fn iter<'t>(&'t self) -> FriendPubkeyVecReaderIterator<'t, 'r> { - FriendPubkeyVecReaderIterator(&self, 0, self.len()) - } -} -pub struct FriendPubkeyVecReaderIterator<'t, 'r>(&'t FriendPubkeyVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for FriendPubkeyVecReaderIterator<'t, 'r> { - type Item = FriendPubkeyReader<'t>; - - fn next(&mut self) -> Option { - if self.1 >= self.2 { - None - } else { - let ret = self.0.get_unchecked(self.1); - self.1 += 1; - Some(ret) - } - } -} -impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for FriendPubkeyVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct SocialUnlockEntries(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for SocialUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for SocialUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for SocialUnlockEntries { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "social_value", self.social_value())?; - write!(f, ", {}: {}", "social_proof", self.social_proof())?; - write!(f, ", {}: {}", "social_friends", self.social_friends())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for SocialUnlockEntries { - fn default() -> Self { - let v: Vec = vec![ - 51, 0, 0, 0, 16, 0, 0, 0, 43, 0, 0, 0, 47, 0, 0, 0, 27, 0, 0, 0, 20, 0, 0, 0, 21, 0, 0, - 0, 22, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - SocialUnlockEntries::new_unchecked(v.into()) - } -} -impl SocialUnlockEntries { - pub const FIELD_COUNT: usize = 3; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn social_value(&self) -> SocialValue { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SocialValue::new_unchecked(self.0.slice(start..end)) - } - - pub fn social_proof(&self) -> Bytes { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - Bytes::new_unchecked(self.0.slice(start..end)) - } - - pub fn social_friends(&self) -> FriendPubkeyVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - FriendPubkeyVec::new_unchecked(self.0.slice(start..end)) - } else { - FriendPubkeyVec::new_unchecked(self.0.slice(start..)) - } - } - - pub fn as_reader<'r>(&'r self) -> SocialUnlockEntriesReader<'r> { - SocialUnlockEntriesReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for SocialUnlockEntries { - type Builder = SocialUnlockEntriesBuilder; - - const NAME: &'static str = "SocialUnlockEntries"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - SocialUnlockEntries(data) - } - - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialUnlockEntriesReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - SocialUnlockEntriesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .social_value(self.social_value()) - .social_proof(self.social_proof()) - .social_friends(self.social_friends()) - } -} -#[derive(Clone, Copy)] -pub struct SocialUnlockEntriesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for SocialUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for SocialUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for SocialUnlockEntriesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "social_value", self.social_value())?; - write!(f, ", {}: {}", "social_proof", self.social_proof())?; - write!(f, ", {}: {}", "social_friends", self.social_friends())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> SocialUnlockEntriesReader<'r> { - pub const FIELD_COUNT: usize = 3; - - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - - pub fn social_value(&self) -> SocialValueReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - SocialValueReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn social_proof(&self) -> BytesReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - BytesReader::new_unchecked(&self.as_slice()[start..end]) - } - - pub fn social_friends(&self) -> FriendPubkeyVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - FriendPubkeyVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - FriendPubkeyVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for SocialUnlockEntriesReader<'r> { - type Entity = SocialUnlockEntries; - - const NAME: &'static str = "SocialUnlockEntriesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - SocialUnlockEntriesReader(slice) - } - - fn as_slice(&self) -> &'r [u8] { - self.0 - } - - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - SocialValueReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - FriendPubkeyVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct SocialUnlockEntriesBuilder { - pub(crate) social_value: SocialValue, - pub(crate) social_proof: Bytes, - pub(crate) social_friends: FriendPubkeyVec, -} -impl SocialUnlockEntriesBuilder { - pub const FIELD_COUNT: usize = 3; - - pub fn social_value(mut self, v: SocialValue) -> Self { - self.social_value = v; - self - } - - pub fn social_proof(mut self, v: Bytes) -> Self { - self.social_proof = v; - self - } - - pub fn social_friends(mut self, v: FriendPubkeyVec) -> Self { - self.social_friends = v; - self - } -} -impl molecule::prelude::Builder for SocialUnlockEntriesBuilder { - type Entity = SocialUnlockEntries; - - const NAME: &'static str = "SocialUnlockEntriesBuilder"; - - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.social_value.as_slice().len() - + self.social_proof.as_slice().len() - + self.social_friends.as_slice().len() - } - - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.social_value.as_slice().len(); - offsets.push(total_size); - total_size += self.social_proof.as_slice().len(); - offsets.push(total_size); - total_size += self.social_friends.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.social_value.as_slice())?; - writer.write_all(self.social_proof.as_slice())?; - writer.write_all(self.social_friends.as_slice())?; - Ok(()) - } - - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - SocialUnlockEntries::new_unchecked(inner.into()) - } -} diff --git a/src/lib.rs b/src/lib.rs index c24d86c..70b7206 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ pub mod common; pub mod define; pub mod extension; -pub mod joyid; pub mod mint; pub mod registry; pub mod smt; From 4595847e94c3d2dedb5b4d0e8f118e230f937ee3 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Tue, 1 Nov 2022 17:12:23 +0800 Subject: [PATCH 19/19] chore: Bump to v0.7.0 --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7c6829d..babcf35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,9 +191,9 @@ dependencies = [ [[package]] name = "ckb-std" -version = "0.10.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47a6ad40455c446ad6fbb303dae24827fc309f43558f59d1f1b863a9de3e9f81" +checksum = "e5761f11372e794d77b8e8a8059b22c5d6ccef907588dd5302a669f056ecce6b" dependencies = [ "buddy-alloc", "cc", @@ -225,7 +225,7 @@ dependencies = [ [[package]] name = "cota-smt" -version = "0.6.0" +version = "0.7.0" dependencies = [ "blake2b-ref 0.3.1", "cfg-if 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index b952479..aba5d6a 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "cota-smt" -version = "0.6.0" +version = "0.7.0" edition = "2018" [dependencies] cfg-if = "1.0" blake2b-ref = "0.3.1" -ckb-std = {version = "0.10.0", optional = true} +ckb-std = {version = "0.9.0", optional = true} ckb-types = {version = "0.101.1", optional = true} molecule = {version = "0.7.2", default_features = false, optional = true} sparse-merkle-tree = {version = "0.5.3", default_features = false}