Skip to content

Commit

Permalink
Merge pull request #47 from tdelabro/less-unwrap-more-errors
Browse files Browse the repository at this point in the history
feat: remove a buch of unwrap
  • Loading branch information
0xLucqs authored Oct 27, 2023
2 parents b7afd0f + 8d11f34 commit 97464aa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum SnOsError {
Runner(CairoRunError),
#[error("SnOs Output Error: {0}")]
Output(String),
#[error(transparent)]
IO(#[from] std::io::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
}

#[derive(thiserror::Error, Clone, Debug)]
Expand Down
7 changes: 5 additions & 2 deletions src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ pub fn starknet_os_input(
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let input_path = exec_scopes.get::<String>("input_path").unwrap_or(DEFAULT_INPUT_PATH.to_string());
let input_path =
std::path::PathBuf::from(exec_scopes.get::<String>("input_path").unwrap_or(DEFAULT_INPUT_PATH.to_string()));

let os_input = Box::new(StarknetOsInput::load(&input_path));
let os_input = Box::new(
StarknetOsInput::load(&input_path).map_err(|e| HintError::CustomHint(e.to_string().into_boxed_str()))?,
);
exec_scopes.assign_or_update_variable("os_input", os_input);

let initial_carried_outputs_ptr = get_ptr_from_var_name("initial_carried_outputs", vm, ids_data, ap_tracking)?;
Expand Down
18 changes: 10 additions & 8 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ pub struct StarknetOsInput {
}

impl StarknetOsInput {
pub fn load(path: &str) -> Self {
let raw_input = fs::read_to_string(path::PathBuf::from(path)).unwrap();
serde_json::from_str(&raw_input).unwrap()
pub fn load(path: &path::Path) -> Result<Self, SnOsError> {
let raw_input = fs::read_to_string(path)?;
let input = serde_json::from_str(&raw_input)?;

Ok(input)
}
pub fn dump(&self, path: &str) -> Result<(), SnOsError> {
fs::File::create(path)
.unwrap()
.write_all(&serde_json::to_vec(&self).unwrap())
.map_err(|e| SnOsError::CatchAll(format!("{e}")))

pub fn dump(&self, path: &path::Path) -> Result<(), SnOsError> {
fs::File::create(path)?.write_all(&serde_json::to_vec(&self)?)?;

Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn load_and_write_input() {
#[fixture]
#[once]
pub fn load_input(_load_and_write_input: ()) -> StarknetOsInput {
StarknetOsInput::load(DEFAULT_INPUT_PATH)
StarknetOsInput::load(std::path::Path::new(DEFAULT_INPUT_PATH)).unwrap()
}

#[fixture]
Expand Down

0 comments on commit 97464aa

Please sign in to comment.