1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use crate::{ darksiders1::gfc, utils::{parsing::derailed, serde::repr}, }; use serde::{de::Error, Deserialize, Deserializer}; use std::{ collections::{hash_map::Entry, HashMap}, convert::TryInto, sync::Arc, }; impl<'de> Deserialize<'de> for gfc::Object { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { let value = De::default() .value(repr::Value::deserialize(deserializer)?) .map_err(|_| Error::custom(derailed()))? .into_object() .map_err(|_| Error::custom(derailed()))?; Ok(Arc::try_unwrap(value).unwrap()) } } #[derive(Default)] struct De { object_database: HashMap<i32, Arc<gfc::Object>>, } impl De { fn value(&mut self, repr: repr::Value) -> Result<gfc::Value, ()> { Ok(match repr { repr::Value::Int { value } => gfc::Value::Int(value), repr::Value::Float { value } => gfc::Value::Float(value), repr::Value::Bool { value } => gfc::Value::Bool(value), repr::Value::String { value } => gfc::Value::String(value), repr::Value::Object(object) => self.object(object)?, repr::Value::Array { items } => { let items = items.into_iter().map(|x| self.value(x)).collect::<Result< Vec<_>, _, >>( )?; gfc::Value::Array(items) } repr::Value::Map { entries } => { let entries = entries .into_iter() .map(|(key, value)| Ok((self.value(key)?, self.value(value)?))) .collect::<Result<Vec<_>, _>>()?; gfc::Value::Map(entries) } repr::Value::Struct { elements } => { let elements = elements .into_iter() .map(|x| self.value(x)) .collect::<Result<Vec<_>, _>>()?; gfc::Value::Struct(elements) } repr::Value::HString { value } => gfc::Value::HString(value), repr::Value::Null => gfc::Value::Null, repr::Value::ObjectLink { id } => { let object = self.object_database.get(&id).ok_or(())?; gfc::Value::Object(object.clone()) } }) } fn object(&mut self, repr: repr::Object) -> Result<gfc::Value, ()> { let properties = match repr.properties { repr::ObjectProperties::List(entries) => { entries .into_iter() .map(|(name, value)| Ok((name, self.value(value)?))) .collect::<Result<Vec<_>, _>>()? } repr::ObjectProperties::Map(entries) => { entries .into_iter() .map(|(name, value)| Ok((name, self.value(value)?))) .collect::<Result<Vec<_>, _>>()? } }; let object = Arc::new(gfc::Object { classname: repr.classname, properties, }); if let Some(id) = repr.id { match self.object_database.entry(id.try_into().map_err(|_| ())?) { Entry::Vacant(entry) => { entry.insert(object.clone()); } Entry::Occupied(_) => { return Err(()); } } } Ok(gfc::Value::Object(object)) } }