Skip to content

Commit

Permalink
streamline serdeapi very slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarow committed Aug 6, 2024
1 parent f363a6c commit 3fd6055
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 12 additions & 7 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,7 @@ impl SerdeAPI for RustCycle {
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::to_writer(wtr, self)?,
"json" => serde_json::to_writer(wtr, self)?,
"toml" => {
let toml_string = self.to_toml()?;
wtr.write_all(toml_string.as_bytes())?;
},
"toml" => wtr.write_all(self.to_toml()?.as_bytes())?,
#[cfg(feature = "bincode")]
"bin" => bincode::serialize_into(wtr, self)?,
"csv" => {
Expand Down Expand Up @@ -709,15 +706,19 @@ impl SerdeAPI for RustCycle {
)
}

fn from_reader<R: std::io::Read>(mut rdr: R, format: &str, skip_init: bool) -> anyhow::Result<Self> {
fn from_reader<R: std::io::Read>(
mut rdr: R,
format: &str,
skip_init: bool,
) -> anyhow::Result<Self> {
let mut deserialized = match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::from_reader(rdr)?,
"json" => serde_json::from_reader(rdr)?,
"toml" => {
let mut buf = String::new();
rdr.read_to_string(&mut buf)?;
Self::from_toml(buf, skip_init)?
},
}
#[cfg(feature = "bincode")]
"bin" => bincode::deserialize_from(rdr)?,
"csv" => {
Expand Down Expand Up @@ -823,7 +824,11 @@ impl RustCycle {
}

/// Load cycle from CSV string
pub fn from_csv_str<S: AsRef<str>>(csv_str: S, name: String, skip_init: bool) -> anyhow::Result<Self> {
pub fn from_csv_str<S: AsRef<str>>(
csv_str: S,
name: String,
skip_init: bool,
) -> anyhow::Result<Self> {
let mut cyc = Self::from_str(csv_str, "csv", skip_init)?;
cyc.name = name;
Ok(cyc)
Expand Down
13 changes: 7 additions & 6 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::to_writer(wtr, self)?,
"json" => serde_json::to_writer(wtr, self)?,
"toml" => {
let toml_string = self.to_toml()?;
wtr.write_all(toml_string.as_bytes())?;
},
"toml" => wtr.write_all(self.to_toml()?.as_bytes())?,
#[cfg(feature = "bincode")]
"bin" => bincode::serialize_into(wtr, self)?,
_ => bail!(
Expand Down Expand Up @@ -135,15 +132,19 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
/// * `rdr` - The reader from which to read object data
/// * `format` - The source format, any of those listed in [`ACCEPTED_BYTE_FORMATS`](`SerdeAPI::ACCEPTED_BYTE_FORMATS`)
///
fn from_reader<R: std::io::Read>(mut rdr: R, format: &str, skip_init: bool) -> anyhow::Result<Self> {
fn from_reader<R: std::io::Read>(
mut rdr: R,
format: &str,
skip_init: bool,
) -> anyhow::Result<Self> {
let mut deserialized: Self = match format.trim_start_matches('.').to_lowercase().as_str() {
"yaml" | "yml" => serde_yaml::from_reader(rdr)?,
"json" => serde_json::from_reader(rdr)?,
"toml" => {
let mut buf = String::new();
rdr.read_to_string(&mut buf)?;
Self::from_toml(buf, skip_init)?
},
}
#[cfg(feature = "bincode")]
"bin" => bincode::deserialize_from(rdr)?,
_ => bail!(
Expand Down

0 comments on commit 3fd6055

Please sign in to comment.