diff --git a/Cargo.lock b/Cargo.lock index dd744a0..494a3c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,7 +225,7 @@ dependencies = [ [[package]] name = "cota-smt" -version = "0.4.0" +version = "0.5.0" dependencies = [ "blake2b-ref 0.3.0", "cfg-if 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index 7b0eeff..1eabb44 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cota-smt" -version = "0.4.0" +version = "0.5.0" edition = "2018" [dependencies] diff --git a/src/common.mol b/src/common.mol index 4b6d871..30fe590 100644 --- a/src/common.mol +++ b/src/common.mol @@ -1,15 +1,19 @@ -// Uint16/Uint32: big endian +// The `UintN` is used to store a `N` bits unsigned integer +// as a byte array in big endian. -array Byte32 [byte; 32]; array Uint16 [byte; 2]; array Uint32 [byte; 4]; array Uint64 [byte; 8]; -vector Bytes ; +array Byte32 [byte; 32]; array CotaId [byte; 20]; array Characteristic [byte; 20]; array OutPointSlice [byte; 24]; +vector Bytes ; +vector Uint32Vec ; +vector Byte32Vec ; + struct CotaNFTId { smt_type: Uint16, cota_id: CotaId, @@ -22,6 +26,16 @@ struct CotaNFTInfo { characteristic: Characteristic, } +table MerkleProof { + indices: Uint32Vec, + lemmas: Byte32Vec, +} + +table TransactionProof { + witnesses_root: Byte32, + proof: MerkleProof, +} + struct DefineCotaNFTId { smt_type: Uint16, cota_id: CotaId, diff --git a/src/common.rs b/src/common.rs index d39fc43..631fe2e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -10,10 +10,9 @@ extern crate alloc; pub use alloc::vec::*; // these lines above are manually added -use molecule::prelude::*; #[derive(Clone)] -pub struct Byte32(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte32 { +pub struct Uint16(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint16 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -22,31 +21,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 Uint16 { 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 Uint16 { 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 Uint16 { 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]; + Uint16::new_unchecked(v.into()) } } -impl Byte32 { - pub const ITEM_COUNT: usize = 32; +impl Uint16 { + pub const ITEM_COUNT: usize = 2; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 2; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -56,141 +52,231 @@ impl Byte32 { Byte::new_unchecked(self.0.slice(1..2)) } - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() } - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) + pub fn as_reader<'r>(&'r self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for Uint16 { + type Builder = Uint16Builder; - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } + const NAME: &'static str = "Uint16"; - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint16(data) } - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint16Reader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint16Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([self.nth0(), self.nth1()]) } - - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) +} +#[derive(Clone, Copy)] +pub struct Uint16Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint16Reader<'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())) } - - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) +} +impl<'r> ::core::fmt::Debug for Uint16Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) } - - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) +} +impl<'r> ::core::fmt::Display for Uint16Reader<'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> Uint16Reader<'r> { + pub const ITEM_COUNT: usize = 2; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 2; - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) } - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) } - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() } +} +impl<'r> molecule::prelude::Reader<'r> for Uint16Reader<'r> { + type Entity = Uint16; - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) + const NAME: &'static str = "Uint16Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint16Reader(slice) } - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) + 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 Uint16Builder(pub(crate) [Byte; 2]); +impl ::core::fmt::Debug for Uint16Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) } +} +impl ::core::default::Default for Uint16Builder { + fn default() -> Self { + Uint16Builder([Byte::default(), Byte::default()]) + } +} +impl Uint16Builder { + pub const ITEM_COUNT: usize = 2; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 2; - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) + pub fn set(mut self, v: [Byte; 2]) -> Self { + self.0 = v; + self } - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self } - pub fn nth24(&self) -> Byte { - Byte::new_unchecked(self.0.slice(24..25)) + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self } +} +impl molecule::prelude::Builder for Uint16Builder { + type Entity = Uint16; - pub fn nth25(&self) -> Byte { - Byte::new_unchecked(self.0.slice(25..26)) + const NAME: &'static str = "Uint16Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE } - pub fn nth26(&self) -> Byte { - Byte::new_unchecked(self.0.slice(26..27)) + 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())?; + Ok(()) } - pub fn nth27(&self) -> Byte { - Byte::new_unchecked(self.0.slice(27..28)) + 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)); + Uint16::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Uint32(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint32 { + 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 Uint32 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint32 { + 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 Uint32 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Uint32::new_unchecked(v.into()) } +} +impl Uint32 { + pub const ITEM_COUNT: usize = 4; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 4; - pub fn nth28(&self) -> Byte { - Byte::new_unchecked(self.0.slice(28..29)) + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) } - pub fn nth29(&self) -> Byte { - Byte::new_unchecked(self.0.slice(29..30)) + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) } - pub fn nth30(&self) -> Byte { - Byte::new_unchecked(self.0.slice(30..31)) + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) } - pub fn nth31(&self) -> Byte { - Byte::new_unchecked(self.0.slice(31..32)) + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) } 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) -> Uint32Reader<'r> { + Uint32Reader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Byte32 { - type Builder = Byte32Builder; +impl molecule::prelude::Entity for Uint32 { + type Builder = Uint32Builder; - const NAME: &'static str = "Byte32"; + const NAME: &'static str = "Uint32"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte32(data) + Uint32(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -202,11 +288,11 @@ impl molecule::prelude::Entity for Byte32 { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32Reader::from_slice(slice).map(|reader| reader.to_entity()) + Uint32Reader::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()) + Uint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -214,45 +300,12 @@ impl molecule::prelude::Entity for Byte32 { } 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::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()]) } } #[derive(Clone, Copy)] -pub struct Byte32Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> { +pub struct Uint32Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint32Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -261,22 +314,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 Uint32Reader<'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 Uint32Reader<'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> Uint32Reader<'r> { + pub const ITEM_COUNT: usize = 4; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 4; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -294,133 +347,21 @@ impl<'r> Byte32Reader<'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; +impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> { + type Entity = Uint32; - const NAME: &'static str = "Byte32Reader"; + const NAME: &'static str = "Uint32Reader"; 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) + Uint32Reader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -436,43 +377,15 @@ 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 Uint32Builder(pub(crate) [Byte; 4]); +impl ::core::fmt::Debug for Uint32Builder { 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 Uint32Builder { 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(), + Uint32Builder([ Byte::default(), Byte::default(), Byte::default(), @@ -480,12 +393,12 @@ impl ::core::default::Default for Byte32Builder { ]) } } -impl Byte32Builder { - pub const ITEM_COUNT: usize = 32; +impl Uint32Builder { + pub const ITEM_COUNT: usize = 4; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 32; + pub const TOTAL_SIZE: usize = 4; - pub fn set(mut self, v: [Byte; 32]) -> Self { + pub fn set(mut self, v: [Byte; 4]) -> Self { self.0 = v; self } @@ -509,202 +422,34 @@ impl Byte32Builder { self.0[3] = v; self } +} +impl molecule::prelude::Builder for Uint32Builder { + type Entity = Uint32; - 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 - } + const NAME: &'static str = "Uint32Builder"; - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE } - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self + 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())?; + Ok(()) } - 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()) + 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)); + Uint32::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct Uint16(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Uint16 { +pub struct Uint64(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint64 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -713,28 +458,28 @@ impl ::core::fmt::LowerHex for Uint16 { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Uint16 { +impl ::core::fmt::Debug for Uint64 { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Uint16 { +impl ::core::fmt::Display for Uint64 { 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 Uint16 { +impl ::core::default::Default for Uint64 { fn default() -> Self { - let v: Vec = vec![0, 0]; - Uint16::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0]; + Uint64::new_unchecked(v.into()) } } -impl Uint16 { - pub const ITEM_COUNT: usize = 2; +impl Uint64 { + pub const ITEM_COUNT: usize = 8; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 2; + pub const TOTAL_SIZE: usize = 8; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -744,21 +489,45 @@ impl Uint16 { 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 raw_data(&self) -> molecule::bytes::Bytes { self.as_bytes() } - pub fn as_reader<'r>(&'r self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> Uint64Reader<'r> { + Uint64Reader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Uint16 { - type Builder = Uint16Builder; +impl molecule::prelude::Entity for Uint64 { + type Builder = Uint64Builder; - const NAME: &'static str = "Uint16"; + const NAME: &'static str = "Uint64"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Uint16(data) + Uint64(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -770,11 +539,11 @@ impl molecule::prelude::Entity for Uint16 { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint16Reader::from_slice(slice).map(|reader| reader.to_entity()) + Uint64Reader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint16Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -782,12 +551,21 @@ impl molecule::prelude::Entity for Uint16 { } fn as_builder(self) -> Self::Builder { - Self::new_builder().set([self.nth0(), self.nth1()]) + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + ]) } } #[derive(Clone, Copy)] -pub struct Uint16Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Uint16Reader<'r> { +pub struct Uint64Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -796,22 +574,22 @@ impl<'r> ::core::fmt::LowerHex for Uint16Reader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for Uint16Reader<'r> { +impl<'r> ::core::fmt::Debug for Uint64Reader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for Uint16Reader<'r> { +impl<'r> ::core::fmt::Display for Uint64Reader<'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> Uint16Reader<'r> { - pub const ITEM_COUNT: usize = 2; +impl<'r> Uint64Reader<'r> { + pub const ITEM_COUNT: usize = 8; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 2; + pub const TOTAL_SIZE: usize = 8; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -821,21 +599,45 @@ impl<'r> Uint16Reader<'r> { ByteReader::new_unchecked(&self.as_slice()[1..2]) } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) } -} -impl<'r> molecule::prelude::Reader<'r> for Uint16Reader<'r> { - type Entity = Uint16; - const NAME: &'static str = "Uint16Reader"; + 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 raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> { + type Entity = Uint64; + + const NAME: &'static str = "Uint64Reader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - Uint16Reader(slice) + Uint64Reader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -851,23 +653,32 @@ impl<'r> molecule::prelude::Reader<'r> for Uint16Reader<'r> { Ok(()) } } -pub struct Uint16Builder(pub(crate) [Byte; 2]); -impl ::core::fmt::Debug for Uint16Builder { +pub struct Uint64Builder(pub(crate) [Byte; 8]); +impl ::core::fmt::Debug for Uint64Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:?})", Self::NAME, &self.0[..]) } } -impl ::core::default::Default for Uint16Builder { +impl ::core::default::Default for Uint64Builder { fn default() -> Self { - Uint16Builder([Byte::default(), Byte::default()]) + Uint64Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) } } -impl Uint16Builder { - pub const ITEM_COUNT: usize = 2; +impl Uint64Builder { + pub const ITEM_COUNT: usize = 8; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 2; + pub const TOTAL_SIZE: usize = 8; - pub fn set(mut self, v: [Byte; 2]) -> Self { + pub fn set(mut self, v: [Byte; 8]) -> Self { self.0 = v; self } @@ -881,11 +692,41 @@ impl Uint16Builder { 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 + } } -impl molecule::prelude::Builder for Uint16Builder { - type Entity = Uint16; +impl molecule::prelude::Builder for Uint64Builder { + type Entity = Uint64; - const NAME: &'static str = "Uint16Builder"; + const NAME: &'static str = "Uint64Builder"; fn expected_length(&self) -> usize { Self::TOTAL_SIZE @@ -894,6 +735,12 @@ impl molecule::prelude::Builder for Uint16Builder { 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())?; Ok(()) } @@ -901,12 +748,12 @@ impl molecule::prelude::Builder for Uint16Builder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Uint16::new_unchecked(inner.into()) + Uint64::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct Uint32(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Uint32 { +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() { @@ -915,28 +762,31 @@ impl ::core::fmt::LowerHex for Uint32 { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Uint32 { +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 Uint32 { +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 Uint32 { +impl ::core::default::Default for Byte32 { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Uint32::new_unchecked(v.into()) + 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 Uint32 { - pub const ITEM_COUNT: usize = 4; +impl Byte32 { + pub const ITEM_COUNT: usize = 32; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 4; + pub const TOTAL_SIZE: usize = 32; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -954,272 +804,133 @@ impl Uint32 { Byte::new_unchecked(self.0.slice(3..4)) } - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) } - pub fn as_reader<'r>(&'r self) -> Uint32Reader<'r> { - Uint32Reader::new_unchecked(self.as_slice()) + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) } -} -impl molecule::prelude::Entity for Uint32 { - type Builder = Uint32Builder; - const NAME: &'static str = "Uint32"; + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Uint32(data) + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) } - fn as_slice(&self) -> &[u8] { - &self.0[..] + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint32Reader::from_slice(slice).map(|reader| reader.to_entity()) + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) } - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()]) + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) } -} -#[derive(Clone, Copy)] -pub struct Uint32Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Uint32Reader<'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())) + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) } -} -impl<'r> ::core::fmt::Debug for Uint32Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) } -} -impl<'r> ::core::fmt::Display for Uint32Reader<'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) + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) } -} -impl<'r> Uint32Reader<'r> { - pub const ITEM_COUNT: usize = 4; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 4; - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) } - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) } - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) } - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) } -} -impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> { - type Entity = Uint32; - const NAME: &'static str = "Uint32Reader"; + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) } - fn new_unchecked(slice: &'r [u8]) -> Self { - Uint32Reader(slice) + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) } - fn as_slice(&self) -> &'r [u8] { - self.0 + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) } - 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 nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) } -} -pub struct Uint32Builder(pub(crate) [Byte; 4]); -impl ::core::fmt::Debug for Uint32Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for Uint32Builder { - fn default() -> Self { - Uint32Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl Uint32Builder { - pub const ITEM_COUNT: usize = 4; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 4; - - pub fn set(mut self, v: [Byte; 4]) -> 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 - } -} -impl molecule::prelude::Builder for Uint32Builder { - type Entity = Uint32; - - const NAME: &'static str = "Uint32Builder"; - - 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())?; - 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)); - Uint32::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Uint64(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Uint64 { - 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 Uint64 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Uint64 { - 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 Uint64 { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0]; - Uint64::new_unchecked(v.into()) - } -} -impl Uint64 { - pub const ITEM_COUNT: usize = 8; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 8; - - 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 nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) } - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) } - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) } - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) } - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) + 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) -> Uint64Reader<'r> { - Uint64Reader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> { + Byte32Reader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for Uint64 { - type Builder = Uint64Builder; +impl molecule::prelude::Entity for Byte32 { + type Builder = Byte32Builder; - const NAME: &'static str = "Uint64"; + const NAME: &'static str = "Byte32"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Uint64(data) + Byte32(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -1231,11 +942,11 @@ impl molecule::prelude::Entity for Uint64 { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint64Reader::from_slice(slice).map(|reader| reader.to_entity()) + Byte32Reader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -1252,12 +963,36 @@ impl molecule::prelude::Entity for Uint64 { 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 Uint64Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> { +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() { @@ -1266,22 +1001,22 @@ impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for Uint64Reader<'r> { +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 Uint64Reader<'r> { +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> Uint64Reader<'r> { - pub const ITEM_COUNT: usize = 8; +impl<'r> Byte32Reader<'r> { + pub const ITEM_COUNT: usize = 32; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 8; + pub const TOTAL_SIZE: usize = 32; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -1315,74 +1050,762 @@ impl<'r> Uint64Reader<'r> { ByteReader::new_unchecked(&self.as_slice()[7..8]) } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) } -} -impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> { - type Entity = Uint64; - const NAME: &'static str = "Uint64Reader"; + 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 { - Uint64Reader(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 Uint64Builder(pub(crate) [Byte; 8]); -impl ::core::fmt::Debug for Uint64Builder { - 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 Uint64Builder { - fn default() -> Self { - Uint64Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) } -} -impl Uint64Builder { - pub const ITEM_COUNT: usize = 8; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 8; - pub fn set(mut self, v: [Byte; 8]) -> 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 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 CotaId(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for CotaId { + 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 CotaId { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for CotaId { + 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 CotaId { + 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]; + CotaId::new_unchecked(v.into()) + } +} +impl CotaId { + 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) -> CotaIdReader<'r> { + CotaIdReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for CotaId { + type Builder = CotaIdBuilder; + + const NAME: &'static str = "CotaId"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + CotaId(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 { + CotaIdReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CotaIdReader::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 CotaIdReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for CotaIdReader<'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 CotaIdReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for CotaIdReader<'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> CotaIdReader<'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 CotaIdReader<'r> { + type Entity = CotaId; + + const NAME: &'static str = "CotaIdReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + CotaIdReader(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 CotaIdBuilder(pub(crate) [Byte; 20]); +impl ::core::fmt::Debug for CotaIdBuilder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for CotaIdBuilder { + fn default() -> Self { + CotaIdBuilder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + 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 CotaIdBuilder { + 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 { @@ -1390,62 +1813,310 @@ impl Uint64Builder { self } - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = 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 CotaIdBuilder { + type Entity = CotaId; + + const NAME: &'static str = "CotaIdBuilder"; + + 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)); + CotaId::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Characteristic(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Characteristic { + 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 Characteristic { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Characteristic { + 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 Characteristic { + 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]; + Characteristic::new_unchecked(v.into()) + } +} +impl Characteristic { + 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 nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) } - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) } - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() } - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self + pub fn as_reader<'r>(&'r self) -> CharacteristicReader<'r> { + CharacteristicReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Builder for Uint64Builder { - type Entity = Uint64; +impl molecule::prelude::Entity for Characteristic { + type Builder = CharacteristicBuilder; - const NAME: &'static str = "Uint64Builder"; + const NAME: &'static str = "Characteristic"; - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Characteristic(data) } - 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())?; - Ok(()) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - 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)); - Uint64::new_unchecked(inner.into()) + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CharacteristicReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CharacteristicReader::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)] -pub struct Bytes(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Bytes { +#[derive(Clone, Copy)] +pub struct CharacteristicReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for CharacteristicReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1454,237 +2125,305 @@ impl ::core::fmt::LowerHex for Bytes { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Bytes { +impl<'r> ::core::fmt::Debug for CharacteristicReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Bytes { +impl<'r> ::core::fmt::Display for CharacteristicReader<'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 ::core::default::Default for Bytes { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Bytes::new_unchecked(v.into()) - } -} -impl Bytes { +impl<'r> CharacteristicReader<'r> { + pub const ITEM_COUNT: usize = 20; pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) } - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) } - pub fn len(&self) -> usize { - self.item_count() + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) } - pub fn is_empty(&self) -> bool { - self.len() == 0 + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) } - pub fn get(&self, idx: usize) -> Option { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) } - pub fn get_unchecked(&self, idx: usize) -> Byte { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Byte::new_unchecked(self.0.slice(start..end)) + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) } - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.0.slice(molecule::NUMBER_SIZE..) + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) } - pub fn as_reader<'r>(&'r self) -> BytesReader<'r> { - BytesReader::new_unchecked(self.as_slice()) + 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 CharacteristicReader<'r> { + type Entity = Characteristic; + + const NAME: &'static str = "CharacteristicReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + CharacteristicReader(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 CharacteristicBuilder(pub(crate) [Byte; 20]); +impl ::core::fmt::Debug for CharacteristicBuilder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for CharacteristicBuilder { + fn default() -> Self { + CharacteristicBuilder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + 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 molecule::prelude::Entity for Bytes { - type Builder = BytesBuilder; - - const NAME: &'static str = "Bytes"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Bytes(data) - } +impl CharacteristicBuilder { + pub const ITEM_COUNT: usize = 20; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 20; - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() + pub fn set(mut self, v: [Byte; 20]) -> Self { + self.0 = v; + self } - fn as_slice(&self) -> &[u8] { - &self.0[..] + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - BytesReader::from_slice(slice).map(|reader| reader.to_entity()) + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - BytesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self } - fn as_builder(self) -> Self::Builder { - Self::new_builder().extend(self.into_iter()) - } -} -#[derive(Clone, Copy)] -pub struct BytesReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for BytesReader<'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 BytesReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for BytesReader<'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) + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self } -} -impl<'r> BytesReader<'r> { - pub const ITEM_SIZE: usize = 1; - pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self } - pub fn item_count(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self } - pub fn len(&self) -> usize { - self.item_count() + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self } - pub fn is_empty(&self) -> bool { - self.len() == 0 + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self } - pub fn get(&self, idx: usize) -> Option> { - if idx >= self.len() { - None - } else { - Some(self.get_unchecked(idx)) - } + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self } - pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - ByteReader::new_unchecked(&self.as_slice()[start..end]) + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self } - pub fn raw_data(&self) -> &'r [u8] { - &self.as_slice()[molecule::NUMBER_SIZE..] + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self } -} -impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> { - type Entity = Bytes; - const NAME: &'static str = "BytesReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self } - fn new_unchecked(slice: &'r [u8]) -> Self { - BytesReader(slice) + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self } - fn as_slice(&self) -> &'r [u8] { - self.0 + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self } - 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(()) + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self } -} -#[derive(Debug, Default)] -pub struct BytesBuilder(pub(crate) Vec); -impl BytesBuilder { - pub const ITEM_SIZE: usize = 1; - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; self } - pub fn push(mut self, v: Byte) -> Self { - self.0.push(v); + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; self } - pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; self } - pub fn replace(&mut self, index: usize, v: Byte) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self } } -impl molecule::prelude::Builder for BytesBuilder { - type Entity = Bytes; +impl molecule::prelude::Builder for CharacteristicBuilder { + type Entity = Characteristic; - const NAME: &'static str = "BytesBuilder"; + const NAME: &'static str = "CharacteristicBuilder"; fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + Self::TOTAL_SIZE } 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())?; - } + 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(()) } @@ -1692,40 +2431,12 @@ impl molecule::prelude::Builder for BytesBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Bytes::new_unchecked(inner.into()) - } -} -pub struct BytesIterator(Bytes, usize, usize); -impl ::core::iter::Iterator for BytesIterator { - type Item = Byte; - - 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 BytesIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for Bytes { - type IntoIter = BytesIterator; - type Item = Byte; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - BytesIterator(self, 0, len) + Characteristic::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct CotaId(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for CotaId { +pub struct OutPointSlice(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for OutPointSlice { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1734,28 +2445,30 @@ impl ::core::fmt::LowerHex for CotaId { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for CotaId { +impl ::core::fmt::Debug for OutPointSlice { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for CotaId { +impl ::core::fmt::Display for OutPointSlice { 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 CotaId { +impl ::core::default::Default for OutPointSlice { 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]; - CotaId::new_unchecked(v.into()) + 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, + ]; + OutPointSlice::new_unchecked(v.into()) } } -impl CotaId { - pub const ITEM_COUNT: usize = 20; +impl OutPointSlice { + pub const ITEM_COUNT: usize = 24; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; + pub const TOTAL_SIZE: usize = 24; pub fn nth0(&self) -> Byte { Byte::new_unchecked(self.0.slice(0..1)) @@ -1837,21 +2550,37 @@ impl CotaId { 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) -> CotaIdReader<'r> { - CotaIdReader::new_unchecked(self.as_slice()) + pub fn as_reader<'r>(&'r self) -> OutPointSliceReader<'r> { + OutPointSliceReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for CotaId { - type Builder = CotaIdBuilder; +impl molecule::prelude::Entity for OutPointSlice { + type Builder = OutPointSliceBuilder; - const NAME: &'static str = "CotaId"; + const NAME: &'static str = "OutPointSlice"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - CotaId(data) + OutPointSlice(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -1863,11 +2592,11 @@ impl molecule::prelude::Entity for CotaId { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaIdReader::from_slice(slice).map(|reader| reader.to_entity()) + OutPointSliceReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaIdReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + OutPointSliceReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -1896,12 +2625,16 @@ impl molecule::prelude::Entity for CotaId { self.nth17(), self.nth18(), self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), ]) } } #[derive(Clone, Copy)] -pub struct CotaIdReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for CotaIdReader<'r> { +pub struct OutPointSliceReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for OutPointSliceReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -1910,22 +2643,22 @@ impl<'r> ::core::fmt::LowerHex for CotaIdReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for CotaIdReader<'r> { +impl<'r> ::core::fmt::Debug for OutPointSliceReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for CotaIdReader<'r> { +impl<'r> ::core::fmt::Display for OutPointSliceReader<'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> CotaIdReader<'r> { - pub const ITEM_COUNT: usize = 20; +impl<'r> OutPointSliceReader<'r> { + pub const ITEM_COUNT: usize = 24; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; + pub const TOTAL_SIZE: usize = 24; pub fn nth0(&self) -> ByteReader<'r> { ByteReader::new_unchecked(&self.as_slice()[0..1]) @@ -2007,21 +2740,37 @@ impl<'r> CotaIdReader<'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 CotaIdReader<'r> { - type Entity = CotaId; +impl<'r> molecule::prelude::Reader<'r> for OutPointSliceReader<'r> { + type Entity = OutPointSlice; - const NAME: &'static str = "CotaIdReader"; + const NAME: &'static str = "OutPointSliceReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - CotaIdReader(slice) + OutPointSliceReader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -2037,15 +2786,19 @@ impl<'r> molecule::prelude::Reader<'r> for CotaIdReader<'r> { Ok(()) } } -pub struct CotaIdBuilder(pub(crate) [Byte; 20]); -impl ::core::fmt::Debug for CotaIdBuilder { +pub struct OutPointSliceBuilder(pub(crate) [Byte; 24]); +impl ::core::fmt::Debug for OutPointSliceBuilder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:?})", Self::NAME, &self.0[..]) } } -impl ::core::default::Default for CotaIdBuilder { +impl ::core::default::Default for OutPointSliceBuilder { fn default() -> Self { - CotaIdBuilder([ + OutPointSliceBuilder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), Byte::default(), Byte::default(), Byte::default(), @@ -2069,12 +2822,12 @@ impl ::core::default::Default for CotaIdBuilder { ]) } } -impl CotaIdBuilder { - pub const ITEM_COUNT: usize = 20; +impl OutPointSliceBuilder { + pub const ITEM_COUNT: usize = 24; pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 20; + pub const TOTAL_SIZE: usize = 24; - pub fn set(mut self, v: [Byte; 20]) -> Self { + pub fn set(mut self, v: [Byte; 24]) -> Self { self.0 = v; self } @@ -2178,50 +2931,177 @@ impl CotaIdBuilder { 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 CotaIdBuilder { - type Entity = CotaId; +impl molecule::prelude::Builder for OutPointSliceBuilder { + type Entity = OutPointSlice; - const NAME: &'static str = "CotaIdBuilder"; + const NAME: &'static str = "OutPointSliceBuilder"; 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 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)); + OutPointSlice::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct Bytes(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Bytes { + 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 Bytes { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Bytes { + 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 Bytes { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Bytes::new_unchecked(v.into()) + } +} +impl Bytes { + pub const ITEM_SIZE: usize = 1; + + 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) -> Byte { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte::new_unchecked(self.0.slice(start..end)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.0.slice(molecule::NUMBER_SIZE..) + } + + pub fn as_reader<'r>(&'r self) -> BytesReader<'r> { + BytesReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Bytes { + type Builder = BytesBuilder; + + const NAME: &'static str = "Bytes"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Bytes(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 { + BytesReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + BytesReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - 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)); - CotaId::new_unchecked(inner.into()) + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) } } -#[derive(Clone)] -pub struct Characteristic(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Characteristic { +#[derive(Clone, Copy)] +pub struct BytesReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for BytesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -2230,174 +3110,175 @@ impl ::core::fmt::LowerHex for Characteristic { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for Characteristic { +impl<'r> ::core::fmt::Debug for BytesReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for Characteristic { +impl<'r> ::core::fmt::Display for BytesReader<'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 ::core::default::Default for Characteristic { - 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]; - Characteristic::new_unchecked(v.into()) - } -} -impl Characteristic { - pub const ITEM_COUNT: usize = 20; +impl<'r> BytesReader<'r> { 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 total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) + pub fn len(&self) -> usize { + self.item_count() } - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) + pub fn is_empty(&self) -> bool { + self.len() == 0 } - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } } - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) + pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + ByteReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) + pub fn raw_data(&self) -> &'r [u8] { + &self.as_slice()[molecule::NUMBER_SIZE..] } +} +impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> { + type Entity = Bytes; - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } + const NAME: &'static str = "BytesReader"; - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) + fn new_unchecked(slice: &'r [u8]) -> Self { + BytesReader(slice) } - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) + 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 BytesBuilder(pub(crate) Vec); +impl BytesBuilder { + pub const ITEM_SIZE: usize = 1; - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self } - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) + pub fn push(mut self, v: Byte) -> Self { + self.0.push(v); + self } - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self } - pub fn as_reader<'r>(&'r self) -> CharacteristicReader<'r> { - CharacteristicReader::new_unchecked(self.as_slice()) + pub fn replace(&mut self, index: usize, v: Byte) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Entity for Characteristic { - type Builder = CharacteristicBuilder; - - const NAME: &'static str = "Characteristic"; +impl molecule::prelude::Builder for BytesBuilder { + type Entity = Bytes; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Characteristic(data) - } + const NAME: &'static str = "BytesBuilder"; - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn as_slice(&self) -> &[u8] { - &self.0[..] + 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 from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CharacteristicReader::from_slice(slice).map(|reader| reader.to_entity()) + 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)); + Bytes::new_unchecked(inner.into()) } +} +pub struct BytesIterator(Bytes, usize, usize); +impl ::core::iter::Iterator for BytesIterator { + type Item = Byte; - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CharacteristicReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + 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) + } } - - fn new_builder() -> Self::Builder { - ::core::default::Default::default() +} +impl ::core::iter::ExactSizeIterator for BytesIterator { + fn len(&self) -> usize { + self.2 - self.1 } +} +impl ::core::iter::IntoIterator for Bytes { + type IntoIter = BytesIterator; + type Item = Byte; - 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(), - ]) + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + BytesIterator(self, 0, len) } } -#[derive(Clone, Copy)] -pub struct CharacteristicReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for CharacteristicReader<'r> { +#[derive(Clone)] +pub struct Uint32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -2406,305 +3287,241 @@ impl<'r> ::core::fmt::LowerHex for CharacteristicReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for CharacteristicReader<'r> { +impl ::core::fmt::Debug for Uint32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for CharacteristicReader<'r> { +impl ::core::fmt::Display for Uint32Vec { 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) + 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> CharacteristicReader<'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]) +impl ::core::default::Default for Uint32Vec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Uint32Vec::new_unchecked(v.into()) } +} +impl Uint32Vec { + pub const ITEM_SIZE: usize = 4; - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) + pub fn len(&self) -> usize { + self.item_count() } - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) + pub fn is_empty(&self) -> bool { + self.len() == 0 } - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } } - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) + pub fn get_unchecked(&self, idx: usize) -> Uint32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32::new_unchecked(self.0.slice(start..end)) } - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) + pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> { + Uint32VecReader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for Uint32Vec { + type Builder = Uint32VecBuilder; - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } + const NAME: &'static str = "Uint32Vec"; - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint32Vec(data) } - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) } } -impl<'r> molecule::prelude::Reader<'r> for CharacteristicReader<'r> { - type Entity = Characteristic; - - const NAME: &'static str = "CharacteristicReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - CharacteristicReader(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); +#[derive(Clone, Copy)] +pub struct Uint32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; } - Ok(()) + write!(f, "{}", hex_string(self.as_slice())) } } -pub struct CharacteristicBuilder(pub(crate) [Byte; 20]); -impl ::core::fmt::Debug for CharacteristicBuilder { +impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) + write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::default::Default for CharacteristicBuilder { - fn default() -> Self { - CharacteristicBuilder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - 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<'r> ::core::fmt::Display for Uint32VecReader<'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 CharacteristicBuilder { - 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 - } +impl<'r> Uint32VecReader<'r> { + pub const ITEM_SIZE: usize = 4; - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self + pub fn len(&self) -> usize { + self.item_count() } - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self + pub fn is_empty(&self) -> bool { + self.len() == 0 } - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } } - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self + pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32Reader::new_unchecked(&self.as_slice()[start..end]) } +} +impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> { + type Entity = Uint32Vec; - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } + const NAME: &'static str = "Uint32VecReader"; - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint32VecReader(slice) } - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self + 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 Uint32VecBuilder(pub(crate) Vec); +impl Uint32VecBuilder { + pub const ITEM_SIZE: usize = 4; - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; self } - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; + pub fn push(mut self, v: Uint32) -> Self { + self.0.push(v); self } - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } self } - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self + pub fn replace(&mut self, index: usize, v: Uint32) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) } } -impl molecule::prelude::Builder for CharacteristicBuilder { - type Entity = Characteristic; +impl molecule::prelude::Builder for Uint32VecBuilder { + type Entity = Uint32Vec; - const NAME: &'static str = "CharacteristicBuilder"; + const NAME: &'static str = "Uint32VecBuilder"; fn expected_length(&self) -> usize { - Self::TOTAL_SIZE + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } 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(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } Ok(()) } @@ -2712,12 +3529,64 @@ impl molecule::prelude::Builder for CharacteristicBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Characteristic::new_unchecked(inner.into()) + Uint32Vec::new_unchecked(inner.into()) + } +} +pub struct Uint32VecIterator(Uint32Vec, usize, usize); +impl ::core::iter::Iterator for Uint32VecIterator { + type Item = Uint32; + + 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 Uint32VecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for Uint32Vec { + type IntoIter = Uint32VecIterator; + type Item = Uint32; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + Uint32VecIterator(self, 0, len) + } +} +impl<'r> Uint32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> { + Uint32VecReaderIterator(&self, 0, self.len()) + } +} +pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { + type Item = Uint32Reader<'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 Uint32VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 } } #[derive(Clone)] -pub struct OutPointSlice(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for OutPointSlice { +pub struct Byte32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -2726,196 +3595,306 @@ impl ::core::fmt::LowerHex for OutPointSlice { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for OutPointSlice { +impl ::core::fmt::Debug for Byte32Vec { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for OutPointSlice { +impl ::core::fmt::Display for Byte32Vec { 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) + 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 OutPointSlice { +impl ::core::default::Default for Byte32Vec { 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, - ]; - OutPointSlice::new_unchecked(v.into()) + let v: Vec = vec![0, 0, 0, 0]; + Byte32Vec::new_unchecked(v.into()) } } -impl OutPointSlice { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; +impl Byte32Vec { + pub const ITEM_SIZE: usize = 32; - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) + pub fn len(&self) -> usize { + self.item_count() } - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) + pub fn is_empty(&self) -> bool { + self.len() == 0 } - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } } - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) + 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 nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) + pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> { + Byte32VecReader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for Byte32Vec { + type Builder = Byte32VecBuilder; - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) + const NAME: &'static str = "Byte32Vec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte32Vec(data) } - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct Byte32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'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 Byte32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte32VecReader<'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> Byte32VecReader<'r> { + pub const ITEM_SIZE: usize = 32; - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) + 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 Byte32VecReader<'r> { + type Entity = Byte32Vec; - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } + const NAME: &'static str = "Byte32VecReader"; - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte32VecReader(slice) } - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth21(&self) -> Byte { - Byte::new_unchecked(self.0.slice(21..22)) + 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 Byte32VecBuilder(pub(crate) Vec); +impl Byte32VecBuilder { + pub const ITEM_SIZE: usize = 32; - pub fn nth22(&self) -> Byte { - Byte::new_unchecked(self.0.slice(22..23)) + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self } - pub fn nth23(&self) -> Byte { - Byte::new_unchecked(self.0.slice(23..24)) + pub fn push(mut self, v: Byte32) -> Self { + self.0.push(v); + self } - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self } - pub fn as_reader<'r>(&'r self) -> OutPointSliceReader<'r> { - OutPointSliceReader::new_unchecked(self.as_slice()) + 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::Entity for OutPointSlice { - type Builder = OutPointSliceBuilder; +impl molecule::prelude::Builder for Byte32VecBuilder { + type Entity = Byte32Vec; - const NAME: &'static str = "OutPointSlice"; + const NAME: &'static str = "Byte32VecBuilder"; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - OutPointSlice(data) + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() + 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 as_slice(&self) -> &[u8] { - &self.0[..] + 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)); + Byte32Vec::new_unchecked(inner.into()) } +} +pub struct Byte32VecIterator(Byte32Vec, usize, usize); +impl ::core::iter::Iterator for Byte32VecIterator { + type Item = Byte32; - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - OutPointSliceReader::from_slice(slice).map(|reader| reader.to_entity()) + 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) + } } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - OutPointSliceReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) +} +impl ::core::iter::ExactSizeIterator for Byte32VecIterator { + fn len(&self) -> usize { + self.2 - self.1 } +} +impl ::core::iter::IntoIterator for Byte32Vec { + type IntoIter = Byte32VecIterator; + type Item = Byte32; - fn new_builder() -> Self::Builder { - ::core::default::Default::default() + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + Byte32VecIterator(self, 0, len) + } +} +impl<'r> Byte32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> { + Byte32VecReaderIterator(&self, 0, self.len()) } +} +pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> { + type Item = Byte32Reader<'t>; - 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(), - ]) + 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) + } } } -#[derive(Clone, Copy)] -pub struct OutPointSliceReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for OutPointSliceReader<'r> { +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct CotaNFTId(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for CotaNFTId { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -2924,134 +3903,138 @@ impl<'r> ::core::fmt::LowerHex for OutPointSliceReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for OutPointSliceReader<'r> { +impl ::core::fmt::Debug for CotaNFTId { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for OutPointSliceReader<'r> { +impl ::core::fmt::Display for CotaNFTId { 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) + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "smt_type", self.smt_type())?; + write!(f, ", {}: {}", "cota_id", self.cota_id())?; + write!(f, ", {}: {}", "index", self.index())?; + write!(f, " }}") } } -impl<'r> OutPointSliceReader<'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]) +impl ::core::default::Default for CotaNFTId { + 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, + ]; + CotaNFTId::new_unchecked(v.into()) } +} +impl CotaNFTId { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; + pub const TOTAL_SIZE: usize = 26; - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) + pub fn smt_type(&self) -> Uint16 { + Uint16::new_unchecked(self.0.slice(0..2)) } - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) + pub fn cota_id(&self) -> CotaId { + CotaId::new_unchecked(self.0.slice(2..22)) } - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) + pub fn index(&self) -> Uint32 { + Uint32::new_unchecked(self.0.slice(22..26)) } - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) + pub fn as_reader<'r>(&'r self) -> CotaNFTIdReader<'r> { + CotaNFTIdReader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for CotaNFTId { + type Builder = CotaNFTIdBuilder; - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } + const NAME: &'static str = "CotaNFTId"; - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + CotaNFTId(data) } - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CotaNFTIdReader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CotaNFTIdReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .smt_type(self.smt_type()) + .cota_id(self.cota_id()) + .index(self.index()) } - - pub fn nth21(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[21..22]) +} +#[derive(Clone, Copy)] +pub struct CotaNFTIdReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for CotaNFTIdReader<'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 CotaNFTIdReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for CotaNFTIdReader<'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, ", {}: {}", "cota_id", self.cota_id())?; + write!(f, ", {}: {}", "index", self.index())?; + write!(f, " }}") } +} +impl<'r> CotaNFTIdReader<'r> { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; + pub const TOTAL_SIZE: usize = 26; - pub fn nth22(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[22..23]) + pub fn smt_type(&self) -> Uint16Reader<'r> { + Uint16Reader::new_unchecked(&self.as_slice()[0..2]) } - pub fn nth23(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[23..24]) + pub fn cota_id(&self) -> CotaIdReader<'r> { + CotaIdReader::new_unchecked(&self.as_slice()[2..22]) } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() + pub fn index(&self) -> Uint32Reader<'r> { + Uint32Reader::new_unchecked(&self.as_slice()[22..26]) } } -impl<'r> molecule::prelude::Reader<'r> for OutPointSliceReader<'r> { - type Entity = OutPointSlice; +impl<'r> molecule::prelude::Reader<'r> for CotaNFTIdReader<'r> { + type Entity = CotaNFTId; - const NAME: &'static str = "OutPointSliceReader"; + const NAME: &'static str = "CotaNFTIdReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - OutPointSliceReader(slice) + CotaNFTIdReader(slice) } fn as_slice(&self) -> &'r [u8] { @@ -3067,206 +4050,252 @@ impl<'r> molecule::prelude::Reader<'r> for OutPointSliceReader<'r> { Ok(()) } } -pub struct OutPointSliceBuilder(pub(crate) [Byte; 24]); -impl ::core::fmt::Debug for OutPointSliceBuilder { +#[derive(Debug, Default)] +pub struct CotaNFTIdBuilder { + pub(crate) smt_type: Uint16, + pub(crate) cota_id: CotaId, + pub(crate) index: Uint32, +} +impl CotaNFTIdBuilder { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; + pub const TOTAL_SIZE: usize = 26; + + pub fn smt_type(mut self, v: Uint16) -> Self { + self.smt_type = v; + self + } + + pub fn cota_id(mut self, v: CotaId) -> Self { + self.cota_id = v; + self + } + + pub fn index(mut self, v: Uint32) -> Self { + self.index = v; + self + } +} +impl molecule::prelude::Builder for CotaNFTIdBuilder { + type Entity = CotaNFTId; + + const NAME: &'static str = "CotaNFTIdBuilder"; + + 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.cota_id.as_slice())?; + writer.write_all(self.index.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)); + CotaNFTId::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct CotaNFTInfo(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for CotaNFTInfo { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::default::Default for OutPointSliceBuilder { +impl ::core::fmt::Debug for CotaNFTInfo { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for CotaNFTInfo { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "configure", self.configure())?; + write!(f, ", {}: {}", "state", self.state())?; + write!(f, ", {}: {}", "characteristic", self.characteristic())?; + write!(f, " }}") + } +} +impl ::core::default::Default for CotaNFTInfo { fn default() -> Self { - OutPointSliceBuilder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) + 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, + ]; + CotaNFTInfo::new_unchecked(v.into()) } } -impl OutPointSliceBuilder { - pub const ITEM_COUNT: usize = 24; - pub const ITEM_SIZE: usize = 1; - pub const TOTAL_SIZE: usize = 24; +impl CotaNFTInfo { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; + pub const TOTAL_SIZE: usize = 22; - pub fn set(mut self, v: [Byte; 24]) -> Self { - self.0 = v; - self + pub fn configure(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) } - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self + pub fn state(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) } - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self + pub fn characteristic(&self) -> Characteristic { + Characteristic::new_unchecked(self.0.slice(2..22)) } - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self + pub fn as_reader<'r>(&'r self) -> CotaNFTInfoReader<'r> { + CotaNFTInfoReader::new_unchecked(self.as_slice()) } +} +impl molecule::prelude::Entity for CotaNFTInfo { + type Builder = CotaNFTInfoBuilder; - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } + const NAME: &'static str = "CotaNFTInfo"; - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + CotaNFTInfo(data) } - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() } - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self + fn as_slice(&self) -> &[u8] { + &self.0[..] } - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CotaNFTInfoReader::from_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + CotaNFTInfoReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self + fn new_builder() -> Self::Builder { + ::core::default::Default::default() } - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self + fn as_builder(self) -> Self::Builder { + Self::new_builder() + .configure(self.configure()) + .state(self.state()) + .characteristic(self.characteristic()) + } +} +#[derive(Clone, Copy)] +pub struct CotaNFTInfoReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for CotaNFTInfoReader<'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())) } - - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self +} +impl<'r> ::core::fmt::Debug for CotaNFTInfoReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) } - - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self +} +impl<'r> ::core::fmt::Display for CotaNFTInfoReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} {{ ", Self::NAME)?; + write!(f, "{}: {}", "configure", self.configure())?; + write!(f, ", {}: {}", "state", self.state())?; + write!(f, ", {}: {}", "characteristic", self.characteristic())?; + write!(f, " }}") } +} +impl<'r> CotaNFTInfoReader<'r> { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; + pub const TOTAL_SIZE: usize = 22; - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self + pub fn configure(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) } - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self + pub fn state(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) } - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self + pub fn characteristic(&self) -> CharacteristicReader<'r> { + CharacteristicReader::new_unchecked(&self.as_slice()[2..22]) } +} +impl<'r> molecule::prelude::Reader<'r> for CotaNFTInfoReader<'r> { + type Entity = CotaNFTInfo; - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } + const NAME: &'static str = "CotaNFTInfoReader"; - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self + fn new_unchecked(slice: &'r [u8]) -> Self { + CotaNFTInfoReader(slice) } - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self + fn as_slice(&self) -> &'r [u8] { + self.0 } - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self + 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 CotaNFTInfoBuilder { + pub(crate) configure: Byte, + pub(crate) state: Byte, + pub(crate) characteristic: Characteristic, +} +impl CotaNFTInfoBuilder { + pub const FIELD_COUNT: usize = 3; + pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; + pub const TOTAL_SIZE: usize = 22; - pub fn nth21(mut self, v: Byte) -> Self { - self.0[21] = v; + pub fn configure(mut self, v: Byte) -> Self { + self.configure = v; self } - pub fn nth22(mut self, v: Byte) -> Self { - self.0[22] = v; + pub fn state(mut self, v: Byte) -> Self { + self.state = v; self } - pub fn nth23(mut self, v: Byte) -> Self { - self.0[23] = v; + pub fn characteristic(mut self, v: Characteristic) -> Self { + self.characteristic = v; self } } -impl molecule::prelude::Builder for OutPointSliceBuilder { - type Entity = OutPointSlice; +impl molecule::prelude::Builder for CotaNFTInfoBuilder { + type Entity = CotaNFTInfo; - const NAME: &'static str = "OutPointSliceBuilder"; + const NAME: &'static str = "CotaNFTInfoBuilder"; 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.configure.as_slice())?; + writer.write_all(self.state.as_slice())?; + writer.write_all(self.characteristic.as_slice())?; Ok(()) } @@ -3274,12 +4303,12 @@ impl molecule::prelude::Builder for OutPointSliceBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - OutPointSlice::new_unchecked(inner.into()) + CotaNFTInfo::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct CotaNFTId(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for CotaNFTId { +pub struct MerkleProof(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for MerkleProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3288,56 +4317,83 @@ impl ::core::fmt::LowerHex for CotaNFTId { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for CotaNFTId { +impl ::core::fmt::Debug for MerkleProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for CotaNFTId { +impl ::core::fmt::Display for MerkleProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "smt_type", self.smt_type())?; - write!(f, ", {}: {}", "cota_id", self.cota_id())?; - write!(f, ", {}: {}", "index", self.index())?; + write!(f, "{}: {}", "indices", self.indices())?; + write!(f, ", {}: {}", "lemmas", self.lemmas())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } write!(f, " }}") } } -impl ::core::default::Default for CotaNFTId { +impl ::core::default::Default for MerkleProof { 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, + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; - CotaNFTId::new_unchecked(v.into()) + MerkleProof::new_unchecked(v.into()) } } -impl CotaNFTId { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; - pub const TOTAL_SIZE: usize = 26; +impl MerkleProof { + pub const FIELD_COUNT: usize = 2; - pub fn smt_type(&self) -> Uint16 { - Uint16::new_unchecked(self.0.slice(0..2)) + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn cota_id(&self) -> CotaId { - CotaId::new_unchecked(self.0.slice(2..22)) + 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 index(&self) -> Uint32 { - Uint32::new_unchecked(self.0.slice(22..26)) + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT } - pub fn as_reader<'r>(&'r self) -> CotaNFTIdReader<'r> { - CotaNFTIdReader::new_unchecked(self.as_slice()) + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn indices(&self) -> Uint32Vec { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint32Vec::new_unchecked(self.0.slice(start..end)) + } + + pub fn lemmas(&self) -> Byte32Vec { + 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; + Byte32Vec::new_unchecked(self.0.slice(start..end)) + } else { + Byte32Vec::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> MerkleProofReader<'r> { + MerkleProofReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for CotaNFTId { - type Builder = CotaNFTIdBuilder; +impl molecule::prelude::Entity for MerkleProof { + type Builder = MerkleProofBuilder; - const NAME: &'static str = "CotaNFTId"; + const NAME: &'static str = "MerkleProof"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - CotaNFTId(data) + MerkleProof(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -3349,11 +4405,11 @@ impl molecule::prelude::Entity for CotaNFTId { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaNFTIdReader::from_slice(slice).map(|reader| reader.to_entity()) + MerkleProofReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaNFTIdReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + MerkleProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -3362,14 +4418,13 @@ impl molecule::prelude::Entity for CotaNFTId { fn as_builder(self) -> Self::Builder { Self::new_builder() - .smt_type(self.smt_type()) - .cota_id(self.cota_id()) - .index(self.index()) + .indices(self.indices()) + .lemmas(self.lemmas()) } } #[derive(Clone, Copy)] -pub struct CotaNFTIdReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for CotaNFTIdReader<'r> { +pub struct MerkleProofReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for MerkleProofReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3378,102 +4433,165 @@ impl<'r> ::core::fmt::LowerHex for CotaNFTIdReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for CotaNFTIdReader<'r> { +impl<'r> ::core::fmt::Debug for MerkleProofReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for CotaNFTIdReader<'r> { +impl<'r> ::core::fmt::Display for MerkleProofReader<'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, ", {}: {}", "cota_id", self.cota_id())?; - write!(f, ", {}: {}", "index", self.index())?; + write!(f, "{}: {}", "indices", self.indices())?; + write!(f, ", {}: {}", "lemmas", self.lemmas())?; + let extra_count = self.count_extra_fields(); + if extra_count != 0 { + write!(f, ", .. ({} fields)", extra_count)?; + } write!(f, " }}") } } -impl<'r> CotaNFTIdReader<'r> { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; - pub const TOTAL_SIZE: usize = 26; +impl<'r> MerkleProofReader<'r> { + pub const FIELD_COUNT: usize = 2; - pub fn smt_type(&self) -> Uint16Reader<'r> { - Uint16Reader::new_unchecked(&self.as_slice()[0..2]) + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn cota_id(&self) -> CotaIdReader<'r> { - CotaIdReader::new_unchecked(&self.as_slice()[2..22]) + 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 index(&self) -> Uint32Reader<'r> { - Uint32Reader::new_unchecked(&self.as_slice()[22..26]) + 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 indices(&self) -> Uint32VecReader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Uint32VecReader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn lemmas(&self) -> Byte32VecReader<'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; + Byte32VecReader::new_unchecked(&self.as_slice()[start..end]) + } else { + Byte32VecReader::new_unchecked(&self.as_slice()[start..]) + } } } -impl<'r> molecule::prelude::Reader<'r> for CotaNFTIdReader<'r> { - type Entity = CotaNFTId; +impl<'r> molecule::prelude::Reader<'r> for MerkleProofReader<'r> { + type Entity = MerkleProof; - const NAME: &'static str = "CotaNFTIdReader"; + const NAME: &'static str = "MerkleProofReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - CotaNFTIdReader(slice) + MerkleProofReader(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 != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, 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); + } + Uint32VecReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + Byte32VecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; Ok(()) } } #[derive(Debug, Default)] -pub struct CotaNFTIdBuilder { - pub(crate) smt_type: Uint16, - pub(crate) cota_id: CotaId, - pub(crate) index: Uint32, +pub struct MerkleProofBuilder { + pub(crate) indices: Uint32Vec, + pub(crate) lemmas: Byte32Vec, } -impl CotaNFTIdBuilder { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [2, 20, 4]; - pub const TOTAL_SIZE: usize = 26; - - pub fn smt_type(mut self, v: Uint16) -> Self { - self.smt_type = v; - self - } +impl MerkleProofBuilder { + pub const FIELD_COUNT: usize = 2; - pub fn cota_id(mut self, v: CotaId) -> Self { - self.cota_id = v; + pub fn indices(mut self, v: Uint32Vec) -> Self { + self.indices = v; self } - pub fn index(mut self, v: Uint32) -> Self { - self.index = v; + pub fn lemmas(mut self, v: Byte32Vec) -> Self { + self.lemmas = v; self } } -impl molecule::prelude::Builder for CotaNFTIdBuilder { - type Entity = CotaNFTId; +impl molecule::prelude::Builder for MerkleProofBuilder { + type Entity = MerkleProof; - const NAME: &'static str = "CotaNFTIdBuilder"; + const NAME: &'static str = "MerkleProofBuilder"; fn expected_length(&self) -> usize { - Self::TOTAL_SIZE + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.indices.as_slice().len() + + self.lemmas.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.smt_type.as_slice())?; - writer.write_all(self.cota_id.as_slice())?; - writer.write_all(self.index.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.indices.as_slice().len(); + offsets.push(total_size); + total_size += self.lemmas.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.indices.as_slice())?; + writer.write_all(self.lemmas.as_slice())?; Ok(()) } @@ -3481,12 +4599,12 @@ impl molecule::prelude::Builder for CotaNFTIdBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - CotaNFTId::new_unchecked(inner.into()) + MerkleProof::new_unchecked(inner.into()) } } #[derive(Clone)] -pub struct CotaNFTInfo(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for CotaNFTInfo { +pub struct TransactionProof(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for TransactionProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3495,56 +4613,85 @@ impl ::core::fmt::LowerHex for CotaNFTInfo { write!(f, "{}", hex_string(self.as_slice())) } } -impl ::core::fmt::Debug for CotaNFTInfo { +impl ::core::fmt::Debug for TransactionProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl ::core::fmt::Display for CotaNFTInfo { +impl ::core::fmt::Display for TransactionProof { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "configure", self.configure())?; - write!(f, ", {}: {}", "state", self.state())?; - write!(f, ", {}: {}", "characteristic", self.characteristic())?; + write!(f, "{}: {}", "witnesses_root", self.witnesses_root())?; + 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 CotaNFTInfo { +impl ::core::default::Default for TransactionProof { 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, + 64, 0, 0, 0, 12, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, ]; - CotaNFTInfo::new_unchecked(v.into()) + TransactionProof::new_unchecked(v.into()) } } -impl CotaNFTInfo { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; - pub const TOTAL_SIZE: usize = 22; +impl TransactionProof { + pub const FIELD_COUNT: usize = 2; - pub fn configure(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn state(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) + 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 characteristic(&self) -> Characteristic { - Characteristic::new_unchecked(self.0.slice(2..22)) + pub fn count_extra_fields(&self) -> usize { + self.field_count() - Self::FIELD_COUNT } - pub fn as_reader<'r>(&'r self) -> CotaNFTInfoReader<'r> { - CotaNFTInfoReader::new_unchecked(self.as_slice()) + pub fn has_extra_fields(&self) -> bool { + Self::FIELD_COUNT != self.field_count() + } + + pub fn witnesses_root(&self) -> Byte32 { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Byte32::new_unchecked(self.0.slice(start..end)) + } + + pub fn proof(&self) -> MerkleProof { + 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; + MerkleProof::new_unchecked(self.0.slice(start..end)) + } else { + MerkleProof::new_unchecked(self.0.slice(start..)) + } + } + + pub fn as_reader<'r>(&'r self) -> TransactionProofReader<'r> { + TransactionProofReader::new_unchecked(self.as_slice()) } } -impl molecule::prelude::Entity for CotaNFTInfo { - type Builder = CotaNFTInfoBuilder; +impl molecule::prelude::Entity for TransactionProof { + type Builder = TransactionProofBuilder; - const NAME: &'static str = "CotaNFTInfo"; + const NAME: &'static str = "TransactionProof"; fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - CotaNFTInfo(data) + TransactionProof(data) } fn as_bytes(&self) -> molecule::bytes::Bytes { @@ -3556,11 +4703,11 @@ impl molecule::prelude::Entity for CotaNFTInfo { } fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaNFTInfoReader::from_slice(slice).map(|reader| reader.to_entity()) + TransactionProofReader::from_slice(slice).map(|reader| reader.to_entity()) } fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - CotaNFTInfoReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + TransactionProofReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) } fn new_builder() -> Self::Builder { @@ -3569,14 +4716,13 @@ impl molecule::prelude::Entity for CotaNFTInfo { fn as_builder(self) -> Self::Builder { Self::new_builder() - .configure(self.configure()) - .state(self.state()) - .characteristic(self.characteristic()) + .witnesses_root(self.witnesses_root()) + .proof(self.proof()) } } #[derive(Clone, Copy)] -pub struct CotaNFTInfoReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for CotaNFTInfoReader<'r> { +pub struct TransactionProofReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for TransactionProofReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use molecule::hex_string; if f.alternate() { @@ -3585,102 +4731,165 @@ impl<'r> ::core::fmt::LowerHex for CotaNFTInfoReader<'r> { write!(f, "{}", hex_string(self.as_slice())) } } -impl<'r> ::core::fmt::Debug for CotaNFTInfoReader<'r> { +impl<'r> ::core::fmt::Debug for TransactionProofReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}({:#x})", Self::NAME, self) } } -impl<'r> ::core::fmt::Display for CotaNFTInfoReader<'r> { +impl<'r> ::core::fmt::Display for TransactionProofReader<'r> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "configure", self.configure())?; - write!(f, ", {}: {}", "state", self.state())?; - write!(f, ", {}: {}", "characteristic", self.characteristic())?; + write!(f, "{}: {}", "witnesses_root", self.witnesses_root())?; + 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> CotaNFTInfoReader<'r> { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; - pub const TOTAL_SIZE: usize = 22; +impl<'r> TransactionProofReader<'r> { + pub const FIELD_COUNT: usize = 2; - pub fn configure(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize } - pub fn state(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) + 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 characteristic(&self) -> CharacteristicReader<'r> { - CharacteristicReader::new_unchecked(&self.as_slice()[2..22]) + 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 witnesses_root(&self) -> Byte32Reader<'r> { + let slice = self.as_slice(); + let start = molecule::unpack_number(&slice[4..]) as usize; + let end = molecule::unpack_number(&slice[8..]) as usize; + Byte32Reader::new_unchecked(&self.as_slice()[start..end]) + } + + pub fn proof(&self) -> MerkleProofReader<'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; + MerkleProofReader::new_unchecked(&self.as_slice()[start..end]) + } else { + MerkleProofReader::new_unchecked(&self.as_slice()[start..]) + } } } -impl<'r> molecule::prelude::Reader<'r> for CotaNFTInfoReader<'r> { - type Entity = CotaNFTInfo; +impl<'r> molecule::prelude::Reader<'r> for TransactionProofReader<'r> { + type Entity = TransactionProof; - const NAME: &'static str = "CotaNFTInfoReader"; + const NAME: &'static str = "TransactionProofReader"; fn to_entity(&self) -> Self::Entity { Self::Entity::new_unchecked(self.as_slice().to_owned().into()) } fn new_unchecked(slice: &'r [u8]) -> Self { - CotaNFTInfoReader(slice) + TransactionProofReader(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 != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, 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); } + Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?; + MerkleProofReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; Ok(()) } } #[derive(Debug, Default)] -pub struct CotaNFTInfoBuilder { - pub(crate) configure: Byte, - pub(crate) state: Byte, - pub(crate) characteristic: Characteristic, +pub struct TransactionProofBuilder { + pub(crate) witnesses_root: Byte32, + pub(crate) proof: MerkleProof, } -impl CotaNFTInfoBuilder { - pub const FIELD_COUNT: usize = 3; - pub const FIELD_SIZES: [usize; 3] = [1, 1, 20]; - pub const TOTAL_SIZE: usize = 22; - - pub fn configure(mut self, v: Byte) -> Self { - self.configure = v; - self - } +impl TransactionProofBuilder { + pub const FIELD_COUNT: usize = 2; - pub fn state(mut self, v: Byte) -> Self { - self.state = v; + pub fn witnesses_root(mut self, v: Byte32) -> Self { + self.witnesses_root = v; self } - pub fn characteristic(mut self, v: Characteristic) -> Self { - self.characteristic = v; + pub fn proof(mut self, v: MerkleProof) -> Self { + self.proof = v; self } } -impl molecule::prelude::Builder for CotaNFTInfoBuilder { - type Entity = CotaNFTInfo; +impl molecule::prelude::Builder for TransactionProofBuilder { + type Entity = TransactionProof; - const NAME: &'static str = "CotaNFTInfoBuilder"; + const NAME: &'static str = "TransactionProofBuilder"; fn expected_length(&self) -> usize { - Self::TOTAL_SIZE + molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) + + self.witnesses_root.as_slice().len() + + self.proof.as_slice().len() } fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.configure.as_slice())?; - writer.write_all(self.state.as_slice())?; - writer.write_all(self.characteristic.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.witnesses_root.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.witnesses_root.as_slice())?; + writer.write_all(self.proof.as_slice())?; Ok(()) } @@ -3688,7 +4897,7 @@ impl molecule::prelude::Builder for CotaNFTInfoBuilder { let mut inner = Vec::with_capacity(self.expected_length()); self.write(&mut inner) .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - CotaNFTInfo::new_unchecked(inner.into()) + TransactionProof::new_unchecked(inner.into()) } } #[derive(Clone)] diff --git a/src/define.rs b/src/define.rs index 02e6659..5ce88e7 100644 --- a/src/define.rs +++ b/src/define.rs @@ -1,5 +1,4 @@ -// Generated by Molecule 0.7.2 - +// Generated by Molecule 0.7.3 #![allow(unused_imports)] #![allow(dead_code)] #![allow(clippy::if_same_then_else)] @@ -11,7 +10,6 @@ pub use alloc::vec::*; // these lines above are manually added use super::common::*; -use molecule::prelude::*; #[derive(Clone)] pub struct DefineCotaNFTEntries(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for DefineCotaNFTEntries { @@ -45,7 +43,7 @@ impl ::core::fmt::Display for DefineCotaNFTEntries { impl ::core::default::Default for DefineCotaNFTEntries { 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, 4, 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, ]; DefineCotaNFTEntries::new_unchecked(v.into()) diff --git a/src/lib.rs b/src/lib.rs index fcc9a4b..43411ee 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ pub mod common; pub mod define; pub mod mint; -pub mod raw_tx; pub mod registry; pub mod smt; pub mod transfer; diff --git a/src/mint.rs b/src/mint.rs index d88c048..6c64cbc 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -11,7 +11,6 @@ pub use alloc::vec::*; // these lines above are manually added use super::common::*; -use molecule::prelude::*; #[derive(Clone)] pub struct MintCotaNFTEntries(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for MintCotaNFTEntries { diff --git a/src/raw_tx.mol b/src/raw_tx.mol deleted file mode 100644 index 627f90a..0000000 --- a/src/raw_tx.mol +++ /dev/null @@ -1,59 +0,0 @@ -import common; - -array H256 [byte; 32]; - -vector BytesVec ; -vector Byte32Vec ; -vector Uint32Vec ; -vector H256Vec ; - -option ScriptOpt (Script); -vector CellDepVec ; -vector CellInputVec ; -vector CellOutputVec ; - -table Script { - code_hash: Byte32, - hash_type: byte, - args: Bytes, -} - -struct OutPoint { - tx_hash: Byte32, - index: Uint32, -} - -struct CellInput { - since: Uint64, - previous_output: OutPoint, -} - -table CellOutput { - capacity: Uint64, - lock: Script, - type_: ScriptOpt, -} - -struct CellDep { - out_point: OutPoint, - dep_type: byte, -} - -table RawTransaction { - version: Uint32, - cell_deps: CellDepVec, - header_deps: Byte32Vec, - inputs: CellInputVec, - outputs: CellOutputVec, - outputs_data: BytesVec, -} - -table MerkleProof { - indices: Uint32Vec, - lemmas: H256Vec, -} - -table TransactionProof { - witnesses_root: H256, - proof: MerkleProof, -} \ No newline at end of file diff --git a/src/raw_tx.rs b/src/raw_tx.rs deleted file mode 100644 index 0197726..0000000 --- a/src/raw_tx.rs +++ /dev/null @@ -1,5400 +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 H256(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for H256 { - 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 H256 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for H256 { - 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 H256 { - 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, - ]; - H256::new_unchecked(v.into()) - } -} -impl H256 { - 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) -> H256Reader<'r> { - H256Reader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for H256 { - type Builder = H256Builder; - - const NAME: &'static str = "H256"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - H256(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 { - H256Reader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256Reader::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 H256Reader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for H256Reader<'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 H256Reader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for H256Reader<'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> H256Reader<'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 H256Reader<'r> { - type Entity = H256; - - const NAME: &'static str = "H256Reader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - H256Reader(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 H256Builder(pub(crate) [Byte; 32]); -impl ::core::fmt::Debug for H256Builder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for H256Builder { - fn default() -> Self { - H256Builder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - 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 H256Builder { - 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 H256Builder { - type Entity = H256; - - const NAME: &'static str = "H256Builder"; - - 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)); - H256::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct BytesVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for BytesVec { - 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 BytesVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for BytesVec { - 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 BytesVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - BytesVec::new_unchecked(v.into()) - } -} -impl BytesVec { - 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) -> BytesVecReader<'r> { - BytesVecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for BytesVec { - type Builder = BytesVecBuilder; - - const NAME: &'static str = "BytesVec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - BytesVec(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 { - BytesVecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - BytesVecReader::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 BytesVecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for BytesVecReader<'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 BytesVecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for BytesVecReader<'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> BytesVecReader<'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 BytesVecReader<'r> { - type Entity = BytesVec; - - const NAME: &'static str = "BytesVecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - BytesVecReader(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 BytesVecBuilder(pub(crate) Vec); -impl BytesVecBuilder { - 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 BytesVecBuilder { - type Entity = BytesVec; - - const NAME: &'static str = "BytesVecBuilder"; - - 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)); - BytesVec::new_unchecked(inner.into()) - } -} -pub struct BytesVecIterator(BytesVec, usize, usize); -impl ::core::iter::Iterator for BytesVecIterator { - 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 BytesVecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for BytesVec { - type IntoIter = BytesVecIterator; - type Item = Bytes; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - BytesVecIterator(self, 0, len) - } -} -impl<'r> BytesVecReader<'r> { - pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> { - BytesVecReaderIterator(&self, 0, self.len()) - } -} -pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'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 BytesVecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct Byte32Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Byte32Vec { - 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 Byte32Vec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Byte32Vec { - 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 Byte32Vec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Byte32Vec::new_unchecked(v.into()) - } -} -impl Byte32Vec { - 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) -> Byte32VecReader<'r> { - Byte32VecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Byte32Vec { - type Builder = Byte32VecBuilder; - - const NAME: &'static str = "Byte32Vec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Byte32Vec(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 { - Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Byte32VecReader::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 Byte32VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'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 Byte32VecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Byte32VecReader<'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> Byte32VecReader<'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 Byte32VecReader<'r> { - type Entity = Byte32Vec; - - const NAME: &'static str = "Byte32VecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Byte32VecReader(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 Byte32VecBuilder(pub(crate) Vec); -impl Byte32VecBuilder { - 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 Byte32VecBuilder { - type Entity = Byte32Vec; - - const NAME: &'static str = "Byte32VecBuilder"; - - 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)); - Byte32Vec::new_unchecked(inner.into()) - } -} -pub struct Byte32VecIterator(Byte32Vec, usize, usize); -impl ::core::iter::Iterator for Byte32VecIterator { - 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 Byte32VecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for Byte32Vec { - type IntoIter = Byte32VecIterator; - type Item = Byte32; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - Byte32VecIterator(self, 0, len) - } -} -impl<'r> Byte32VecReader<'r> { - pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> { - Byte32VecReaderIterator(&self, 0, self.len()) - } -} -pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'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 Byte32VecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct Uint32Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Uint32Vec { - 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 Uint32Vec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Uint32Vec { - 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 Uint32Vec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Uint32Vec::new_unchecked(v.into()) - } -} -impl Uint32Vec { - pub const ITEM_SIZE: usize = 4; - - 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) -> Uint32 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Uint32::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> { - Uint32VecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Uint32Vec { - type Builder = Uint32VecBuilder; - - const NAME: &'static str = "Uint32Vec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Uint32Vec(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 { - Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - Uint32VecReader::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 Uint32VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'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 Uint32VecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for Uint32VecReader<'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> Uint32VecReader<'r> { - pub const ITEM_SIZE: usize = 4; - - 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) -> Uint32Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - Uint32Reader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> { - type Entity = Uint32Vec; - - const NAME: &'static str = "Uint32VecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - Uint32VecReader(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 Uint32VecBuilder(pub(crate) Vec); -impl Uint32VecBuilder { - pub const ITEM_SIZE: usize = 4; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: Uint32) -> 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: Uint32) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for Uint32VecBuilder { - type Entity = Uint32Vec; - - const NAME: &'static str = "Uint32VecBuilder"; - - 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)); - Uint32Vec::new_unchecked(inner.into()) - } -} -pub struct Uint32VecIterator(Uint32Vec, usize, usize); -impl ::core::iter::Iterator for Uint32VecIterator { - type Item = Uint32; - - 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 Uint32VecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for Uint32Vec { - type IntoIter = Uint32VecIterator; - type Item = Uint32; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - Uint32VecIterator(self, 0, len) - } -} -impl<'r> Uint32VecReader<'r> { - pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> { - Uint32VecReaderIterator(&self, 0, self.len()) - } -} -pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { - type Item = Uint32Reader<'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 Uint32VecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct H256Vec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for H256Vec { - 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 H256Vec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for H256Vec { - 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 H256Vec { - fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - H256Vec::new_unchecked(v.into()) - } -} -impl H256Vec { - 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) -> H256 { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - H256::new_unchecked(self.0.slice(start..end)) - } - - pub fn as_reader<'r>(&'r self) -> H256VecReader<'r> { - H256VecReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for H256Vec { - type Builder = H256VecBuilder; - - const NAME: &'static str = "H256Vec"; - - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - H256Vec(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 { - H256VecReader::from_slice(slice).map(|reader| reader.to_entity()) - } - - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - H256VecReader::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 H256VecReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for H256VecReader<'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 H256VecReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for H256VecReader<'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> H256VecReader<'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) -> H256Reader<'r> { - let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; - let end = start + Self::ITEM_SIZE; - H256Reader::new_unchecked(&self.as_slice()[start..end]) - } -} -impl<'r> molecule::prelude::Reader<'r> for H256VecReader<'r> { - type Entity = H256Vec; - - const NAME: &'static str = "H256VecReader"; - - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - - fn new_unchecked(slice: &'r [u8]) -> Self { - H256VecReader(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 H256VecBuilder(pub(crate) Vec); -impl H256VecBuilder { - pub const ITEM_SIZE: usize = 32; - - pub fn set(mut self, v: Vec) -> Self { - self.0 = v; - self - } - - pub fn push(mut self, v: H256) -> 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: H256) -> Option { - self.0 - .get_mut(index) - .map(|item| ::core::mem::replace(item, v)) - } -} -impl molecule::prelude::Builder for H256VecBuilder { - type Entity = H256Vec; - - const NAME: &'static str = "H256VecBuilder"; - - 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)); - H256Vec::new_unchecked(inner.into()) - } -} -pub struct H256VecIterator(H256Vec, usize, usize); -impl ::core::iter::Iterator for H256VecIterator { - type Item = H256; - - 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 H256VecIterator { - fn len(&self) -> usize { - self.2 - self.1 - } -} -impl ::core::iter::IntoIterator for H256Vec { - type IntoIter = H256VecIterator; - type Item = H256; - - fn into_iter(self) -> Self::IntoIter { - let len = self.len(); - H256VecIterator(self, 0, len) - } -} -impl<'r> H256VecReader<'r> { - pub fn iter<'t>(&'t self) -> H256VecReaderIterator<'t, 'r> { - H256VecReaderIterator(&self, 0, self.len()) - } -} -pub struct H256VecReaderIterator<'t, 'r>(&'t H256VecReader<'r>, usize, usize); -impl<'t: 'r, 'r> ::core::iter::Iterator for H256VecReaderIterator<'t, 'r> { - type Item = H256Reader<'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 H256VecReaderIterator<'t, 'r> { - fn len(&self) -> usize { - self.2 - self.1 - } -} -#[derive(Clone)] -pub struct ScriptOpt(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for ScriptOpt { - 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 ScriptOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for ScriptOpt { - 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 ScriptOpt { - fn default() -> Self { - let v: Vec = vec![]; - ScriptOpt::new_unchecked(v.into()) - } -} -impl ScriptOpt { - 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