From 70e9f00e7be7fae0bfe74a6e3b5fc7065546a628 Mon Sep 17 00:00:00 2001 From: Chad Baker Date: Tue, 13 Jun 2023 09:38:25 -0600 Subject: [PATCH] copied better `SerdeAPI` from altrios --- rust/fastsim-core/src/traits.rs | 59 +++++++++++++++++++++----------- rust/fastsim-core/src/vehicle.rs | 5 +-- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/rust/fastsim-core/src/traits.rs b/rust/fastsim-core/src/traits.rs index 69eaeac9..0507a66b 100644 --- a/rust/fastsim-core/src/traits.rs +++ b/rust/fastsim-core/src/traits.rs @@ -3,9 +3,23 @@ use std::collections::HashMap; pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { /// runs any initialization steps that might be needed - fn init(&mut self) {} + fn init(&mut self) -> anyhow::Result<()> { + Ok(()) + } - fn to_file(&self, filename: &str) -> anyhow::Result<()> { + #[allow(clippy::wrong_self_convention)] + /// Save current data structure to file. Method adaptively calls serialization methods + /// dependent on the suffix of the file given as str. + /// + /// # Argument: + /// + /// * `filename`: a `str` storing the targeted file name. Currently `.json` and `.yaml` suffixes are + /// supported + /// + /// # Returns: + /// + /// A Rust Result + fn to_file(&self, filename: &str) -> Result<(), anyhow::Error> { let file = PathBuf::from(filename); match file.extension().unwrap().to_str().unwrap() { "json" => serde_json::to_writer(&File::create(file)?, self)?, @@ -15,6 +29,19 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { Ok(()) } + /// Read from file and return instantiated struct. Method adaptively calls deserialization + /// methods dependent on the suffix of the file name given as str. + /// Function returns a dynamic Error Result if it fails. + /// + /// # Argument: + /// + /// * `filename`: a `str` storing the targeted file name. Currently `.json` and `.yaml` suffixes are + /// supported + /// + /// # Returns: + /// + /// A Rust Result wrapping data structure if method is called successfully; otherwise a dynamic + /// Error. fn from_file(filename: &str) -> Result where Self: std::marker::Sized, @@ -26,13 +53,14 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { .unwrap_or(""); let file = File::open(filename)?; - let mut res = match extension { - "yaml" => Ok(serde_yaml::from_reader::(file)?), - "json" => Ok(serde_json::from_reader::(file)?), - _ => Err(anyhow!("Unsupported file extension {}", extension)), + // deserialized file + let mut file_de: Self = match extension { + "yaml" => serde_yaml::from_reader(file)?, + "json" => serde_json::from_reader(file)?, + _ => bail!("Unsupported file extension {}", extension), }; - res.as_mut().unwrap().init(); - res + file_de.init()?; + Ok(file_de) } /// json serialization method. @@ -41,10 +69,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { } /// json deserialization method. - fn from_json(json_str: &str) -> Result - where - Self: Sized, - { + fn from_json(json_str: &str) -> Result { Ok(serde_json::from_str(json_str)?) } @@ -54,10 +79,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { } /// yaml deserialization method. - fn from_yaml(yaml_str: &str) -> Result - where - Self: Sized, - { + fn from_yaml(yaml_str: &str) -> Result { Ok(serde_yaml::from_str(yaml_str)?) } @@ -67,10 +89,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { } /// bincode deserialization method. - fn from_bincode(encoded: &[u8]) -> Result - where - Self: Sized, - { + fn from_bincode(encoded: &[u8]) -> Result { Ok(deserialize(encoded)?) } } diff --git a/rust/fastsim-core/src/vehicle.rs b/rust/fastsim-core/src/vehicle.rs index 1c350187..d0d6349e 100644 --- a/rust/fastsim-core/src/vehicle.rs +++ b/rust/fastsim-core/src/vehicle.rs @@ -916,8 +916,9 @@ impl RustVehicle { } impl SerdeAPI for RustVehicle { - fn init(&mut self) { - self.set_derived().unwrap(); + fn init(&mut self) -> anyhow::Result<()> { + self.set_derived()?; + Ok(()) } }