Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serde revamp and anyhowization #58

Merged
merged 24 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dccb651
make much more use of anyhow, replacing most PyResults
Oct 18, 2023
fd113fa
remove extraneous comment
Oct 18, 2023
b25e09d
resolve fastsim-2 merge conflicts
Oct 18, 2023
021d72e
anyhowize fastsim-cli
Oct 18, 2023
d9aa2d8
change classmethods to staticmethods, no longer need PyType
Oct 19, 2023
1f2368a
anyhowize simdrivelabel and cyc_mods
Oct 19, 2023
ec6ab43
minor reorg of cycle.rs methods
Oct 19, 2023
0ab74d6
improve fastsim-cli
Oct 19, 2023
dec9471
revamp serde trait and anyhowize some misc functions
Oct 20, 2023
d0a15ae
clean up serde api to_file
Oct 20, 2023
6857a03
remove results in tests
Nov 3, 2023
22673ed
merge fastsim-2 into anyhowize-everything
Nov 3, 2023
e30636a
clean up some more anyhow calls
Nov 3, 2023
b6976f5
remove from_json_str method
Nov 3, 2023
7665bb9
from_str now calls from_yaml or from_json
Nov 3, 2023
428abee
shut annoying import warning up (it IS actually used)
Nov 3, 2023
8dd2f8d
more anyhow and result cleanup, removed some unnecessary result returns
Nov 3, 2023
9e5ec49
refactor code that clippy didn't like
Nov 3, 2023
5414653
expose from_resource to Python API
Nov 3, 2023
12d3a56
make solve_step return a Result in thermal.rs instead of unwrapping
Nov 3, 2023
2d2bc17
move to/from_file functions to other block
Nov 16, 2023
b8bdfa2
cargo fmt, to_str and from_str exposed to Python, better error messag…
Nov 17, 2023
e39c5f0
new to_file method for CSVs! also to_str works for CSVs now
Nov 17, 2023
4a73581
bin file serde working, but not for vehicles for some reason
Nov 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub fn main() {
traceMissInMph: sdl.0.trace_miss_speed_mph,
h2AndDiesel: None,
};
println!("{}", res.to_json());
println!("{}", res.to_json().unwrap());
calbaker marked this conversation as resolved.
Show resolved Hide resolved
} else if is_adopt_hd {
let hd_cyc_filestring = include_str!(concat!(
"..",
Expand Down Expand Up @@ -358,7 +358,7 @@ pub fn main() {
traceMissInMph: sim_drive.trace_miss_speed_mps * MPH_PER_MPS,
h2AndDiesel: h2_diesel_results,
};
println!("{}", res.to_json());
println!("{}", res.to_json().unwrap());
calbaker marked this conversation as resolved.
Show resolved Hide resolved
} else {
let mut sim_drive = RustSimDrive::new(cyc, veh);
// // this does nothing if it has already been called for the constructed `sim_drive`
Expand Down Expand Up @@ -520,7 +520,7 @@ fn json_rewrite(x: String) -> (String, Option<Vec<f64>>, Option<Vec<f64>>) {

parsed_data["stop_start"] = json!(false);

let adoptstring = ParsedValue(parsed_data).to_json();
let adoptstring = ParsedValue(parsed_data).to_json().unwrap();

(adoptstring, fc_pwr_out_perc, hd_h2_diesel_ice_h2share)
}
59 changes: 29 additions & 30 deletions rust/fastsim-core/fastsim-proc-macros/src/add_pyo3_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
if !is_state_or_history {
py_impl_block.extend::<TokenStream2>(quote! {
#[pyo3(name = "to_file")]
pub fn to_file_py(&self, filename: &str) -> PyResult<()> {
Ok(self.to_file(filename)?)
pub fn to_file_py(&self, filepath: &str) -> anyhow::Result<()> {
self.to_file(filepath)
}

#[classmethod]
#[pyo3(name = "from_file")]
pub fn from_file_py(_cls: &PyType, json_str:String) -> PyResult<Self> {
// unwrap is ok here because it makes sense to stop execution if a file is not loadable
Ok(Self::from_file(&json_str)?)
pub fn from_file_py(_cls: &PyType, filepath: &str) -> anyhow::Result<Self> {
Self::from_file(filepath)
}
});
}
Expand Down Expand Up @@ -168,27 +167,27 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn __str__(&self) -> String {
format!("{:?}", self.0)
}
pub fn __getitem__(&self, idx: i32) -> PyResult<#contained_dtype> {
pub fn __getitem__(&self, idx: i32) -> anyhow::Result<#contained_dtype> {
if idx >= self.0.len() as i32 {
Err(PyIndexError::new_err("Index is out of bounds"))
anyhow::bail!(PyIndexError::new_err("Index is out of bounds"))
} else if idx >= 0 {
Ok(self.0[idx as usize].clone())
} else {
Ok(self.0[self.0.len() + idx as usize].clone())
}
}
pub fn __setitem__(&mut self, _idx: usize, _new_value: #contained_dtype
) -> PyResult<()> {
Err(PyNotImplementedError::new_err(
) -> anyhow::Result<()> {
anyhow::bail!(PyNotImplementedError::new_err(
"Setting value at index is not implemented.
Run `tolist` method, modify value at index, and
then set entire vector.",
))
}
pub fn tolist(&self) -> PyResult<Vec<#contained_dtype>> {
pub fn tolist(&self) -> anyhow::Result<Vec<#contained_dtype>> {
Ok(#tolist_body)
}
pub fn __list__(&self) -> PyResult<Vec<#contained_dtype>> {
pub fn __list__(&self) -> anyhow::Result<Vec<#contained_dtype>> {
Ok(#tolist_body)
}
pub fn __len__(&self) -> usize {
Expand All @@ -214,7 +213,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
// py_impl_block.extend::<TokenStream2>(quote! {
// #[classmethod]
// #[pyo3(name = "default")]
// pub fn default_py(_cls: &PyType) -> PyResult<Self> {
// pub fn default_py(_cls: &PyType) -> anyhow::Result<Self> {
// Ok(Self::default())
// }
// });
Expand All @@ -224,43 +223,43 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn __copy__(&self) -> Self {self.clone()}
pub fn __deepcopy__(&self, _memo: &PyDict) -> Self {self.clone()}

/// json serialization method.
/// JSON serialization method.
#[pyo3(name = "to_json")]
pub fn to_json_py(&self) -> PyResult<String> {
Ok(self.to_json())
pub fn to_json_py(&self) -> anyhow::Result<String> {
self.to_json()
}

#[classmethod]
/// json deserialization method.
/// JSON deserialization method.
#[pyo3(name = "from_json")]
pub fn from_json_py(_cls: &PyType, json_str: &str) -> PyResult<Self> {
Ok(Self::from_json(json_str)?)
pub fn from_json_py(_cls: &PyType, json_str: &str) -> anyhow::Result<Self> {
Self::from_json(json_str)
}

/// yaml serialization method.
/// YAML serialization method.
#[pyo3(name = "to_yaml")]
pub fn to_yaml_py(&self) -> PyResult<String> {
Ok(self.to_yaml())
pub fn to_yaml_py(&self) -> anyhow::Result<String> {
self.to_yaml()
}

#[classmethod]
/// yaml deserialization method.
/// YAML deserialization method.
#[pyo3(name = "from_yaml")]
pub fn from_yaml_py(_cls: &PyType, yaml_str: &str) -> PyResult<Self> {
Ok(Self::from_yaml(yaml_str)?)
pub fn from_yaml_py(_cls: &PyType, yaml_str: &str) -> anyhow::Result<Self> {
Self::from_yaml(yaml_str)
}

/// bincode serialization method.
#[pyo3(name = "to_bincode")]
pub fn to_bincode_py<'py>(&self, py: Python<'py>) -> PyResult<&'py PyBytes> {
Ok(PyBytes::new(py, &self.to_bincode()))
pub fn to_bincode_py<'py>(&self, py: Python<'py>) -> anyhow::Result<&'py PyBytes> {
Ok(PyBytes::new(py, &self.to_bincode()?))
}

#[classmethod]
/// bincode deserialization method.
#[pyo3(name = "from_bincode")]
pub fn from_bincode_py(_cls: &PyType, encoded: &PyBytes) -> PyResult<Self> {
Ok(Self::from_bincode(encoded.as_bytes())?)
pub fn from_bincode_py(_cls: &PyType, encoded: &PyBytes) -> anyhow::Result<Self> {
Self::from_bincode(encoded.as_bytes())
}

});
Expand Down Expand Up @@ -331,11 +330,11 @@ pub fn impl_getters_and_setters(
"orphaned" => {
impl_block.extend::<TokenStream2>(quote! {
#[getter]
pub fn get_orphaned(&self) -> PyResult<bool> {
pub fn get_orphaned(&self) -> anyhow::Result<bool> {
Ok(self.orphaned)
}
/// Reset the orphaned flag to false.
pub fn reset_orphaned(&mut self) -> PyResult<()> {
pub fn reset_orphaned(&mut self) -> anyhow::Result<()> {
self.orphaned = false;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! impl_vec_get_set {
let get_name: TokenStream2 = format!("get_{}", $fident).parse().unwrap();
$impl_block.extend::<TokenStream2>(quote! {
#[getter]
pub fn #get_name(&self) -> PyResult<$wrapper_type> {
pub fn #get_name(&self) -> anyhow::Result<$wrapper_type> {
Ok($wrapper_type::new(self.#$fident.clone()))
}
});
Expand All @@ -16,19 +16,19 @@ macro_rules! impl_vec_get_set {
if $has_orphaned {
$impl_block.extend(quote! {
#[setter]
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> {
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> anyhow::Result<()> {
if !self.orphaned {
self.#$fident = new_value;
Ok(())
} else {
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
anyhow::bail!(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
}
}
})
} else {
$impl_block.extend(quote! {
#[setter]
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> {
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> anyhow::Result<()> {
self.#$fident = new_value;
Ok(())
}
Expand All @@ -39,19 +39,19 @@ macro_rules! impl_vec_get_set {
if $has_orphaned {
$impl_block.extend(quote! {
#[setter]
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> {
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> anyhow::Result<()> {
if !self.orphaned {
self.#$fident = Array1::from_vec(new_value);
Ok(())
} else {
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
anyhow::bail!(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
}
}
})
} else {
$impl_block.extend(quote! {
#[setter]
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> PyResult<()> {
pub fn #set_name(&mut self, new_value: Vec<$contained_type>) -> anyhow::Result<()> {
self.#$fident = Array1::from_vec(new_value);
Ok(())
}
Expand Down Expand Up @@ -80,15 +80,15 @@ macro_rules! impl_get_body {
let get_block = if $opts.field_has_orphaned {
quote! {
#[getter]
pub fn #get_name(&mut self) -> PyResult<#$type> {
pub fn #get_name(&mut self) -> anyhow::Result<#$type> {
self.#$field.orphaned = true;
Ok(self.#$field.clone())
}
}
} else {
quote! {
#[getter]
pub fn #get_name(&self) -> PyResult<#$type> {
pub fn #get_name(&self) -> anyhow::Result<#$type> {
Ok(self.#$field.clone())
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ macro_rules! impl_set_body {
self.#$field.orphaned = false;
Ok(())
} else {
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
anyhow::bail!(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
}
}
} else if $has_orphaned {
Expand All @@ -129,7 +129,7 @@ macro_rules! impl_set_body {
self.#$field = new_value;
Ok(())
} else {
Err(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
anyhow::bail!(PyAttributeError::new_err(crate::utils::NESTED_STRUCT_ERR))
}
}
} else {
Expand All @@ -141,7 +141,7 @@ macro_rules! impl_set_body {

$impl_block.extend::<TokenStream2>(quote! {
#[setter]
pub fn #set_name(&mut self, new_value: #$type) -> PyResult<()> {
pub fn #set_name(&mut self, new_value: #$type) -> anyhow::Result<()> {
#orphaned_set_block
}
});
Expand Down
Loading