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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
pub use ;
pub use ;
use crateResult;
use BytesMut;
use Config;
use ;
pub use ;
/// Serializes a given value into a binary format using the default configuration.
///
/// # Default Configuration
/// - **Optional Strategy**: Tagged (uses a single byte to indicate `Some` or `None`)
/// - **Endianness**: Little-endian
/// - **Limit**: No size limit
/// - **Container Length**: 4 bytes (used to encode the length of sequences, strings, etc.)
///
/// # Parameters
/// - `value`: A reference to the value to be serialized. The value must implement the `Serialize` trait.
///
/// # Returns
/// - `Ok(BytesMut)`: The serialized binary representation of the value.
/// - `Err(Error)`: An error if serialization fails or exceeds the configured limit.
///
/// # Example
/// ```rust
/// use binja::{to_bytes, BinarySerialize};
///
/// #[derive(BinarySerialize)]
/// struct Example {
/// field1: u32,
/// field2: Option<u32>,
/// }
///
/// let value = Example {
/// field1: 42,
/// field2: Some(7),
/// };
///
/// let serialized = to_bytes(&value).unwrap().to_vec();
/// assert_eq!(serialized, vec![0x2A, 0x0, 0x0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x0]);
/// ```
/// See [`to_bytes`].
/// Serializes a given value into a binary format using a custom configuration.
///
/// # Parameters
/// - `value`: A reference to the value to be serialized. The value must implement the `Serialize` trait.
/// - `config`: A `Config` object specifying the serialization settings (e.g., endianness, optional strategy, etc.).
///
/// # Returns
/// - `Ok(BytesMut)`: The serialized binary representation of the value.
/// - `Err(Error)`: An error if serialization fails or exceeds the configured limit.
///
/// # Example
/// ```rust
/// use binja::{to_bytes_with_config, BinarySerialize};
/// use binja::config::{Config, EndiannessStrategy, OptionalStrategy};
///
/// #[derive(BinarySerialize)]
/// struct Example {
/// field1: u32,
/// field2: Option<u32>,
/// }
///
/// let config = Config {
/// endianness_strategy: EndiannessStrategy::Big,
/// optional_strategy: OptionalStrategy::Tagged,
/// ..Default::default()
/// };
///
/// let value = Example {
/// field1: 42,
/// field2: Some(7),
/// };
///
/// let serialized = to_bytes_with_config(&value, config).unwrap().to_vec();
/// assert_eq!(serialized, vec![0x00, 0x00, 0x00, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x07]);
/// ```
/// See [`to_bytes_with_config`].
/// Deserializes a binary slice into a value of type `T` using the default configuration.
///
/// # Default Configuration
/// - **Optional Strategy**: Tagged (uses a single byte to indicate `Some` or `None`)
/// - **Endianness**: Little-endian
/// - **Limit**: No size limit
/// - **Container Length**: 4 bytes (used to decode the length of sequences, strings, etc.)
///
/// # Parameters
/// - `bytes`: The binary slice to deserialize. Must represent a valid serialized value of type `T`.
///
/// # Returns
/// - `Ok((T, usize))`: The deserialized value and the number of bytes read.
/// - `Err(Error)`: If deserialization fails or the input is invalid.
///
/// # Example
/// ```rust
/// use binja::{from_bytes, BinaryParse};
///
/// #[derive(BinaryParse, PartialEq, Debug)]
/// struct Example {
/// field1: u32,
/// field2: Option<u32>,
/// }
///
/// let bytes = vec![0x2A, 0x0, 0x0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x0];
/// let (value, size): (Example, usize) = from_bytes(&bytes).unwrap();
/// assert_eq!(
/// value,
/// Example {
/// field1: 42,
/// field2: Some(7),
/// }
/// );
/// assert_eq!(size, 0);
/// ```
/// See [`from_bytes`].
/// Deserializes a binary slice into a value of type `T` using a custom configuration.
///
/// # Parameters
/// - `bytes`: The binary slice to deserialize. Must represent a valid serialized value of type `T`.
/// - `config`: The `Config` specifying deserialization settings (endianness, optional strategy, etc.).
///
/// # Returns
/// - `Ok((T, usize))`: The deserialized value and the number of bytes read.
/// - `Err(Error)`: If deserialization fails or the input is invalid.
///
/// # Example
/// ```rust
/// use binja::{from_bytes_with_config, BinaryParse};
/// use binja::config::{Config, EndiannessStrategy, OptionalStrategy};
///
/// #[derive(BinaryParse, PartialEq, Debug)]
/// struct Example {
/// field1: u32,
/// field2: Option<u32>,
/// }
///
/// let config = Config {
/// endianness_strategy: EndiannessStrategy::Big,
/// optional_strategy: OptionalStrategy::Tagged,
/// ..Default::default()
/// };
///
/// let bytes = vec![0x00, 0x00, 0x00, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x07];
/// let (value, size): (Example, usize) = from_bytes_with_config(&bytes, config).unwrap();
/// assert_eq!(
/// value,
/// Example {
/// field1: 42,
/// field2: Some(7),
/// }
/// );
/// assert_eq!(size, 0);
/// ```
/// See [`from_bytes_with_config`].