ckb_sdk/types/
omni_lock.rs

1// Generated by Molecule 0.7.0
2
3#![allow(unused_imports)]
4
5use ckb_types::molecule;
6use ckb_types::packed::*;
7use ckb_types::prelude::*;
8// these lines above are manually added
9// replace "::molecule" to "molecule" in below code
10use super::xudt_rce_mol::*;
11
12use molecule::prelude::*;
13
14#[derive(Clone)]
15pub struct Auth(molecule::bytes::Bytes);
16impl ::core::fmt::LowerHex for Auth {
17    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
18        use molecule::hex_string;
19        if f.alternate() {
20            write!(f, "0x")?;
21        }
22        write!(f, "{}", hex_string(self.as_slice()))
23    }
24}
25impl ::core::fmt::Debug for Auth {
26    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
27        write!(f, "{}({:#x})", Self::NAME, self)
28    }
29}
30impl ::core::fmt::Display for Auth {
31    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
32        use molecule::hex_string;
33        let raw_data = hex_string(&self.raw_data());
34        write!(f, "{}(0x{})", Self::NAME, raw_data)
35    }
36}
37impl ::core::default::Default for Auth {
38    fn default() -> Self {
39        let v: Vec<u8> = vec![
40            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41        ];
42        Auth::new_unchecked(v.into())
43    }
44}
45impl Auth {
46    pub const TOTAL_SIZE: usize = 21;
47    pub const ITEM_SIZE: usize = 1;
48    pub const ITEM_COUNT: usize = 21;
49    pub fn nth0(&self) -> Byte {
50        Byte::new_unchecked(self.0.slice(0..1))
51    }
52    pub fn nth1(&self) -> Byte {
53        Byte::new_unchecked(self.0.slice(1..2))
54    }
55    pub fn nth2(&self) -> Byte {
56        Byte::new_unchecked(self.0.slice(2..3))
57    }
58    pub fn nth3(&self) -> Byte {
59        Byte::new_unchecked(self.0.slice(3..4))
60    }
61    pub fn nth4(&self) -> Byte {
62        Byte::new_unchecked(self.0.slice(4..5))
63    }
64    pub fn nth5(&self) -> Byte {
65        Byte::new_unchecked(self.0.slice(5..6))
66    }
67    pub fn nth6(&self) -> Byte {
68        Byte::new_unchecked(self.0.slice(6..7))
69    }
70    pub fn nth7(&self) -> Byte {
71        Byte::new_unchecked(self.0.slice(7..8))
72    }
73    pub fn nth8(&self) -> Byte {
74        Byte::new_unchecked(self.0.slice(8..9))
75    }
76    pub fn nth9(&self) -> Byte {
77        Byte::new_unchecked(self.0.slice(9..10))
78    }
79    pub fn nth10(&self) -> Byte {
80        Byte::new_unchecked(self.0.slice(10..11))
81    }
82    pub fn nth11(&self) -> Byte {
83        Byte::new_unchecked(self.0.slice(11..12))
84    }
85    pub fn nth12(&self) -> Byte {
86        Byte::new_unchecked(self.0.slice(12..13))
87    }
88    pub fn nth13(&self) -> Byte {
89        Byte::new_unchecked(self.0.slice(13..14))
90    }
91    pub fn nth14(&self) -> Byte {
92        Byte::new_unchecked(self.0.slice(14..15))
93    }
94    pub fn nth15(&self) -> Byte {
95        Byte::new_unchecked(self.0.slice(15..16))
96    }
97    pub fn nth16(&self) -> Byte {
98        Byte::new_unchecked(self.0.slice(16..17))
99    }
100    pub fn nth17(&self) -> Byte {
101        Byte::new_unchecked(self.0.slice(17..18))
102    }
103    pub fn nth18(&self) -> Byte {
104        Byte::new_unchecked(self.0.slice(18..19))
105    }
106    pub fn nth19(&self) -> Byte {
107        Byte::new_unchecked(self.0.slice(19..20))
108    }
109    pub fn nth20(&self) -> Byte {
110        Byte::new_unchecked(self.0.slice(20..21))
111    }
112    pub fn raw_data(&self) -> molecule::bytes::Bytes {
113        self.as_bytes()
114    }
115    pub fn as_reader<'r>(&'r self) -> AuthReader<'r> {
116        AuthReader::new_unchecked(self.as_slice())
117    }
118}
119impl molecule::prelude::Entity for Auth {
120    type Builder = AuthBuilder;
121    const NAME: &'static str = "Auth";
122    fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
123        Auth(data)
124    }
125    fn as_bytes(&self) -> molecule::bytes::Bytes {
126        self.0.clone()
127    }
128    fn as_slice(&self) -> &[u8] {
129        &self.0[..]
130    }
131    fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
132        AuthReader::from_slice(slice).map(|reader| reader.to_entity())
133    }
134    fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
135        AuthReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
136    }
137    fn new_builder() -> Self::Builder {
138        ::core::default::Default::default()
139    }
140    fn as_builder(self) -> Self::Builder {
141        Self::new_builder().set([
142            self.nth0(),
143            self.nth1(),
144            self.nth2(),
145            self.nth3(),
146            self.nth4(),
147            self.nth5(),
148            self.nth6(),
149            self.nth7(),
150            self.nth8(),
151            self.nth9(),
152            self.nth10(),
153            self.nth11(),
154            self.nth12(),
155            self.nth13(),
156            self.nth14(),
157            self.nth15(),
158            self.nth16(),
159            self.nth17(),
160            self.nth18(),
161            self.nth19(),
162            self.nth20(),
163        ])
164    }
165}
166#[derive(Clone, Copy)]
167pub struct AuthReader<'r>(&'r [u8]);
168impl<'r> ::core::fmt::LowerHex for AuthReader<'r> {
169    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
170        use molecule::hex_string;
171        if f.alternate() {
172            write!(f, "0x")?;
173        }
174        write!(f, "{}", hex_string(self.as_slice()))
175    }
176}
177impl<'r> ::core::fmt::Debug for AuthReader<'r> {
178    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
179        write!(f, "{}({:#x})", Self::NAME, self)
180    }
181}
182impl<'r> ::core::fmt::Display for AuthReader<'r> {
183    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
184        use molecule::hex_string;
185        let raw_data = hex_string(&self.raw_data());
186        write!(f, "{}(0x{})", Self::NAME, raw_data)
187    }
188}
189impl<'r> AuthReader<'r> {
190    pub const TOTAL_SIZE: usize = 21;
191    pub const ITEM_SIZE: usize = 1;
192    pub const ITEM_COUNT: usize = 21;
193    pub fn nth0(&self) -> ByteReader<'r> {
194        ByteReader::new_unchecked(&self.as_slice()[0..1])
195    }
196    pub fn nth1(&self) -> ByteReader<'r> {
197        ByteReader::new_unchecked(&self.as_slice()[1..2])
198    }
199    pub fn nth2(&self) -> ByteReader<'r> {
200        ByteReader::new_unchecked(&self.as_slice()[2..3])
201    }
202    pub fn nth3(&self) -> ByteReader<'r> {
203        ByteReader::new_unchecked(&self.as_slice()[3..4])
204    }
205    pub fn nth4(&self) -> ByteReader<'r> {
206        ByteReader::new_unchecked(&self.as_slice()[4..5])
207    }
208    pub fn nth5(&self) -> ByteReader<'r> {
209        ByteReader::new_unchecked(&self.as_slice()[5..6])
210    }
211    pub fn nth6(&self) -> ByteReader<'r> {
212        ByteReader::new_unchecked(&self.as_slice()[6..7])
213    }
214    pub fn nth7(&self) -> ByteReader<'r> {
215        ByteReader::new_unchecked(&self.as_slice()[7..8])
216    }
217    pub fn nth8(&self) -> ByteReader<'r> {
218        ByteReader::new_unchecked(&self.as_slice()[8..9])
219    }
220    pub fn nth9(&self) -> ByteReader<'r> {
221        ByteReader::new_unchecked(&self.as_slice()[9..10])
222    }
223    pub fn nth10(&self) -> ByteReader<'r> {
224        ByteReader::new_unchecked(&self.as_slice()[10..11])
225    }
226    pub fn nth11(&self) -> ByteReader<'r> {
227        ByteReader::new_unchecked(&self.as_slice()[11..12])
228    }
229    pub fn nth12(&self) -> ByteReader<'r> {
230        ByteReader::new_unchecked(&self.as_slice()[12..13])
231    }
232    pub fn nth13(&self) -> ByteReader<'r> {
233        ByteReader::new_unchecked(&self.as_slice()[13..14])
234    }
235    pub fn nth14(&self) -> ByteReader<'r> {
236        ByteReader::new_unchecked(&self.as_slice()[14..15])
237    }
238    pub fn nth15(&self) -> ByteReader<'r> {
239        ByteReader::new_unchecked(&self.as_slice()[15..16])
240    }
241    pub fn nth16(&self) -> ByteReader<'r> {
242        ByteReader::new_unchecked(&self.as_slice()[16..17])
243    }
244    pub fn nth17(&self) -> ByteReader<'r> {
245        ByteReader::new_unchecked(&self.as_slice()[17..18])
246    }
247    pub fn nth18(&self) -> ByteReader<'r> {
248        ByteReader::new_unchecked(&self.as_slice()[18..19])
249    }
250    pub fn nth19(&self) -> ByteReader<'r> {
251        ByteReader::new_unchecked(&self.as_slice()[19..20])
252    }
253    pub fn nth20(&self) -> ByteReader<'r> {
254        ByteReader::new_unchecked(&self.as_slice()[20..21])
255    }
256    pub fn raw_data(&self) -> &'r [u8] {
257        self.as_slice()
258    }
259}
260impl<'r> molecule::prelude::Reader<'r> for AuthReader<'r> {
261    type Entity = Auth;
262    const NAME: &'static str = "AuthReader";
263    fn to_entity(&self) -> Self::Entity {
264        Self::Entity::new_unchecked(self.as_slice().to_owned().into())
265    }
266    fn new_unchecked(slice: &'r [u8]) -> Self {
267        AuthReader(slice)
268    }
269    fn as_slice(&self) -> &'r [u8] {
270        self.0
271    }
272    fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
273        use molecule::verification_error as ve;
274        let slice_len = slice.len();
275        if slice_len != Self::TOTAL_SIZE {
276            return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
277        }
278        Ok(())
279    }
280}
281pub struct AuthBuilder(pub(crate) [Byte; 21]);
282impl ::core::fmt::Debug for AuthBuilder {
283    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
284        write!(f, "{}({:?})", Self::NAME, &self.0[..])
285    }
286}
287impl ::core::default::Default for AuthBuilder {
288    fn default() -> Self {
289        AuthBuilder([
290            Byte::default(),
291            Byte::default(),
292            Byte::default(),
293            Byte::default(),
294            Byte::default(),
295            Byte::default(),
296            Byte::default(),
297            Byte::default(),
298            Byte::default(),
299            Byte::default(),
300            Byte::default(),
301            Byte::default(),
302            Byte::default(),
303            Byte::default(),
304            Byte::default(),
305            Byte::default(),
306            Byte::default(),
307            Byte::default(),
308            Byte::default(),
309            Byte::default(),
310            Byte::default(),
311        ])
312    }
313}
314impl AuthBuilder {
315    pub const TOTAL_SIZE: usize = 21;
316    pub const ITEM_SIZE: usize = 1;
317    pub const ITEM_COUNT: usize = 21;
318    pub fn set(mut self, v: [Byte; 21]) -> Self {
319        self.0 = v;
320        self
321    }
322    pub fn nth0(mut self, v: Byte) -> Self {
323        self.0[0] = v;
324        self
325    }
326    pub fn nth1(mut self, v: Byte) -> Self {
327        self.0[1] = v;
328        self
329    }
330    pub fn nth2(mut self, v: Byte) -> Self {
331        self.0[2] = v;
332        self
333    }
334    pub fn nth3(mut self, v: Byte) -> Self {
335        self.0[3] = v;
336        self
337    }
338    pub fn nth4(mut self, v: Byte) -> Self {
339        self.0[4] = v;
340        self
341    }
342    pub fn nth5(mut self, v: Byte) -> Self {
343        self.0[5] = v;
344        self
345    }
346    pub fn nth6(mut self, v: Byte) -> Self {
347        self.0[6] = v;
348        self
349    }
350    pub fn nth7(mut self, v: Byte) -> Self {
351        self.0[7] = v;
352        self
353    }
354    pub fn nth8(mut self, v: Byte) -> Self {
355        self.0[8] = v;
356        self
357    }
358    pub fn nth9(mut self, v: Byte) -> Self {
359        self.0[9] = v;
360        self
361    }
362    pub fn nth10(mut self, v: Byte) -> Self {
363        self.0[10] = v;
364        self
365    }
366    pub fn nth11(mut self, v: Byte) -> Self {
367        self.0[11] = v;
368        self
369    }
370    pub fn nth12(mut self, v: Byte) -> Self {
371        self.0[12] = v;
372        self
373    }
374    pub fn nth13(mut self, v: Byte) -> Self {
375        self.0[13] = v;
376        self
377    }
378    pub fn nth14(mut self, v: Byte) -> Self {
379        self.0[14] = v;
380        self
381    }
382    pub fn nth15(mut self, v: Byte) -> Self {
383        self.0[15] = v;
384        self
385    }
386    pub fn nth16(mut self, v: Byte) -> Self {
387        self.0[16] = v;
388        self
389    }
390    pub fn nth17(mut self, v: Byte) -> Self {
391        self.0[17] = v;
392        self
393    }
394    pub fn nth18(mut self, v: Byte) -> Self {
395        self.0[18] = v;
396        self
397    }
398    pub fn nth19(mut self, v: Byte) -> Self {
399        self.0[19] = v;
400        self
401    }
402    pub fn nth20(mut self, v: Byte) -> Self {
403        self.0[20] = v;
404        self
405    }
406}
407impl molecule::prelude::Builder for AuthBuilder {
408    type Entity = Auth;
409    const NAME: &'static str = "AuthBuilder";
410    fn expected_length(&self) -> usize {
411        Self::TOTAL_SIZE
412    }
413    fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
414        writer.write_all(self.0[0].as_slice())?;
415        writer.write_all(self.0[1].as_slice())?;
416        writer.write_all(self.0[2].as_slice())?;
417        writer.write_all(self.0[3].as_slice())?;
418        writer.write_all(self.0[4].as_slice())?;
419        writer.write_all(self.0[5].as_slice())?;
420        writer.write_all(self.0[6].as_slice())?;
421        writer.write_all(self.0[7].as_slice())?;
422        writer.write_all(self.0[8].as_slice())?;
423        writer.write_all(self.0[9].as_slice())?;
424        writer.write_all(self.0[10].as_slice())?;
425        writer.write_all(self.0[11].as_slice())?;
426        writer.write_all(self.0[12].as_slice())?;
427        writer.write_all(self.0[13].as_slice())?;
428        writer.write_all(self.0[14].as_slice())?;
429        writer.write_all(self.0[15].as_slice())?;
430        writer.write_all(self.0[16].as_slice())?;
431        writer.write_all(self.0[17].as_slice())?;
432        writer.write_all(self.0[18].as_slice())?;
433        writer.write_all(self.0[19].as_slice())?;
434        writer.write_all(self.0[20].as_slice())?;
435        Ok(())
436    }
437    fn build(&self) -> Self::Entity {
438        let mut inner = Vec::with_capacity(self.expected_length());
439        self.write(&mut inner)
440            .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
441        Auth::new_unchecked(inner.into())
442    }
443}
444#[derive(Clone)]
445pub struct Identity(molecule::bytes::Bytes);
446impl ::core::fmt::LowerHex for Identity {
447    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
448        use molecule::hex_string;
449        if f.alternate() {
450            write!(f, "0x")?;
451        }
452        write!(f, "{}", hex_string(self.as_slice()))
453    }
454}
455impl ::core::fmt::Debug for Identity {
456    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
457        write!(f, "{}({:#x})", Self::NAME, self)
458    }
459}
460impl ::core::fmt::Display for Identity {
461    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
462        write!(f, "{} {{ ", Self::NAME)?;
463        write!(f, "{}: {}", "identity", self.identity())?;
464        write!(f, ", {}: {}", "proofs", self.proofs())?;
465        let extra_count = self.count_extra_fields();
466        if extra_count != 0 {
467            write!(f, ", .. ({} fields)", extra_count)?;
468        }
469        write!(f, " }}")
470    }
471}
472impl ::core::default::Default for Identity {
473    fn default() -> Self {
474        let v: Vec<u8> = vec![
475            37, 0, 0, 0, 12, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
476            0, 0, 0, 0, 0, 4, 0, 0, 0,
477        ];
478        Identity::new_unchecked(v.into())
479    }
480}
481impl Identity {
482    pub const FIELD_COUNT: usize = 2;
483    pub fn total_size(&self) -> usize {
484        molecule::unpack_number(self.as_slice()) as usize
485    }
486    pub fn field_count(&self) -> usize {
487        if self.total_size() == molecule::NUMBER_SIZE {
488            0
489        } else {
490            (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
491        }
492    }
493    pub fn count_extra_fields(&self) -> usize {
494        self.field_count() - Self::FIELD_COUNT
495    }
496    pub fn has_extra_fields(&self) -> bool {
497        Self::FIELD_COUNT != self.field_count()
498    }
499    pub fn identity(&self) -> Auth {
500        let slice = self.as_slice();
501        let start = molecule::unpack_number(&slice[4..]) as usize;
502        let end = molecule::unpack_number(&slice[8..]) as usize;
503        Auth::new_unchecked(self.0.slice(start..end))
504    }
505    pub fn proofs(&self) -> SmtProofEntryVec {
506        let slice = self.as_slice();
507        let start = molecule::unpack_number(&slice[8..]) as usize;
508        if self.has_extra_fields() {
509            let end = molecule::unpack_number(&slice[12..]) as usize;
510            SmtProofEntryVec::new_unchecked(self.0.slice(start..end))
511        } else {
512            SmtProofEntryVec::new_unchecked(self.0.slice(start..))
513        }
514    }
515    pub fn as_reader<'r>(&'r self) -> IdentityReader<'r> {
516        IdentityReader::new_unchecked(self.as_slice())
517    }
518}
519impl molecule::prelude::Entity for Identity {
520    type Builder = IdentityBuilder;
521    const NAME: &'static str = "Identity";
522    fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
523        Identity(data)
524    }
525    fn as_bytes(&self) -> molecule::bytes::Bytes {
526        self.0.clone()
527    }
528    fn as_slice(&self) -> &[u8] {
529        &self.0[..]
530    }
531    fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
532        IdentityReader::from_slice(slice).map(|reader| reader.to_entity())
533    }
534    fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
535        IdentityReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
536    }
537    fn new_builder() -> Self::Builder {
538        ::core::default::Default::default()
539    }
540    fn as_builder(self) -> Self::Builder {
541        Self::new_builder()
542            .identity(self.identity())
543            .proofs(self.proofs())
544    }
545}
546#[derive(Clone, Copy)]
547pub struct IdentityReader<'r>(&'r [u8]);
548impl<'r> ::core::fmt::LowerHex for IdentityReader<'r> {
549    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
550        use molecule::hex_string;
551        if f.alternate() {
552            write!(f, "0x")?;
553        }
554        write!(f, "{}", hex_string(self.as_slice()))
555    }
556}
557impl<'r> ::core::fmt::Debug for IdentityReader<'r> {
558    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
559        write!(f, "{}({:#x})", Self::NAME, self)
560    }
561}
562impl<'r> ::core::fmt::Display for IdentityReader<'r> {
563    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
564        write!(f, "{} {{ ", Self::NAME)?;
565        write!(f, "{}: {}", "identity", self.identity())?;
566        write!(f, ", {}: {}", "proofs", self.proofs())?;
567        let extra_count = self.count_extra_fields();
568        if extra_count != 0 {
569            write!(f, ", .. ({} fields)", extra_count)?;
570        }
571        write!(f, " }}")
572    }
573}
574impl<'r> IdentityReader<'r> {
575    pub const FIELD_COUNT: usize = 2;
576    pub fn total_size(&self) -> usize {
577        molecule::unpack_number(self.as_slice()) as usize
578    }
579    pub fn field_count(&self) -> usize {
580        if self.total_size() == molecule::NUMBER_SIZE {
581            0
582        } else {
583            (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
584        }
585    }
586    pub fn count_extra_fields(&self) -> usize {
587        self.field_count() - Self::FIELD_COUNT
588    }
589    pub fn has_extra_fields(&self) -> bool {
590        Self::FIELD_COUNT != self.field_count()
591    }
592    pub fn identity(&self) -> AuthReader<'r> {
593        let slice = self.as_slice();
594        let start = molecule::unpack_number(&slice[4..]) as usize;
595        let end = molecule::unpack_number(&slice[8..]) as usize;
596        AuthReader::new_unchecked(&self.as_slice()[start..end])
597    }
598    pub fn proofs(&self) -> SmtProofEntryVecReader<'r> {
599        let slice = self.as_slice();
600        let start = molecule::unpack_number(&slice[8..]) as usize;
601        if self.has_extra_fields() {
602            let end = molecule::unpack_number(&slice[12..]) as usize;
603            SmtProofEntryVecReader::new_unchecked(&self.as_slice()[start..end])
604        } else {
605            SmtProofEntryVecReader::new_unchecked(&self.as_slice()[start..])
606        }
607    }
608}
609impl<'r> molecule::prelude::Reader<'r> for IdentityReader<'r> {
610    type Entity = Identity;
611    const NAME: &'static str = "IdentityReader";
612    fn to_entity(&self) -> Self::Entity {
613        Self::Entity::new_unchecked(self.as_slice().to_owned().into())
614    }
615    fn new_unchecked(slice: &'r [u8]) -> Self {
616        IdentityReader(slice)
617    }
618    fn as_slice(&self) -> &'r [u8] {
619        self.0
620    }
621    fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
622        use molecule::verification_error as ve;
623        let slice_len = slice.len();
624        if slice_len < molecule::NUMBER_SIZE {
625            return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
626        }
627        let total_size = molecule::unpack_number(slice) as usize;
628        if slice_len != total_size {
629            return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
630        }
631        if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
632            return Ok(());
633        }
634        if slice_len < molecule::NUMBER_SIZE * 2 {
635            return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
636        }
637        let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
638        if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
639            return ve!(Self, OffsetsNotMatch);
640        }
641        if slice_len < offset_first {
642            return ve!(Self, HeaderIsBroken, offset_first, slice_len);
643        }
644        let field_count = offset_first / molecule::NUMBER_SIZE - 1;
645        if field_count < Self::FIELD_COUNT {
646            return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
647        } else if !compatible && field_count > Self::FIELD_COUNT {
648            return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
649        };
650        let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
651            .chunks_exact(molecule::NUMBER_SIZE)
652            .map(|x| molecule::unpack_number(x) as usize)
653            .collect();
654        offsets.push(total_size);
655        if offsets.windows(2).any(|i| i[0] > i[1]) {
656            return ve!(Self, OffsetsNotMatch);
657        }
658        AuthReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
659        SmtProofEntryVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
660        Ok(())
661    }
662}
663#[derive(Debug, Default)]
664pub struct IdentityBuilder {
665    pub(crate) identity: Auth,
666    pub(crate) proofs: SmtProofEntryVec,
667}
668impl IdentityBuilder {
669    pub const FIELD_COUNT: usize = 2;
670    pub fn identity(mut self, v: Auth) -> Self {
671        self.identity = v;
672        self
673    }
674    pub fn proofs(mut self, v: SmtProofEntryVec) -> Self {
675        self.proofs = v;
676        self
677    }
678}
679impl molecule::prelude::Builder for IdentityBuilder {
680    type Entity = Identity;
681    const NAME: &'static str = "IdentityBuilder";
682    fn expected_length(&self) -> usize {
683        molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
684            + self.identity.as_slice().len()
685            + self.proofs.as_slice().len()
686    }
687    fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
688        let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
689        let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
690        offsets.push(total_size);
691        total_size += self.identity.as_slice().len();
692        offsets.push(total_size);
693        total_size += self.proofs.as_slice().len();
694        writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
695        for offset in offsets.into_iter() {
696            writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
697        }
698        writer.write_all(self.identity.as_slice())?;
699        writer.write_all(self.proofs.as_slice())?;
700        Ok(())
701    }
702    fn build(&self) -> Self::Entity {
703        let mut inner = Vec::with_capacity(self.expected_length());
704        self.write(&mut inner)
705            .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
706        Identity::new_unchecked(inner.into())
707    }
708}
709#[derive(Clone)]
710pub struct IdentityOpt(molecule::bytes::Bytes);
711impl ::core::fmt::LowerHex for IdentityOpt {
712    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
713        use molecule::hex_string;
714        if f.alternate() {
715            write!(f, "0x")?;
716        }
717        write!(f, "{}", hex_string(self.as_slice()))
718    }
719}
720impl ::core::fmt::Debug for IdentityOpt {
721    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
722        write!(f, "{}({:#x})", Self::NAME, self)
723    }
724}
725impl ::core::fmt::Display for IdentityOpt {
726    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
727        if let Some(v) = self.to_opt() {
728            write!(f, "{}(Some({}))", Self::NAME, v)
729        } else {
730            write!(f, "{}(None)", Self::NAME)
731        }
732    }
733}
734impl ::core::default::Default for IdentityOpt {
735    fn default() -> Self {
736        let v: Vec<u8> = vec![];
737        IdentityOpt::new_unchecked(v.into())
738    }
739}
740impl IdentityOpt {
741    pub fn is_none(&self) -> bool {
742        self.0.is_empty()
743    }
744    pub fn is_some(&self) -> bool {
745        !self.0.is_empty()
746    }
747    pub fn to_opt(&self) -> Option<Identity> {
748        if self.is_none() {
749            None
750        } else {
751            Some(Identity::new_unchecked(self.0.clone()))
752        }
753    }
754    pub fn as_reader<'r>(&'r self) -> IdentityOptReader<'r> {
755        IdentityOptReader::new_unchecked(self.as_slice())
756    }
757}
758impl molecule::prelude::Entity for IdentityOpt {
759    type Builder = IdentityOptBuilder;
760    const NAME: &'static str = "IdentityOpt";
761    fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
762        IdentityOpt(data)
763    }
764    fn as_bytes(&self) -> molecule::bytes::Bytes {
765        self.0.clone()
766    }
767    fn as_slice(&self) -> &[u8] {
768        &self.0[..]
769    }
770    fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
771        IdentityOptReader::from_slice(slice).map(|reader| reader.to_entity())
772    }
773    fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
774        IdentityOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
775    }
776    fn new_builder() -> Self::Builder {
777        ::core::default::Default::default()
778    }
779    fn as_builder(self) -> Self::Builder {
780        Self::new_builder().set(self.to_opt())
781    }
782}
783#[derive(Clone, Copy)]
784pub struct IdentityOptReader<'r>(&'r [u8]);
785impl<'r> ::core::fmt::LowerHex for IdentityOptReader<'r> {
786    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
787        use molecule::hex_string;
788        if f.alternate() {
789            write!(f, "0x")?;
790        }
791        write!(f, "{}", hex_string(self.as_slice()))
792    }
793}
794impl<'r> ::core::fmt::Debug for IdentityOptReader<'r> {
795    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
796        write!(f, "{}({:#x})", Self::NAME, self)
797    }
798}
799impl<'r> ::core::fmt::Display for IdentityOptReader<'r> {
800    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
801        if let Some(v) = self.to_opt() {
802            write!(f, "{}(Some({}))", Self::NAME, v)
803        } else {
804            write!(f, "{}(None)", Self::NAME)
805        }
806    }
807}
808impl<'r> IdentityOptReader<'r> {
809    pub fn is_none(&self) -> bool {
810        self.0.is_empty()
811    }
812    pub fn is_some(&self) -> bool {
813        !self.0.is_empty()
814    }
815    pub fn to_opt(&self) -> Option<IdentityReader<'r>> {
816        if self.is_none() {
817            None
818        } else {
819            Some(IdentityReader::new_unchecked(self.as_slice()))
820        }
821    }
822}
823impl<'r> molecule::prelude::Reader<'r> for IdentityOptReader<'r> {
824    type Entity = IdentityOpt;
825    const NAME: &'static str = "IdentityOptReader";
826    fn to_entity(&self) -> Self::Entity {
827        Self::Entity::new_unchecked(self.as_slice().to_owned().into())
828    }
829    fn new_unchecked(slice: &'r [u8]) -> Self {
830        IdentityOptReader(slice)
831    }
832    fn as_slice(&self) -> &'r [u8] {
833        self.0
834    }
835    fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
836        if !slice.is_empty() {
837            IdentityReader::verify(&slice[..], compatible)?;
838        }
839        Ok(())
840    }
841}
842#[derive(Debug, Default)]
843pub struct IdentityOptBuilder(pub(crate) Option<Identity>);
844impl IdentityOptBuilder {
845    pub fn set(mut self, v: Option<Identity>) -> Self {
846        self.0 = v;
847        self
848    }
849}
850impl molecule::prelude::Builder for IdentityOptBuilder {
851    type Entity = IdentityOpt;
852    const NAME: &'static str = "IdentityOptBuilder";
853    fn expected_length(&self) -> usize {
854        self.0
855            .as_ref()
856            .map(|ref inner| inner.as_slice().len())
857            .unwrap_or(0)
858    }
859    fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
860        self.0
861            .as_ref()
862            .map(|ref inner| writer.write_all(inner.as_slice()))
863            .unwrap_or(Ok(()))
864    }
865    fn build(&self) -> Self::Entity {
866        let mut inner = Vec::with_capacity(self.expected_length());
867        self.write(&mut inner)
868            .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
869        IdentityOpt::new_unchecked(inner.into())
870    }
871}
872#[derive(Clone)]
873pub struct OmniLockWitnessLock(molecule::bytes::Bytes);
874impl ::core::fmt::LowerHex for OmniLockWitnessLock {
875    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
876        use molecule::hex_string;
877        if f.alternate() {
878            write!(f, "0x")?;
879        }
880        write!(f, "{}", hex_string(self.as_slice()))
881    }
882}
883impl ::core::fmt::Debug for OmniLockWitnessLock {
884    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
885        write!(f, "{}({:#x})", Self::NAME, self)
886    }
887}
888impl ::core::fmt::Display for OmniLockWitnessLock {
889    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
890        write!(f, "{} {{ ", Self::NAME)?;
891        write!(f, "{}: {}", "signature", self.signature())?;
892        write!(f, ", {}: {}", "omni_identity", self.omni_identity())?;
893        write!(f, ", {}: {}", "preimage", self.preimage())?;
894        let extra_count = self.count_extra_fields();
895        if extra_count != 0 {
896            write!(f, ", .. ({} fields)", extra_count)?;
897        }
898        write!(f, " }}")
899    }
900}
901impl ::core::default::Default for OmniLockWitnessLock {
902    fn default() -> Self {
903        let v: Vec<u8> = vec![16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0];
904        OmniLockWitnessLock::new_unchecked(v.into())
905    }
906}
907impl OmniLockWitnessLock {
908    pub const FIELD_COUNT: usize = 3;
909    pub fn total_size(&self) -> usize {
910        molecule::unpack_number(self.as_slice()) as usize
911    }
912    pub fn field_count(&self) -> usize {
913        if self.total_size() == molecule::NUMBER_SIZE {
914            0
915        } else {
916            (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
917        }
918    }
919    pub fn count_extra_fields(&self) -> usize {
920        self.field_count() - Self::FIELD_COUNT
921    }
922    pub fn has_extra_fields(&self) -> bool {
923        Self::FIELD_COUNT != self.field_count()
924    }
925    pub fn signature(&self) -> BytesOpt {
926        let slice = self.as_slice();
927        let start = molecule::unpack_number(&slice[4..]) as usize;
928        let end = molecule::unpack_number(&slice[8..]) as usize;
929        BytesOpt::new_unchecked(self.0.slice(start..end))
930    }
931    pub fn omni_identity(&self) -> IdentityOpt {
932        let slice = self.as_slice();
933        let start = molecule::unpack_number(&slice[8..]) as usize;
934        let end = molecule::unpack_number(&slice[12..]) as usize;
935        IdentityOpt::new_unchecked(self.0.slice(start..end))
936    }
937    pub fn preimage(&self) -> BytesOpt {
938        let slice = self.as_slice();
939        let start = molecule::unpack_number(&slice[12..]) as usize;
940        if self.has_extra_fields() {
941            let end = molecule::unpack_number(&slice[16..]) as usize;
942            BytesOpt::new_unchecked(self.0.slice(start..end))
943        } else {
944            BytesOpt::new_unchecked(self.0.slice(start..))
945        }
946    }
947    pub fn as_reader<'r>(&'r self) -> OmniLockWitnessLockReader<'r> {
948        OmniLockWitnessLockReader::new_unchecked(self.as_slice())
949    }
950}
951impl molecule::prelude::Entity for OmniLockWitnessLock {
952    type Builder = OmniLockWitnessLockBuilder;
953    const NAME: &'static str = "OmniLockWitnessLock";
954    fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
955        OmniLockWitnessLock(data)
956    }
957    fn as_bytes(&self) -> molecule::bytes::Bytes {
958        self.0.clone()
959    }
960    fn as_slice(&self) -> &[u8] {
961        &self.0[..]
962    }
963    fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
964        OmniLockWitnessLockReader::from_slice(slice).map(|reader| reader.to_entity())
965    }
966    fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
967        OmniLockWitnessLockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
968    }
969    fn new_builder() -> Self::Builder {
970        ::core::default::Default::default()
971    }
972    fn as_builder(self) -> Self::Builder {
973        Self::new_builder()
974            .signature(self.signature())
975            .omni_identity(self.omni_identity())
976            .preimage(self.preimage())
977    }
978}
979#[derive(Clone, Copy)]
980pub struct OmniLockWitnessLockReader<'r>(&'r [u8]);
981impl<'r> ::core::fmt::LowerHex for OmniLockWitnessLockReader<'r> {
982    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
983        use molecule::hex_string;
984        if f.alternate() {
985            write!(f, "0x")?;
986        }
987        write!(f, "{}", hex_string(self.as_slice()))
988    }
989}
990impl<'r> ::core::fmt::Debug for OmniLockWitnessLockReader<'r> {
991    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
992        write!(f, "{}({:#x})", Self::NAME, self)
993    }
994}
995impl<'r> ::core::fmt::Display for OmniLockWitnessLockReader<'r> {
996    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
997        write!(f, "{} {{ ", Self::NAME)?;
998        write!(f, "{}: {}", "signature", self.signature())?;
999        write!(f, ", {}: {}", "omni_identity", self.omni_identity())?;
1000        write!(f, ", {}: {}", "preimage", self.preimage())?;
1001        let extra_count = self.count_extra_fields();
1002        if extra_count != 0 {
1003            write!(f, ", .. ({} fields)", extra_count)?;
1004        }
1005        write!(f, " }}")
1006    }
1007}
1008impl<'r> OmniLockWitnessLockReader<'r> {
1009    pub const FIELD_COUNT: usize = 3;
1010    pub fn total_size(&self) -> usize {
1011        molecule::unpack_number(self.as_slice()) as usize
1012    }
1013    pub fn field_count(&self) -> usize {
1014        if self.total_size() == molecule::NUMBER_SIZE {
1015            0
1016        } else {
1017            (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
1018        }
1019    }
1020    pub fn count_extra_fields(&self) -> usize {
1021        self.field_count() - Self::FIELD_COUNT
1022    }
1023    pub fn has_extra_fields(&self) -> bool {
1024        Self::FIELD_COUNT != self.field_count()
1025    }
1026    pub fn signature(&self) -> BytesOptReader<'r> {
1027        let slice = self.as_slice();
1028        let start = molecule::unpack_number(&slice[4..]) as usize;
1029        let end = molecule::unpack_number(&slice[8..]) as usize;
1030        BytesOptReader::new_unchecked(&self.as_slice()[start..end])
1031    }
1032    pub fn omni_identity(&self) -> IdentityOptReader<'r> {
1033        let slice = self.as_slice();
1034        let start = molecule::unpack_number(&slice[8..]) as usize;
1035        let end = molecule::unpack_number(&slice[12..]) as usize;
1036        IdentityOptReader::new_unchecked(&self.as_slice()[start..end])
1037    }
1038    pub fn preimage(&self) -> BytesOptReader<'r> {
1039        let slice = self.as_slice();
1040        let start = molecule::unpack_number(&slice[12..]) as usize;
1041        if self.has_extra_fields() {
1042            let end = molecule::unpack_number(&slice[16..]) as usize;
1043            BytesOptReader::new_unchecked(&self.as_slice()[start..end])
1044        } else {
1045            BytesOptReader::new_unchecked(&self.as_slice()[start..])
1046        }
1047    }
1048}
1049impl<'r> molecule::prelude::Reader<'r> for OmniLockWitnessLockReader<'r> {
1050    type Entity = OmniLockWitnessLock;
1051    const NAME: &'static str = "OmniLockWitnessLockReader";
1052    fn to_entity(&self) -> Self::Entity {
1053        Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1054    }
1055    fn new_unchecked(slice: &'r [u8]) -> Self {
1056        OmniLockWitnessLockReader(slice)
1057    }
1058    fn as_slice(&self) -> &'r [u8] {
1059        self.0
1060    }
1061    fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
1062        use molecule::verification_error as ve;
1063        let slice_len = slice.len();
1064        if slice_len < molecule::NUMBER_SIZE {
1065            return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
1066        }
1067        let total_size = molecule::unpack_number(slice) as usize;
1068        if slice_len != total_size {
1069            return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
1070        }
1071        if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
1072            return Ok(());
1073        }
1074        if slice_len < molecule::NUMBER_SIZE * 2 {
1075            return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
1076        }
1077        let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
1078        if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
1079            return ve!(Self, OffsetsNotMatch);
1080        }
1081        if slice_len < offset_first {
1082            return ve!(Self, HeaderIsBroken, offset_first, slice_len);
1083        }
1084        let field_count = offset_first / molecule::NUMBER_SIZE - 1;
1085        if field_count < Self::FIELD_COUNT {
1086            return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
1087        } else if !compatible && field_count > Self::FIELD_COUNT {
1088            return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
1089        };
1090        let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
1091            .chunks_exact(molecule::NUMBER_SIZE)
1092            .map(|x| molecule::unpack_number(x) as usize)
1093            .collect();
1094        offsets.push(total_size);
1095        if offsets.windows(2).any(|i| i[0] > i[1]) {
1096            return ve!(Self, OffsetsNotMatch);
1097        }
1098        BytesOptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
1099        IdentityOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
1100        BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
1101        Ok(())
1102    }
1103}
1104#[derive(Debug, Default)]
1105pub struct OmniLockWitnessLockBuilder {
1106    pub(crate) signature: BytesOpt,
1107    pub(crate) omni_identity: IdentityOpt,
1108    pub(crate) preimage: BytesOpt,
1109}
1110impl OmniLockWitnessLockBuilder {
1111    pub const FIELD_COUNT: usize = 3;
1112    pub fn signature(mut self, v: BytesOpt) -> Self {
1113        self.signature = v;
1114        self
1115    }
1116    pub fn omni_identity(mut self, v: IdentityOpt) -> Self {
1117        self.omni_identity = v;
1118        self
1119    }
1120    pub fn preimage(mut self, v: BytesOpt) -> Self {
1121        self.preimage = v;
1122        self
1123    }
1124}
1125impl molecule::prelude::Builder for OmniLockWitnessLockBuilder {
1126    type Entity = OmniLockWitnessLock;
1127    const NAME: &'static str = "OmniLockWitnessLockBuilder";
1128    fn expected_length(&self) -> usize {
1129        molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
1130            + self.signature.as_slice().len()
1131            + self.omni_identity.as_slice().len()
1132            + self.preimage.as_slice().len()
1133    }
1134    fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1135        let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
1136        let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
1137        offsets.push(total_size);
1138        total_size += self.signature.as_slice().len();
1139        offsets.push(total_size);
1140        total_size += self.omni_identity.as_slice().len();
1141        offsets.push(total_size);
1142        total_size += self.preimage.as_slice().len();
1143        writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
1144        for offset in offsets.into_iter() {
1145            writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
1146        }
1147        writer.write_all(self.signature.as_slice())?;
1148        writer.write_all(self.omni_identity.as_slice())?;
1149        writer.write_all(self.preimage.as_slice())?;
1150        Ok(())
1151    }
1152    fn build(&self) -> Self::Entity {
1153        let mut inner = Vec::with_capacity(self.expected_length());
1154        self.write(&mut inner)
1155            .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1156        OmniLockWitnessLock::new_unchecked(inner.into())
1157    }
1158}