1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! ## Example
//!
//! Consider the following TL-B schema:
//!
//! ```tlb
//! tag$10 query_id:uint64 amount:(VarUInteger 16) payload:(Maybe ^Cell) = Hello;
//! ```
//!
//! Let's first define a struct `Hello` that holds these parameters:
//!
//! ```rust
//! # use num_bigint::BigUint;
//! # use tlb::Cell;
//! struct Hello {
//! pub query_id: u64,
//! pub amount: BigUint,
//! pub payload: Option<Cell>,
//! }
//! ```
//!
//! ### **Ser**ialization
//!
//! To be able to **ser**ialize a type to [`Cell`], we should implement
//! [`CellSerialize`](crate::ser::CellSerialize) on it:
//!
//! ```
//! # use num_bigint::BigUint;
//! # use tlb::{
//! # Ref,
//! # bits::{NBits, VarInt, ser::BitWriterExt},
//! # Cell,
//! # ser::{CellSerialize, CellBuilder, CellBuilderError, CellSerializeExt},
//! # StringError,
//! # };
//! #
//! # struct Hello {
//! # pub query_id: u64,
//! # pub amount: BigUint,
//! # pub payload: Option<Cell>,
//! # }
//! impl CellSerialize for Hello {
//! type Args = ();
//!
//! fn store(&self, builder: &mut CellBuilder, _: Self::Args) -> Result<(), CellBuilderError> {
//! builder
//! // tag$10
//! .pack_as::<_, NBits<2>>(0b10, ())?
//! // query_id:uint64
//! .pack(self.query_id, ())?
//! // amount:(VarUInteger 16)
//! .pack_as::<_, &VarInt<4>>(&self.amount, ())?
//! // payload:(Maybe ^Cell)
//! .store_as::<_, Option<Ref>>(self.payload.as_ref(), ())?;
//! Ok(())
//! }
//! }
//!
//! # fn main() -> Result<(), StringError> {
//! // serialize value into cell
//! let hello = Hello {
//! query_id: 0,
//! amount: 1_000u64.into(),
//! payload: None,
//! };
//! let cell = hello.to_cell(())?;
//! # Ok(())
//! # }
//! ```
//!
//! ### **De**serialization
//!
//! To be able to **de**serialize a type from [`Cell`], we should implement
//! [`CellDeserialize`](crate::de::CellDeserialize) on it:
//!
//! ```rust
//! # use num_bigint::BigUint;
//! # use tlb::{
//! # Ref, ParseFully,
//! # bits::{NBits, VarInt, de::BitReaderExt, ser::BitWriterExt},
//! # Cell,
//! # de::{CellDeserialize, CellParser, CellParserError},
//! # Error,
//! # ser::{CellSerialize, CellBuilder, CellBuilderError, CellSerializeExt},
//! # StringError,
//! # };
//! # #[derive(Debug, PartialEq)]
//! # struct Hello {
//! # pub query_id: u64,
//! # pub amount: BigUint,
//! # pub payload: Option<Cell>,
//! # }
//! # impl CellSerialize for Hello {
//! # type Args = ();
//! #
//! # fn store(&self, builder: &mut CellBuilder, _: Self::Args) -> Result<(), CellBuilderError> {
//! # builder
//! # // tag$10
//! # .pack_as::<_, NBits<2>>(0b10, ())?
//! # // query_id:uint64
//! # .pack(self.query_id, ())?
//! # // amount:(VarUInteger 16)
//! # .pack_as::<_, &VarInt<4>>(&self.amount, ())?
//! # // payload:(Maybe ^Cell)
//! # .store_as::<_, Option<Ref>>(self.payload.as_ref(), ())?;
//! # Ok(())
//! # }
//! # }
//! impl<'de> CellDeserialize<'de> for Hello {
//! type Args = ();
//!
//! fn parse(parser: &mut CellParser<'de>, _: Self::Args) -> Result<Self, CellParserError<'de>> {
//! // tag$10
//! let tag: u8 = parser.unpack_as::<_, NBits<2>>(())?;
//! if tag != 0b10 {
//! return Err(Error::custom(format!("unknown tag: {tag:#b}")));
//! }
//! Ok(Self {
//! // query_id:uint64
//! query_id: parser.unpack(())?,
//! // amount:(VarUInteger 16)
//! amount: parser.unpack_as::<_, VarInt<4>>(())?,
//! // payload:(Maybe ^Cell)
//! payload: parser.parse_as::<_, Option<Ref<ParseFully>>>(())?,
//! })
//! }
//! }
//!
//! # fn main() -> Result<(), StringError> {
//! # let orig = Hello {
//! # query_id: 0,
//! # amount: 1_000u64.into(),
//! # payload: None,
//! # };
//! # let cell = orig.to_cell(())?;
//! let mut parser = cell.parser();
//! let hello: Hello = parser.parse(())?;
//! # assert_eq!(hello, orig);
//! # Ok(())
//! # }
//! ```
pub use ;
pub use ;