From fb734b230b210016247cf78994716af043ba6851 Mon Sep 17 00:00:00 2001 From: maciektr Date: Thu, 12 Oct 2023 12:26:36 +0200 Subject: [PATCH] Serialize sierra version to int not string --- crates/cairo-lang-sierra/src/program.rs | 50 ++++++++++++++++--- .../cairo-lang-sierra/src/test_data/fib_jumps | 2 +- .../src/test_data/fib_no_gas | 2 +- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/crates/cairo-lang-sierra/src/program.rs b/crates/cairo-lang-sierra/src/program.rs index 2fe768db4bf..f2626ededad 100644 --- a/crates/cairo-lang-sierra/src/program.rs +++ b/crates/cairo-lang-sierra/src/program.rs @@ -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::{ @@ -18,22 +19,57 @@ 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; + +#[derive(Debug, Error)] +#[error("Unsupported Sierra program version")] +struct VersionError; + +impl Serialize for Version { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_u8(V) + } +} + +impl<'de, const V: u8> Deserialize<'de> for Version { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let value = u8::deserialize(deserializer)?; + if value == V { Ok(Version::) } else { Err(serde::de::Error::custom(VersionError)) } + } } impl From for VersionedProgram { fn from(value: ProgramArtifact) -> Self { - VersionedProgram::V1(value) + VersionedProgram::v1(value) } } impl VersionedProgram { pub fn into_v1(self) -> Result { match self { - VersionedProgram::V1(program) => Ok(program), + VersionedProgram::V1 { program, .. } => Ok(program), } } } @@ -41,7 +77,7 @@ impl VersionedProgram { 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), } } } @@ -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() } } diff --git a/crates/cairo-lang-sierra/src/test_data/fib_jumps b/crates/cairo-lang-sierra/src/test_data/fib_jumps index c65f2971095..0dd833fbecf 100644 --- a/crates/cairo-lang-sierra/src/test_data/fib_jumps +++ b/crates/cairo-lang-sierra/src/test_data/fib_jumps @@ -5,7 +5,7 @@ test_sierra_serde_json //! > pretty_json { - "version": "1", + "version": 1, "type_declarations": [ { "id": { diff --git a/crates/cairo-lang-sierra/src/test_data/fib_no_gas b/crates/cairo-lang-sierra/src/test_data/fib_no_gas index 29e3b0c834a..fe3047931e4 100644 --- a/crates/cairo-lang-sierra/src/test_data/fib_no_gas +++ b/crates/cairo-lang-sierra/src/test_data/fib_no_gas @@ -5,7 +5,7 @@ test_sierra_serde_json //! > pretty_json { - "version": "1", + "version": 1, "type_declarations": [ { "id": {