Skip to content

Commit

Permalink
Added file_utils to print not-found path in error (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw authored Jan 21, 2025
1 parent 82e6c20 commit 29175f2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions stwo_cairo_prover/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions stwo_cairo_prover/crates/prover/src/input/vm_import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cairo_vm::stdlib::collections::HashMap;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry;
use json::PrivateInput;
use stwo_cairo_utils::file_utils::{open_file, read_to_string, IoErrorWithPath};
use thiserror::Error;
use tracing::{span, Level};

Expand All @@ -22,7 +23,7 @@ use crate::input::memory::MemoryBuilder;
#[derive(Debug, Error)]
pub enum VmImportError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
Io(#[from] IoErrorWithPath),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("No memory segments")]
Expand All @@ -37,10 +38,10 @@ pub fn adapt_vm_output(
dev_mode: bool,
) -> Result<ProverInput, VmImportError> {
let _span = span!(Level::INFO, "adapt_vm_output").entered();
let public_input_string = std::fs::read_to_string(public_input_json)?;
let public_input_string = read_to_string(public_input_json)?;
let public_input: PublicInput<'_> = serde_json::from_str(&public_input_string)?;
let private_input: PrivateInput =
serde_json::from_str(&std::fs::read_to_string(private_input_json)?)?;

let private_input: PrivateInput = serde_json::from_reader(&open_file(private_input_json)?)?;

let end_addr = public_input
.memory_segments
Expand All @@ -59,8 +60,8 @@ pub fn adapt_vm_output(
.unwrap()
.join(&private_input.trace_path);

let mut memory_file = std::io::BufReader::new(std::fs::File::open(memory_path)?);
let mut trace_file = std::io::BufReader::new(std::fs::File::open(trace_path)?);
let mut memory_file = std::io::BufReader::new(open_file(memory_path.as_path())?);
let mut trace_file = std::io::BufReader::new(open_file(trace_path.as_path())?);

let public_memory_addresses = public_input
.public_memory
Expand Down
1 change: 1 addition & 0 deletions stwo_cairo_prover/crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ log.workspace = true
thiserror.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde.workspace = true
36 changes: 36 additions & 0 deletions stwo_cairo_prover/crates/utils/src/file_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};

/// An extension error to the io:Error, additionally specifying the not-found path.
#[derive(std::fmt::Debug)]
pub struct IoErrorWithPath {
source: io::Error,
path: PathBuf,
}
impl std::error::Error for IoErrorWithPath {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
impl std::fmt::Display for IoErrorWithPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}: {}", self.source, self.path.display())
}
}

/// A wrapper to `File::open`, which, in case of failure, also logs the not-found path.
pub fn open_file(path: &Path) -> Result<File, IoErrorWithPath> {
File::open(path).map_err(|e| IoErrorWithPath {
source: e,
path: path.to_path_buf(),
})
}

/// A wrapper to `std::fs::read_to_string`, which, in case of failure, also logs the not-found path.
pub fn read_to_string(path: &Path) -> Result<String, IoErrorWithPath> {
std::fs::read_to_string(path).map_err(|e| IoErrorWithPath {
source: e,
path: path.to_path_buf(),
})
}
1 change: 1 addition & 0 deletions stwo_cairo_prover/crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod binary_utils;
pub mod file_utils;
pub mod logging_utils;
pub mod vm_utils;

0 comments on commit 29175f2

Please sign in to comment.