Skip to content

Commit

Permalink
remove unnecessary inits and add one in RustCycle::push
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Carow authored and Kyle Carow committed Jan 5, 2024
1 parent 6e2d09c commit 2f4f433
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub fn main() -> anyhow::Result<()> {
let fastsim_api = FastSimApi::parse();

if let Some(_cyc_json_str) = fastsim_api.cyc {
// TODO: this probably could be filled out...
anyhow::bail!("Need to implement: let cyc = RustCycle::from_json(cyc_json_str)");
}
let (is_adopt_hd, adopt_hd_string, adopt_hd_has_cycle) =
Expand Down
32 changes: 17 additions & 15 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ impl SerdeAPI for RustCycle {
self.road_type.len() == cyc_len,
"Length of `road_type` does not match length of `time_s`"
);
self.initialized = true;
}
Ok(())
}
Expand Down Expand Up @@ -700,17 +701,17 @@ impl SerdeAPI for RustCycle {
/// Note that using this method to instantiate a RustCycle from CSV, rather
/// than the `from_csv_str` method, sets the cycle name to an empty string
fn from_str<S: AsRef<str>>(contents: S, format: &str) -> anyhow::Result<Self> {
let mut deserialized = match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => Self::from_yaml(contents)?,
"json" => Self::from_json(contents)?,
"csv" => Self::from_csv_str(contents, "".to_string())?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_STR_FORMATS
),
};
deserialized.init()?;
Ok(deserialized)
Ok(
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => Self::from_yaml(contents)?,
"json" => Self::from_json(contents)?,
"csv" => Self::from_csv_str(contents, "".to_string())?,
_ => bail!(
"Unsupported format {format:?}, must be one of {:?}",
Self::ACCEPTED_STR_FORMATS
),
},
)
}

fn from_reader<R: std::io::Read>(rdr: R, format: &str) -> anyhow::Result<Self> {
Expand All @@ -723,8 +724,10 @@ impl SerdeAPI for RustCycle {
let mut cyc = Self::default();
let mut rdr = csv::Reader::from_reader(rdr);
for result in rdr.deserialize() {
cyc.push(result?);
cyc.initialized = true; // skip init checks until finished
cyc.push(result?)?;
}
cyc.initialized = false;
cyc
}
_ => {
Expand Down Expand Up @@ -812,15 +815,13 @@ impl RustCycle {
})?;
let mut cyc = Self::from_reader(file, "csv")?;
cyc.name = name;
cyc.init()?;
Ok(cyc)
}

/// Load cycle from CSV string
pub fn from_csv_str<S: AsRef<str>>(csv_str: S, name: String) -> anyhow::Result<Self> {
let mut cyc = Self::from_reader(csv_str.as_ref().as_bytes(), "csv")?;
cyc.name = name;
cyc.init()?;
Ok(cyc)
}

Expand All @@ -842,7 +843,7 @@ impl RustCycle {
RustCycleCache::new(self)
}

pub fn push(&mut self, cyc_elem: RustCycleElement) {
pub fn push(&mut self, cyc_elem: RustCycleElement) -> anyhow::Result<()> {
self.time_s
.append(Axis(0), array![cyc_elem.time_s].view())
.unwrap();
Expand All @@ -857,6 +858,7 @@ impl RustCycle {
.append(Axis(0), array![road_type].view())
.unwrap();
}
self.init()
}

pub fn len(&self) -> usize {
Expand Down
3 changes: 1 addition & 2 deletions rust/fastsim-core/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::proc_macros::{add_pyo3_api, ApproxEq};
#[cfg(feature = "pyo3")]
use crate::pyo3imports::*;

use serde_json::from_str;
use std::collections::HashMap;

/// Unit conversions that should NEVER change
Expand Down Expand Up @@ -163,7 +162,7 @@ impl SerdeAPI for AdjCoef {}
impl Default for RustLongParams {
fn default() -> Self {
let long_params_str: &str = include_str!("../resources/longparams.json");
let long_params: Self = from_str(long_params_str).unwrap();
let long_params = Self::from_json(long_params_str).unwrap();
long_params
}
}
Expand Down
8 changes: 2 additions & 6 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
let file = crate::resources::RESOURCES_DIR
.get_file(filepath)
.with_context(|| format!("File not found in resources: {filepath:?}"))?;
let mut deserialized = Self::from_reader(file.contents(), extension)?;
deserialized.init()?;
Ok(deserialized)
Self::from_reader(file.contents(), extension)
}

/// Write (serialize) an object to a file.
Expand Down Expand Up @@ -76,9 +74,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
format!("Could not open file: {filepath:?}")
}
})?;
let mut deserialized = Self::from_reader(file, extension)?;
deserialized.init()?;
Ok(deserialized)
Self::from_reader(file, extension)
}

/// Write (serialize) an object into a string
Expand Down
1 change: 1 addition & 0 deletions rust/fastsim-core/src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ impl SerdeAPI for RustVehicle {
fn init(&mut self) -> anyhow::Result<()> {
if !self.initialized {
self.set_derived()?;
self.initialized = true;
}
Ok(())
}
Expand Down

0 comments on commit 2f4f433

Please sign in to comment.