hitbox_backend/format/
ron.rs1use bytes::Bytes;
2use hitbox_core::{BoxContext, Raw};
3
4use super::{Format, FormatDeserializer, FormatError, FormatSerializer, FormatTypeId};
5use crate::context::Context;
6
7#[derive(Debug, Clone, Copy, Default)]
12pub struct RonFormat;
13
14impl Format for RonFormat {
15 fn with_serializer(
16 &self,
17 f: &mut dyn FnMut(&mut FormatSerializer) -> Result<(), FormatError>,
18 _context: &dyn Context,
19 ) -> Result<Raw, FormatError> {
20 let mut buf = String::new();
22 {
23 let mut ser = ron::ser::Serializer::new(&mut buf, None)
24 .map_err(|error| FormatError::Serialize(Box::new(error)))?;
25 let mut erased = <dyn erased_serde::Serializer>::erase(&mut ser);
26 let mut format_ser = FormatSerializer::Serde(&mut erased);
27 f(&mut format_ser)?;
28 }
29 Ok(Bytes::from(buf.into_bytes()))
30 }
31
32 fn with_deserializer(
33 &self,
34 data: &[u8],
35 f: &mut dyn FnMut(&mut FormatDeserializer) -> Result<(), FormatError>,
36 _ctx: &mut BoxContext,
37 ) -> Result<(), FormatError> {
38 let s = std::str::from_utf8(data).map_err(|e| FormatError::Deserialize(Box::new(e)))?;
39 let mut deserializer = ron::de::Deserializer::from_str(s)
40 .map_err(|e| FormatError::Deserialize(Box::new(e)))?;
41 let mut erased = <dyn erased_serde::Deserializer>::erase(&mut deserializer);
42 let mut format_deserializer = FormatDeserializer::Serde(&mut erased);
43 f(&mut format_deserializer)?;
44 Ok(())
45 }
46
47 fn clone_box(&self) -> Box<dyn Format> {
48 Box::new(*self)
49 }
50
51 fn format_type_id(&self) -> FormatTypeId {
52 FormatTypeId::Ron
53 }
54}