Skip to content

Commit

Permalink
make much more use of anyhow, replacing most PyResults
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Carow authored and Kyle Carow committed Oct 18, 2023
1 parent 4fc118b commit dccb651
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 290 deletions.
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());
} 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());
} 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

0 comments on commit dccb651

Please sign in to comment.