tonstruct/lib.rs
1//! TON transaction body *de*serialization crate
2//!
3//! These docs are not 100% ready. You can help us to improve it
4//!
5//! ## Example
6//!
7//! ## Deserialization from Cell
8//!
9//! To parse transaction body you need to derive `FromCell` macro.
10//! In example below we parse transaction body from jetton transfer
11//!
12//! ```rust
13//! use tonstruct::{FromCell, fields::{Uint, Coins, Address, Int, CellRef, Comment}};
14//!
15//! #[derive(FromCell, Debug, PartialEq)]
16//! struct ForwardPayload {
17//! is_right: bool,
18//! text_comment: CellRef<Comment>,
19//! }
20//!
21//! #[derive(FromCell, Debug, PartialEq)]
22//! struct CustomPayload {}
23//!
24//! #[derive(FromCell, Debug, PartialEq)]
25//! struct Message {
26//! op_code: Uint<32>,
27//! query_id: Uint<64>,
28//! amount: Coins,
29//! destination: Address,
30//! response_destination: Address,
31//! custom_payload: Option<CustomPayload>,
32//! forward_ton_amount: Coins,
33//! forward_payload: ForwardPayload,
34//! }
35//!
36//! fn parse_body(cell: tonlib_core::cell::Cell) -> Message {
37//! <Message as FromCell>::from_cell(cell).unwrap()
38//! }
39//! ```
40//!
41//! ## Serialization to Cell
42//!
43//! ```rust
44//! use tonstruct::{ToCell, fields::{Comment}};
45//!
46//! #[derive(ToCell)]
47//! struct Message {
48//! field: Comment
49//! }
50//!
51//! fn struct_to_cell(message: Message) -> tonlib_core::cell::Cell {
52//! message.to_cell().unwrap()
53//! }
54//! ```
55//!
56//! # Implemented TON types
57//!
58//! To see all implemented TON types and its documentation open [`fields`] page
59
60mod from_cell;
61mod to_cell;
62mod types;
63mod utils;
64
65pub use from_cell::FromCell;
66pub use to_cell::ToCell;
67pub use tonstruct_proc_macro::{FromCell, ToCell};
68pub mod fields {
69 //! # Implemented TON types
70 //!
71 //! ## Boolean
72 //!
73 //! TON boolean implemented using built-in Rust [`bool`]
74 //!
75 //! ```rust
76 //! use tonstruct::{ToCell, FromCell};
77 //!
78 //! #[derive(ToCell, FromCell)]
79 //! struct Message {
80 //! boolean_field: bool,
81 //! }
82 //! ```
83 //!
84 //! ## [`Int`]
85 //!
86 //! TON integer fields implemented using custom wrapper structure [`Int`]
87 //!
88 //! Bits count is specified by adding const generic parameter (e.g. 32 bit integer would be look like `Int<32>`)
89 //!
90 //! ```rust
91 //! use tonstruct::{ToCell, FromCell};
92 //! use tonstruct::fields::Int;
93 //!
94 //! #[derive(ToCell, FromCell)]
95 //! struct Message {
96 //! integer_field: Int<32>,
97 //! }
98 //! ```
99 //! ## [`Uint`]
100 //!
101 //! Unsigned integer type is similar to regular Int type.
102 //!
103 //! Bits count is also specified by adding const generic parameter (e.g. 32 bit unsigned integer would be look like `Uint<32>`)
104 //!
105 //! ```rust
106 //! use tonstruct::{ToCell, FromCell};
107 //! use tonstruct::fields::Uint;
108 //!
109 //! #[derive(ToCell, FromCell)]
110 //! struct Message {
111 //! unsigned_integer_field: Uint<32>,
112 //! }
113 //! ```
114 //!
115 //! ## [`Coins`]
116 //!
117 //! Coins is just like integers but without bits count
118 //!
119 //! ```rust
120 //! use tonstruct::{ToCell, FromCell};
121 //! use tonstruct::fields::Coins;
122 //!
123 //! #[derive(ToCell, FromCell)]
124 //! struct Message {
125 //! unsigned_integer_field: Coins,
126 //! }
127 //! ```
128 //!
129 //! ## [`Address`]
130 //!
131 //! ```rust
132 //! use tonstruct::{ToCell, FromCell};
133 //! use tonstruct::fields::Address;
134 //!
135 //! #[derive(ToCell, FromCell)]
136 //! struct Message {
137 //! address_field: Address,
138 //! }
139 //! ```
140 //!
141 //! ## Optional field
142 //!
143 //! Optional fields implemented using build-in [`Option`] enum that checks 1 bit before parsing.
144 //! If that bit is `0b01` so enum will be resolved as [`Some`].
145 //! Otherwise, as [`None`]
146 //!
147 //! ```rust
148 //! use tonstruct::{ToCell, FromCell};
149 //! use tonstruct::fields::Address;
150 //!
151 //! #[derive(ToCell, FromCell)]
152 //! struct Message {
153 //! optional_address_field: Option<Address>,
154 //! }
155 //! ```
156 //!
157 //! ## String
158 //!
159 //! Regular string (simple utf8 byte sequence) is implemented using built-in [`String`] type.
160 //!
161 //! See below other string types!
162 //!
163 //! ```rust
164 //! use tonstruct::{ToCell, FromCell};
165 //!
166 //! #[derive(ToCell, FromCell)]
167 //! struct Message {
168 //! string_field: String,
169 //! }
170 //! ```
171 //!
172 //! ## [`Comment`]
173 //!
174 //! It's just a simple string but prefixed with 4 null bytes
175 //!
176 //! ```rust
177 //! use tonstruct::{ToCell, FromCell};
178 //! use tonstruct::fields::Comment;
179 //!
180 //! #[derive(ToCell, FromCell)]
181 //! struct Message {
182 //! comment_string_field: Comment,
183 //! }
184 //! ```
185 //!
186 //! # Structures
187 //!
188 //! ## Simple
189 //!
190 //! You can use simple sub structures. Its fields will be parsed sequentially without loading reference Cell.
191 //!
192 //! ```rust
193 //! use tonstruct::{ToCell, FromCell};
194 //!
195 //! #[derive(ToCell, FromCell)]
196 //! struct Sub {
197 //! // Some fields
198 //! }
199 //!
200 //! #[derive(ToCell, FromCell)]
201 //! struct Message {
202 //! substruct: Sub,
203 //! }
204 //! ```
205 //!
206 //! ## [`CellRef`]
207 //!
208 //! This wrapper is used when you need to parse structure in separated referenced Cell
209 //!
210 //! ```rust
211 //! use tonstruct::{ToCell, FromCell};
212 //! use tonstruct::fields::CellRef;
213 //!
214 //! #[derive(ToCell, FromCell)]
215 //! struct Sub {
216 //! // Some fields
217 //! }
218 //!
219 //! #[derive(ToCell, FromCell)]
220 //! struct Message {
221 //! substruct: CellRef<Sub>,
222 //! }
223 //! ```
224 pub use crate::types::address::Address;
225 pub use crate::types::cell_ref::CellRef;
226 pub use crate::types::coins::Coins;
227 pub use crate::types::comment::Comment;
228 pub use crate::types::int::Int;
229 pub use crate::types::uint::Uint;
230}