osal-rs-serde 0.5.1

Serialization/Deserialization framework for osal-rs - extensible and reusable
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/***************************************************************************
 *
 * osal-rs-serde
 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 *
 ***************************************************************************/

//! Serialization traits and implementations.
//!
//! This module provides the core serialization functionality for osal-rs-serde.
//! It defines the [`Serialize`] trait for types that can be serialized and the
//! [`Serializer`] trait for implementing custom serialization formats.
//!
//! # Overview
//!
//! - [`Serialize`]: Trait implemented by types that can be serialized
//! - [`Serializer`]: Trait for implementing custom serialization formats  
//! - [`ByteSerializer`]: Concrete implementation that writes little-endian binary data
//!
//! # Usage with Derive Macro
//!
//! The easiest way to implement serialization is using the derive macro:
//!
//! ```ignore
//! use osal_rs_serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct SensorData {
//!     temperature: i16,
//!     humidity: u8,
//!     pressure: u32,
//! }
//! ```
//!
//! # Manual Implementation
//!
//! For custom serialization logic, implement the trait manually:
//!
//! ```ignore
//! use osal_rs_serde::{Serialize, Serializer};
//!
//! struct Point {
//!     x: i32,
//!     y: i32,
//! }
//!
//! impl Serialize for Point {
//!     fn serialize<S: Serializer>(&self, _name: &str, serializer: &mut S) -> Result<(), S::Error> {
//!         serializer.serialize_i32("x", self.x)?;
//!         serializer.serialize_i32("y", self.y)?;
//!         Ok(())
//!     }
//! }
//! ```
//!
//! # Supported Types
//!
//! The serialization framework supports:
//! - All primitive types (bool, integers, floats)
//! - Arrays `[T; N]` where T: Serialize
//! - Tuples (up to 3 elements)
//! - `Option<T>` where T: Serialize
//! - `Vec<T>` where T: Serialize (requires `alloc`)
//! - `String` and `&str` (requires `alloc` for String)
//! - Custom types implementing `Serialize`

#[cfg(feature = "alloc")]
use alloc::string::String;

use crate::error::{Error, Result};

/// Trait for types that can be serialized.
///
/// This trait should be implemented (or derived) for any type that needs to be serialized.
/// The implementation defines how the type should be written to a serializer.
///
/// # Derive Macro
///
/// The easiest way to implement this trait is using the derive macro (requires `derive` feature):
///
/// ```ignore
/// use osal_rs_serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Config {
///     id: u32,
///     enabled: bool,
///     timeout: Option<u16>,
/// }
/// ```
///
/// # Manual Implementation
///
/// For custom serialization logic or types not supported by the derive macro:
///
/// ```ignore
/// use osal_rs_serde::{Serialize, Serializer};
///
/// struct Point {
///     x: i32,
///     y: i32,
/// }
///
/// impl Serialize for Point {
///     fn serialize<S: Serializer>(&self, serializer: &mut S) -> core::result::Result<(), S::Error> {
///         serializer.serialize_i32("x", self.x)?;
///         serializer.serialize_i32("y", self.y)?;
///         Ok(())
///     }
/// }
/// ```
///
/// # Built-in Implementations
///
/// This trait is already implemented for:
/// - All primitive types (bool, u8-u128, i8-i128, f32, f64)
/// - Arrays `[T; N]` where T: Serialize
/// - Tuples (T1, T2) and (T1, T2, T3) where all T: Serialize
/// - `Option<T>` where T: Serialize
/// - `Vec<T>` where T: Serialize (requires `alloc`)
/// - `String` and `&str` (requires `alloc` for String)
pub trait Serialize {
    /// Serialize this value using the given serializer.
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error>
    where
        S: Serializer;
}

/// Trait that defines how to serialize various types.
///
/// Implementations of this trait determine the output format.
/// For example, `ByteSerializer` writes data in little-endian binary format.
pub trait Serializer: Sized {
    /// The error type that can be returned during serialization.
    type Error: From<Error>;

    /// Serialize a `bool` value.
    fn serialize_bool(&mut self, name: &str, v: bool) -> core::result::Result<(), Self::Error>;

    /// Serialize a `u8` value.
    fn serialize_u8(&mut self, name: &str, v: u8) -> core::result::Result<(), Self::Error>;

    /// Serialize an `i8` value.
    fn serialize_i8(&mut self, name: &str, v: i8) -> core::result::Result<(), Self::Error>;

    /// Serialize a `u16` value.
    fn serialize_u16(&mut self, name: &str, v: u16) -> core::result::Result<(), Self::Error>;

    /// Serialize an `i16` value.
    fn serialize_i16(&mut self, name: &str, v: i16) -> core::result::Result<(), Self::Error>;

    /// Serialize a `u32` value.
    fn serialize_u32(&mut self, name: &str, v: u32) -> core::result::Result<(), Self::Error>;

