Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize sierra version to int not string #4252

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions crates/cairo-lang-sierra/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use anyhow::Result;
use num_bigint::BigInt;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::debug_info::DebugInfo;
use crate::extensions::gas::{
Expand All @@ -18,30 +19,65 @@ use crate::ids::{
///
/// Always prefer using this struct as saved artifacts instead of inner ones.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "version")]
#[serde(untagged)]
pub enum VersionedProgram {
#[serde(rename = "1")]
V1(ProgramArtifact),
V1 {
version: Version<1>,
#[serde(flatten)]
program: ProgramArtifact,
},
}

impl VersionedProgram {
pub fn v1(program: ProgramArtifact) -> Self {
Self::V1 { program, version: Version::<1> }
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Version<const V: u8>;

#[derive(Debug, Error)]
#[error("Unsupported Sierra program version")]
struct VersionError;

impl<const V: u8> Serialize for Version<V> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u8(V)
}
}

impl<'de, const V: u8> Deserialize<'de> for Version<V> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = u8::deserialize(deserializer)?;
if value == V { Ok(Version::<V>) } else { Err(serde::de::Error::custom(VersionError)) }
}
}

impl From<ProgramArtifact> for VersionedProgram {
fn from(value: ProgramArtifact) -> Self {
VersionedProgram::V1(value)
VersionedProgram::v1(value)
}
}

impl VersionedProgram {
pub fn into_v1(self) -> Result<ProgramArtifact> {
match self {
VersionedProgram::V1(program) => Ok(program),
VersionedProgram::V1 { program, .. } => Ok(program),
}
}
}

impl fmt::Display for VersionedProgram {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VersionedProgram::V1(program) => fmt::Display::fmt(program, f),
VersionedProgram::V1 { program, .. } => fmt::Display::fmt(program, f),
}
}
}
Expand Down Expand Up @@ -96,7 +132,7 @@ impl Program {

/// Create a new [`ProgramArtifact`] out of this [`Program`].
pub fn into_artifact(self) -> VersionedProgram {
VersionedProgram::V1(ProgramArtifact::stripped(self))
ProgramArtifact::stripped(self).into()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-sierra/src/test_data/fib_jumps
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_sierra_serde_json

//! > pretty_json
{
"version": "1",
"version": 1,
"type_declarations": [
{
"id": {
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-sierra/src/test_data/fib_no_gas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_sierra_serde_json

//! > pretty_json
{
"version": "1",
"version": 1,
"type_declarations": [
{
"id": {
Expand Down