custom serializer for Type::Array

This commit is contained in:
damirka 2021-09-02 18:27:20 +03:00
parent 041faa28db
commit 9bef8611e8
4 changed files with 34 additions and 14 deletions

View File

@ -14,12 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<Type>, Option<ArrayDimensions>),
Tuple(Vec<Type>),
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<PositiveNumber>, where the only element is "0".
///
fn serialize_array<S>(type_: &Type, dimensions: &Option<ArrayDimensions>, serializer: S) -> Result<S::Ok, S::Error>
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()
}

View File

@ -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

View File

@ -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

View File

@ -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