rust-nex/rnex-core/src/rmc/structures/any.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

2026-02-01 20:59:23 +01:00
use rnex_core::rmc::structures::{Result, RmcSerialize};
2026-04-25 14:15:42 +02:00
use std::io::{Cursor, Read, Write};
2025-09-21 15:59:27 +02:00
use v_byte_helpers::{IS_BIG_ENDIAN, ReadExtensions};
2025-01-26 12:09:56 +01:00
2026-04-25 14:15:42 +02:00
#[derive(Debug, Default, Clone)]
2026-02-01 20:59:23 +01:00
pub struct Any {
2025-01-26 12:09:56 +01:00
pub name: String,
2026-02-01 20:59:23 +01:00
pub data: Vec<u8>,
2025-01-26 12:09:56 +01:00
}
2026-02-01 20:59:23 +01:00
impl RmcSerialize for Any {
2025-11-12 22:41:34 +01:00
fn serialize(&self, writer: &mut impl Write) -> Result<()> {
self.name.serialize(writer)?;
2026-04-28 15:07:33 +02:00
let u32_len = self.data.len() as u32;
2026-04-25 14:15:42 +02:00
(u32_len + 4).serialize(writer)?;
2026-04-28 15:07:33 +02:00
self.data.serialize(writer)?;
Ok(())
}
2025-11-13 10:06:58 +01:00
fn deserialize(reader: &mut impl Read) -> Result<Self> {
let name = String::deserialize(reader)?;
2025-01-26 12:09:56 +01:00
2026-04-28 15:07:33 +02:00
// also length ?
let _len2: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
let data = Vec::deserialize(reader)?;
2026-04-28 14:52:19 +02:00
2026-04-28 15:07:33 +02:00
Ok(Any { name, data })
}
2026-02-01 20:59:23 +01:00
}
2026-04-25 14:15:42 +02:00
impl Any {
pub fn try_get<T: RmcSerialize>(&self) -> Option<Result<T>> {
if self.name != T::name() {
return None;
}
return Some(T::deserialize(&mut Cursor::new(&self.data[..])));
}
pub fn new<T: RmcSerialize>(val: &T) -> Result<Self> {
return Ok(Self {
name: T::name().to_owned(),
data: val.to_data()?,
});
}
}