From 9bef8611e8a3ed74dc5cd291bb3d0e257eec6c01 Mon Sep 17 00:00:00 2001 From: damirka Date: Thu, 2 Sep 2021 18:27:20 +0300 Subject: [PATCH] custom serializer for Type::Array --- ast/src/types/type_.rs | 24 +++++++++++++++++-- .../array_without_size/definition.leo.out | 8 +++---- .../array_without_size/function_input.leo.out | 8 +++---- .../array_without_size/type_alias.leo.out | 8 +++---- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ast/src/types/type_.rs b/ast/src/types/type_.rs index 1b060514f6..6120fe8315 100644 --- a/ast/src/types/type_.rs +++ b/ast/src/types/type_.rs @@ -14,12 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ArrayDimensions, Identifier, IntegerType}; +use crate::{ArrayDimensions, Identifier, IntegerType, PositiveNumber}; use leo_input::types::{ ArrayType as InputArrayType, DataType as InputDataType, TupleType as InputTupleType, Type as InputType, }; -use serde::{Deserialize, Serialize}; +use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer}; use std::fmt; /// Explicit type used for defining a variable or expression type @@ -34,6 +34,7 @@ pub enum Type { IntegerType(IntegerType), // Data type wrappers + #[serde(serialize_with = "serialize_array")] Array(Box, Option), Tuple(Vec), CircuitOrAlias(Identifier), @@ -197,3 +198,22 @@ pub fn inner_array_type(element_type: Type, dimensions: ArrayDimensions) -> Type Type::Array(Box::new(element_type), Some(dimensions)) } } + +/// +/// Custom Serializer for Type::Array. Handles the case when ArrayDimensions are None and turns it into +/// a Vec, where the only element is "0". +/// +fn serialize_array(type_: &Type, dimensions: &Option, serializer: S) -> Result +where + S: Serializer, +{ + let mut seq = serializer.serialize_seq(Some(2))?; + seq.serialize_element(type_)?; + // seq.serialize_element(dimensions)?; + if let Some(dimensions) = dimensions { + seq.serialize_element(&dimensions)?; + } else { + seq.serialize_element(&ArrayDimensions(vec![PositiveNumber { value: "0".into() }]))?; + } + seq.end() +} diff --git a/tests/expectations/compiler/compiler/array_without_size/definition.leo.out b/tests/expectations/compiler/compiler/array_without_size/definition.leo.out index c5828379b9..ce18b13169 100644 --- a/tests/expectations/compiler/compiler/array_without_size/definition.leo.out +++ b/tests/expectations/compiler/compiler/array_without_size/definition.leo.out @@ -16,7 +16,7 @@ outputs: r0: type: bool value: "true" - initial_ast: 5ef136cadd1330fb09fa0a4c27630bea068d1801edadb0d6073cfd23fab043e7 - imports_resolved_ast: 5ef136cadd1330fb09fa0a4c27630bea068d1801edadb0d6073cfd23fab043e7 - canonicalized_ast: 5ef136cadd1330fb09fa0a4c27630bea068d1801edadb0d6073cfd23fab043e7 - type_inferenced_ast: cdaf744ea99aa80bb5bf2e541084b1a51692b98c5815d807377db94d0b292dc2 + initial_ast: edf8a3b7372af353b99830752d41d8c04d1863a4d03c754f41aac3545649c644 + imports_resolved_ast: edf8a3b7372af353b99830752d41d8c04d1863a4d03c754f41aac3545649c644 + canonicalized_ast: edf8a3b7372af353b99830752d41d8c04d1863a4d03c754f41aac3545649c644 + type_inferenced_ast: 5479f110a1cbd68040560f64a09663207e756630aa2621a4bb424c48a3cab64d diff --git a/tests/expectations/compiler/compiler/array_without_size/function_input.leo.out b/tests/expectations/compiler/compiler/array_without_size/function_input.leo.out index e0a971d975..17c396727b 100644 --- a/tests/expectations/compiler/compiler/array_without_size/function_input.leo.out +++ b/tests/expectations/compiler/compiler/array_without_size/function_input.leo.out @@ -16,7 +16,7 @@ outputs: r0: type: bool value: "true" - initial_ast: 3871f9d6b5a028658777a540523dbeb2159dd0e47b11aefc4ec64d1b3aabd537 - imports_resolved_ast: 3871f9d6b5a028658777a540523dbeb2159dd0e47b11aefc4ec64d1b3aabd537 - canonicalized_ast: 3871f9d6b5a028658777a540523dbeb2159dd0e47b11aefc4ec64d1b3aabd537 - type_inferenced_ast: a25984c359654334f71221546da1ccd4da2cabeb6c8aeae72113bbaba622b440 + initial_ast: 5ae730ffb3671acde08944aaa8450a54bb9ce436c92d5c21e7a2a7b9c8d404a7 + imports_resolved_ast: 5ae730ffb3671acde08944aaa8450a54bb9ce436c92d5c21e7a2a7b9c8d404a7 + canonicalized_ast: 5ae730ffb3671acde08944aaa8450a54bb9ce436c92d5c21e7a2a7b9c8d404a7 + type_inferenced_ast: c2b15c1e0644a4af597019f7a56d1c8485723e43089ff0aa8951e3ec31729f1f diff --git a/tests/expectations/compiler/compiler/array_without_size/type_alias.leo.out b/tests/expectations/compiler/compiler/array_without_size/type_alias.leo.out index fda337a3b5..5e0c6d6871 100644 --- a/tests/expectations/compiler/compiler/array_without_size/type_alias.leo.out +++ b/tests/expectations/compiler/compiler/array_without_size/type_alias.leo.out @@ -16,7 +16,7 @@ outputs: r0: type: bool value: "true" - initial_ast: d6744be96fc8dbd32abfd290fa276a60c6a36d86ac991605af75f84d38bd913a - imports_resolved_ast: d6744be96fc8dbd32abfd290fa276a60c6a36d86ac991605af75f84d38bd913a - canonicalized_ast: e0c42db11f31a32f3ec40b4f6bd0109519408c530878ced2f7ce2e3729aefb8f - type_inferenced_ast: e4e14be0f8a9521b81f30d83e0258f8d84b5acf93563659f14da8f6aeefefc2a + initial_ast: 1695abb58931812bfe65ffdb967c9e8e36abbab771bfd8a20e289e3e1b102b5a + imports_resolved_ast: 1695abb58931812bfe65ffdb967c9e8e36abbab771bfd8a20e289e3e1b102b5a + canonicalized_ast: 637eaabe62c318b0c9f9d6d26936c11aa8804022866ce356e14dc02e29a34251 + type_inferenced_ast: 1540899195a176d1b1d0e1d886a671f07f063dd10fea15f853e67ffcfc0ae9ce