use osal_rs_serde::{Serialize, Deserialize, to_bytes, from_bytes};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Config {
device_id: u32,
name: Option<u8>, enabled: bool,
timeout: Option<u16>, }
fn main() {
println!("=== OSAL-RS-Serde Optional Fields Example ===\n");
let config_full = Config {
device_id: 100,
name: Some(42),
enabled: true,
timeout: Some(5000),
};
println!("Config (all fields): {:?}", config_full);
let mut buffer = [0u8; 64];
let len = to_bytes(&config_full, &mut buffer).unwrap();
println!("Serialized {} bytes", len);
let decoded: Config = from_bytes(&buffer[..len]).unwrap();
println!("Decoded: {:?}", decoded);
assert_eq!(config_full.device_id, decoded.device_id);
assert_eq!(config_full.name, decoded.name);
assert_eq!(config_full.timeout, decoded.timeout);
println!("\n=== Config with None values ===");
let config_partial = Config {
device_id: 200,
name: None,
enabled: false,
timeout: Some(1000),
};
println!("Config (partial): {:?}", config_partial);
let len = to_bytes(&config_partial, &mut buffer).unwrap();
println!("Serialized {} bytes", len);
let decoded: Config = from_bytes(&buffer[..len]).unwrap();
println!("Decoded: {:?}", decoded);
assert_eq!(config_partial.device_id, decoded.device_id);
assert_eq!(config_partial.name, decoded.name);
assert_eq!(config_partial.timeout, decoded.timeout);
println!("\n=== Config with all optional None ===");
let config_minimal = Config {
device_id: 300,
name: None,
enabled: true,
timeout: None,
};
println!("Config (minimal): {:?}", config_minimal);
let len = to_bytes(&config_minimal, &mut buffer).unwrap();
println!("Serialized {} bytes (notice smaller size due to None values)", len);
let decoded: Config = from_bytes(&buffer[..len]).unwrap();
println!("Decoded: {:?}", decoded);
assert_eq!(config_minimal, decoded);
println!("\n=== Example completed successfully! ===");
}