Skip to content

Commit

Permalink
Feature: deserialize Cairo PIE from bytes
Browse files Browse the repository at this point in the history
Added a `from_bytes` class method to build a `CairoPie` method in
addition to the existing `from_file` method.
  • Loading branch information
odesenfans committed Feb 22, 2024
1 parent d3a33c2 commit e0a4653
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl CairoPie {
#[cfg(feature = "std")]
pub fn from_zip_archive<R: Read + Seek>(
mut zip: zip::ZipArchive<R>,
) -> Result<CairoPie, CairoPieError> {
) -> Result<Self, CairoPieError> {
let metadata: CairoPieMetadata = parse_zip_file(zip.by_name("metadata.json")?)?;
let execution_resources: ExecutionResources =
parse_zip_file(zip.by_name("execution_resources.json")?)?;
Expand All @@ -242,7 +242,7 @@ impl CairoPie {
};
let memory = read_memory_file(zip.by_name("memory.bin")?, addr_size, felt_bytes)?;

Ok(CairoPie {
Ok(Self {
metadata,
memory,
execution_resources,
Expand All @@ -252,11 +252,19 @@ impl CairoPie {
}

#[cfg(feature = "std")]
pub fn from_file(path: &Path) -> Result<CairoPie, CairoPieError> {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, CairoPieError> {
let reader = std::io::Cursor::new(bytes);
let zip_archive = zip::ZipArchive::new(reader)?;

Self::from_zip_archive(zip_archive)
}

#[cfg(feature = "std")]
pub fn from_file(path: &Path) -> Result<Self, CairoPieError> {
let file = std::fs::File::open(path)?;
let zip = zip::ZipArchive::new(file)?;

CairoPie::from_zip_archive(zip)
Self::from_zip_archive(zip)
}
}

Expand Down

0 comments on commit e0a4653

Please sign in to comment.