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 Jan 23, 2024
1 parent 9a82194 commit 461e1ba
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl CairoPie {

pub fn from_zip_archive<R: Read + Seek>(
mut zip: zip::ZipArchive<R>,
) -> Result<CairoPie, CairoPieError> {
) -> Result<Self, CairoPieError> {
let metadata: CairoPieMetadata = Self::parse_zip_file(zip.by_name("metadata.json")?)?;
let execution_resources: ExecutionResources =
Self::parse_zip_file(zip.by_name("execution_resources.json")?)?;
Expand All @@ -189,7 +189,7 @@ impl CairoPie {
};
let memory = Self::read_memory_file(zip.by_name("memory.bin")?, addr_size, felt_bytes)?;

Ok(CairoPie {
Ok(Self {
metadata,
memory,
execution_resources,
Expand All @@ -198,13 +198,22 @@ impl CairoPie {
})
}

/// Builds a CairoPie object from an array of bytes.
#[cfg(feature = "std")]
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)
}

/// Builds a CairoPie object from a ZIP archive.
#[cfg(feature = "std")]
pub fn from_file(path: &Path) -> Result<CairoPie, CairoPieError> {
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 461e1ba

Please sign in to comment.