|
| 1 | +use crate::engine::Engine; |
| 2 | + |
| 3 | +#[derive(Debug)] |
| 4 | +pub enum SerializeError { |
| 5 | + #[cfg(not(feature = "flatbuffers"))] |
| 6 | + DataFormatError(crate::data_format::SerializationError), |
| 7 | + |
| 8 | + #[cfg(feature = "flatbuffers")] |
| 9 | + FlatbuffersError(), |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug)] |
| 13 | +pub enum DeserializeError { |
| 14 | + #[cfg(not(feature = "flatbuffers"))] |
| 15 | + DataFormatError(crate::data_format::DeserializationError), |
| 16 | + |
| 17 | + #[cfg(feature = "flatbuffers")] |
| 18 | + FlatbuffersError(), |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(not(feature = "flatbuffers"))] |
| 22 | +impl From<crate::data_format::SerializationError> for SerializeError { |
| 23 | + fn from(value: crate::data_format::SerializationError) -> Self { |
| 24 | + SerializeError::DataFormatError(value) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(not(feature = "flatbuffers"))] |
| 29 | +impl From<crate::data_format::DeserializationError> for DeserializeError { |
| 30 | + fn from(value: crate::data_format::DeserializationError) -> Self { |
| 31 | + DeserializeError::DataFormatError(value) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub trait Serialize { |
| 36 | + fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError>; |
| 37 | + fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError>; |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(feature = "flatbuffers")] |
| 41 | +impl Serialize for Engine { |
| 42 | + fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> { |
| 43 | + Err(SerializeError::FlatbuffersError()) |
| 44 | + } |
| 45 | + |
| 46 | + fn deserialize(&mut self, _serialized: &[u8]) -> Result<(), DeserializeError> { |
| 47 | + Err(DeserializeError::FlatbuffersError()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(not(feature = "flatbuffers"))] |
| 52 | +impl Serialize for Engine { |
| 53 | + /// Serializes the `Engine` into a binary format so that it can be quickly reloaded later. |
| 54 | + fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> { |
| 55 | + use crate::data_format::SerializeFormat; |
| 56 | + |
| 57 | + let serialize_format = SerializeFormat::build(&self.blocker, &self.cosmetic_cache); |
| 58 | + |
| 59 | + let result = serialize_format.serialize()?; |
| 60 | + Ok(result) |
| 61 | + } |
| 62 | + |
| 63 | + /// Deserialize the `Engine` from the binary format generated by `Engine::serialize_raw`. The |
| 64 | + /// method will automatically select the correct deserialization implementation. |
| 65 | + fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError> { |
| 66 | + use crate::data_format::DeserializeFormat; |
| 67 | + let current_tags = self.blocker.tags_enabled(); |
| 68 | + let deserialize_format = DeserializeFormat::deserialize(serialized)?; |
| 69 | + let (blocker, cosmetic_cache) = deserialize_format.build(); |
| 70 | + self.blocker = blocker; |
| 71 | + self.blocker |
| 72 | + .use_tags(¤t_tags.iter().map(|s| &**s).collect::<Vec<_>>()); |
| 73 | + self.cosmetic_cache = cosmetic_cache; |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | +} |
0 commit comments