use std::any::Any;
use feagi_data_structures::FeagiJSON;
use feagi_data_structures::FeagiDataError;
use crate::{FeagiByteContainer, FeagiByteStructureType, FeagiSerializable};
const BYTE_STRUCT_VERSION: u8 = 1;
impl FeagiSerializable for FeagiJSON {
fn get_type(&self) -> FeagiByteStructureType {
FeagiByteStructureType::JSON
}
fn get_version(&self) -> u8 {
BYTE_STRUCT_VERSION
}
fn get_number_of_bytes_needed(&self) -> usize {
self.borrow_json_value().to_string().as_bytes().len() + FeagiByteContainer::STRUCT_HEADER_BYTE_COUNT
}
fn try_serialize_struct_to_byte_slice(&self, byte_destination: &mut [u8]) -> Result<(), FeagiDataError> {
byte_destination[0] = self.get_type() as u8;
byte_destination[1] = self.get_version();
let json_string = self.borrow_json_value().to_string();
let json_bytes = json_string.as_bytes();
byte_destination[FeagiByteContainer::STRUCT_HEADER_BYTE_COUNT..].copy_from_slice(json_bytes);
Ok(())
}
fn try_deserialize_and_update_self_from_byte_slice(&mut self, byte_structure_slice: &[u8]) -> Result<(), FeagiDataError> {
self.verify_byte_slice_is_of_correct_version(byte_structure_slice)?;
let json_bytes = &byte_structure_slice[FeagiByteContainer::STRUCT_HEADER_BYTE_COUNT..];
let json_value = match serde_json::from_slice(json_bytes) {
Ok(value) => value,
Err(e) => return Err(FeagiDataError::DeserializationError(format!("Invalid JSON data: {}", e))),
};
self.update_json_value(json_value);
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
}