    /// Serialize an `i32` value.
    fn serialize_i32(&mut self, name: &str, v: i32) -> core::result::Result<(), Self::Error>;

    /// Serialize a `u64` value.
    fn serialize_u64(&mut self, name: &str, v: u64) -> core::result::Result<(), Self::Error>;

    /// Serialize an `i64` value.
    fn serialize_i64(&mut self, name: &str, v: i64) -> core::result::Result<(), Self::Error>;

    /// Serialize a `u128` value.
    fn serialize_u128(&mut self, name: &str, v: u128) -> core::result::Result<(), Self::Error>;

    /// Serialize an `i128` value.
    fn serialize_i128(&mut self, name: &str, v: i128) -> core::result::Result<(), Self::Error>;

    /// Serialize an `f32` value.
    fn serialize_f32(&mut self, name: &str, v: f32) -> core::result::Result<(), Self::Error>;

    /// Serialize an `f64` value.
    fn serialize_f64(&mut self, name: &str, v: f64) -> core::result::Result<(), Self::Error>;

    /// Serialize a byte slice.
    fn serialize_bytes(&mut self, name: &str, v: &[u8]) -> core::result::Result<(), Self::Error>;

    /// Serialize a string.
    fn serialize_string(&mut self, name: &str, v: &String) -> core::result::Result<(), Self::Error>;

    /// Serialize a string slice.
    fn serialize_str(&mut self, name: &str, v: &str) -> core::result::Result<(), Self::Error>;

    /// Serialize a vector of serializable items.
    fn serialize_vec<T>(&mut self, name: &str, v: &alloc::vec::Vec<T>) -> core::result::Result<(), Self::Error>
    where
        T: Serialize;

    /// Serialize an array of serializable items.
    fn serialize_array<T>(&mut self, name: &str, v: &[T]) -> core::result::Result<(), Self::Error>
    where
        T: Serialize;

    /// Begin serializing a struct with the given name and number of fields.
    /// Default implementation does nothing (suitable for binary formats).
    fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> core::result::Result<(), Self::Error> {
        Ok(())
    }

    /// Serialize a struct field with name and value.
    /// Default implementation just serializes the value.
    fn serialize_field<T>(&mut self, name: &str, value: &T) -> core::result::Result<(), Self::Error>
    where
        T: Serialize,
    {
        value.serialize(name, self)
    }

    /// End serializing a struct.
    /// Default implementation does nothing (suitable for binary formats).
    fn serialize_struct_end(&mut self) -> core::result::Result<(), Self::Error> {
        Ok(())
    }
}

/// A serializer that writes data to a byte buffer in little-endian format.
///
/// This is a concrete implementation of the `Serializer` trait that writes
/// binary data in a compact, little-endian format. This is the default serializer
/// used by the [`crate::to_bytes`] convenience function.
///
/// # Format
///
/// - All integers are written in little-endian byte order
/// - Floating-point numbers use IEEE 754 representation
/// - `bool` is written as a single byte (0 or 1)
/// - `Option<T>`: 1 byte tag (0=None, 1=Some) followed by T if Some
/// - Arrays: Elements serialized sequentially (no length prefix)
/// - Tuples: Elements serialized sequentially
/// - Strings/Vec: u32 length prefix followed by data
///
/// # Examples
///
/// ## Basic Usage
///
/// ```ignore
/// use osal_rs_serde::{ByteSerializer, Serializer, Serialize};
///
/// let mut buffer = [0u8; 16];
/// let mut serializer = ByteSerializer::new(&mut buffer);
///
/// serializer.serialize_u32("", 42).unwrap();
/// serializer.serialize_bool("", true).unwrap();
/// serializer.serialize_i16("", -100).unwrap();
///
/// let len = serializer.position();
/// println!("Serialized {} bytes", len);
/// ```
///
/// ## With Structs
///
/// ```ignore
/// use osal_rs_serde::{ByteSerializer, Serialize};
///
/// #[derive(Serialize)]
/// struct Message {
///     id: u32,
///     value: i16,
/// }
///
/// let msg = Message { id: 100, value: -50 };
/// let mut buffer = [0u8; 32];
/// let mut serializer = ByteSerializer::new(&mut buffer);
/// msg.serialize(&mut serializer).unwrap();
/// ```
///
/// # Memory Layout
///
/// The serializer writes data sequentially without padding or alignment:
///
/// ```text
/// struct Data { a: u16, b: u32 }
/// Memory: [a_lo, a_hi, b0, b1, b2, b3]
/// ```
pub struct ByteSerializer<'a> {
    buffer: &'a mut [u8],
    position: usize,
}

impl<'a> ByteSerializer<'a> {
    /// Create a new ByteSerializer with the given buffer.
    pub fn new(buffer: &'a mut [u8]) -> Self {
        Self {
            buffer,
            position: 0,
        }
    }

