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