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<()> {
|
2025-03-24 23:31:25 +01:00
|
|
|
self.name.serialize(writer)?;
|
|
|
|
|
|
|
|
|
|
let u32_len = self.data.len() as u32;
|
2026-04-25 14:15:42 +02:00
|
|
|
(u32_len + 4).serialize(writer)?;
|
2025-03-24 23:31:25 +01:00
|
|
|
self.data.serialize(writer)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-26 23:21:35 +01:00
|
|
|
}
|
2025-11-13 10:06:58 +01:00
|
|
|
fn deserialize(reader: &mut impl Read) -> Result<Self> {
|
2025-01-26 23:21:35 +01:00
|
|
|
let name = String::deserialize(reader)?;
|
2025-01-26 12:09:56 +01:00
|
|
|
|
2025-01-26 23:21:35 +01:00
|
|
|
// also length ?
|
2025-02-01 17:31:13 +01:00
|
|
|
let _len2: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
|
2026-04-25 20:49:31 +02:00
|
|
|
let data = Vec::deserialize(reader)?;
|
2025-01-26 12:09:56 +01:00
|
|
|
|
2026-02-01 20:59:23 +01:00
|
|
|
Ok(Any { name, data })
|
2025-01-26 23:21:35 +01:00
|
|
|
}
|
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()?,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|