    /// Get the current position in the buffer.
    pub fn position(&self) -> usize {
        self.position
    }

    /// Write bytes to the buffer.
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
        if self.position + bytes.len() > self.buffer.len() {
            return Err(Error::BufferTooSmall);
        }
        self.buffer[self.position..self.position + bytes.len()].copy_from_slice(bytes);
        self.position += bytes.len();
        Ok(())
    }
}

impl<'a> Serializer for ByteSerializer<'a> {
    type Error = Error;

    fn serialize_bool(&mut self, _name: &str, v: bool) -> Result<()> {
        self.serialize_u8("", if v { 1 } else { 0 })
    }

    fn serialize_u8(&mut self, _name: &str, v: u8) -> Result<()> {
        self.write_bytes(&[v])
    }

    fn serialize_i8(&mut self, _name: &str, v: i8) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_u16(&mut self, _name: &str, v: u16) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_i16(&mut self, _name: &str, v: i16) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_u32(&mut self, _name: &str, v: u32) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_i32(&mut self, _name: &str, v: i32) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_u64(&mut self, _name: &str, v: u64) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_i64(&mut self, _name: &str, v: i64) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_u128(&mut self, _name: &str, v: u128) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_i128(&mut self, _name: &str, v: i128) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_f32(&mut self, _name: &str, v: f32) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_f64(&mut self, _name: &str, v: f64) -> Result<()> {
        self.write_bytes(&v.to_le_bytes())
    }

    fn serialize_bytes(&mut self, _name: &str, v: &[u8]) -> Result<()> {
        // First write the length as u32
        self.serialize_u32("", v.len() as u32)?;
        self.write_bytes(v)
    }

    fn serialize_string(&mut self, name: &str, v: &String) -> core::result::Result<(), Self::Error> {
        self.serialize_str(name, v.as_str())
    }

    fn serialize_str(&mut self, name: &str, v: &str) -> core::result::Result<(), Self::Error> {
        self.serialize_bytes(name, v.as_bytes())
    }

    fn serialize_vec<T>(&mut self, name: &str, v: &alloc::vec::Vec<T>) -> core::result::Result<(), Self::Error> 
    where
        T: Serialize {
        // First write the length as u32
        self.serialize_u32(name, v.len() as u32)?;
        for item in v.iter() {
            item.serialize(name, self)?;
        }
        Ok(())
    }

    /// Serialize an array of serializable items.
    fn serialize_array<T>(&mut self, name: &str, v: &[T]) -> core::result::Result<(), Self::Error> 
    where
        T: Serialize {
        for item in v.iter() {
            item.serialize(name, self)?;
        }
        Ok(())
    }

}

// Implementations for primitive types

impl Serialize for bool {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer
    {
    
        serializer.serialize_bool(name, *self)
    }
}

impl Serialize for u8 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_u8(name, *self)
    }
}

impl Serialize for i8 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_i8(name, *self)
    }
}

impl Serialize for u16 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_u16(name, *self)
    }
}

impl Serialize for i16 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_i16(name, *self)
    }
}

impl Serialize for u32 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_u32(name, *self)
    }
}

impl Serialize for i32 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_i32(name, *self)
    }
}

impl Serialize for u64 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_u64(name, *self)
    }
}

impl Serialize for i64 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_i64(name, *self)
    }
}

impl Serialize for u128 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_u128(name, *self)
    }
}

impl Serialize for i128 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_i128(name, *self)
    }
}

impl Serialize for f32 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_f32(name, *self)
    }
}

impl Serialize for f64 {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_f64(name, *self)
    }
}

// String implementations
impl Serialize for &str {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_str(name, self)
    }
}

#[cfg(feature = "alloc")]
impl Serialize for String {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        serializer.serialize_string(name, self)
    }
}

// Array implementation
impl<T: Serialize, const N: usize> Serialize for [T; N] {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer
    {
        serializer.serialize_array(name, self)
    }
}

// Tuple implementations
impl<T1: Serialize, T2: Serialize> Serialize for (T1, T2) {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        self.0.serialize(name, serializer)?;
        self.1.serialize(name, serializer)?;
        Ok(())
    }
}

impl<T1: Serialize, T2: Serialize, T3: Serialize> Serialize for (T1, T2, T3) {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer
    {
        self.0.serialize(name, serializer)?;
        self.1.serialize(name, serializer)?;
        self.2.serialize(name, serializer)?;
        Ok(())
    }
}

// Option implementation
impl<T: Serialize> Serialize for Option<T> {
    fn serialize<S>(&self, name: &str, serializer: &mut S) -> core::result::Result<(), S::Error> 
    where
        S: Serializer,
    {
        match self {
            Some(value) => {
                serializer.serialize_u8(name, 1)?;
                value.serialize(name, serializer)?;
            }
            None => {
                serializer.serialize_u8(name, 0)?;
            }
        }
        Ok(())
    }
}