mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-21 12:07:56 +03:00
Added custom serialization for spans in tuple-like enum variants; changed expected output in serialization test
This commit is contained in:
parent
a9746cb756
commit
73ff1d85e0
@ -22,18 +22,30 @@ use crate::{Char, CharValue};
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum ValueExpression {
|
||||
// todo: deserialize values here
|
||||
Address(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Boolean(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Address(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
Boolean(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
Char(CharValue),
|
||||
Field(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Field(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
Group(Box<GroupValue>),
|
||||
Implicit(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Implicit(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
Integer(
|
||||
IntegerType,
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
Span,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
String(Vec<Char>, Span),
|
||||
String(Vec<Char>, #[serde(with = "leo_errors::common::span_json")] Span),
|
||||
}
|
||||
|
||||
impl fmt::Display for ValueExpression {
|
||||
|
@ -26,7 +26,10 @@ use tendril::StrTendril;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum GroupCoordinate {
|
||||
Number(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Number(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
SignHigh,
|
||||
SignLow,
|
||||
Inferred,
|
||||
|
@ -26,7 +26,10 @@ use tendril::StrTendril;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum GroupValue {
|
||||
Single(#[serde(with = "leo_errors::common::tendril_json")] StrTendril, Span),
|
||||
Single(
|
||||
#[serde(with = "leo_errors::common::tendril_json")] StrTendril,
|
||||
#[serde(with = "leo_errors::common::span_json")] Span,
|
||||
),
|
||||
Tuple(GroupTuple),
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ use std::fmt;
|
||||
pub enum AssigneeAccess {
|
||||
ArrayRange(Option<Expression>, Option<Expression>),
|
||||
ArrayIndex(Expression),
|
||||
Tuple(PositiveNumber, Span),
|
||||
Tuple(PositiveNumber, #[serde(with = "leo_errors::common::span_json")] Span),
|
||||
Member(Identifier),
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,11 @@ pub use self::macros::*;
|
||||
pub mod span;
|
||||
pub use self::span::Span;
|
||||
|
||||
/// This module contains a custome serialize/deserialize
|
||||
/// implementation for Span.
|
||||
pub mod span_json;
|
||||
pub use self::span_json::*;
|
||||
|
||||
/// This module contains information on how to serialize and
|
||||
/// deserialze StrTendril type.
|
||||
pub mod tendril_json;
|
||||
|
55
errors/src/common/span_json.rs
Normal file
55
errors/src/common/span_json.rs
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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::Span;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use serde::de::{MapAccess, Visitor};
|
||||
use serde::ser::SerializeMap;
|
||||
use serde::{Deserializer, Serializer};
|
||||
|
||||
/// The AST contains a few tuple-like enum variants the contain spans
|
||||
/// #[derive(Serialize, Deserialize)] outputs these fields as anonmyous
|
||||
/// mappings, which makes them difficult to remove from the JSON AST.
|
||||
/// This function provides a custom serialization that maps the keyword
|
||||
/// `span` to the span information.
|
||||
pub fn serialize<S: Serializer>(span: &Span, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let mut map = serializer.serialize_map(Some(1))?;
|
||||
map.serialize_entry("span", span)?;
|
||||
map.end()
|
||||
}
|
||||
|
||||
/// Custom deserialization to enable removing spans from enums.
|
||||
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Span, D::Error> {
|
||||
deserializer.deserialize_map(SpanMapVisitor)
|
||||
}
|
||||
|
||||
/// This visitor is used by the deserializer to unwrap mappings
|
||||
/// and extract span information.
|
||||
struct SpanMapVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for SpanMapVisitor {
|
||||
type Value = Span;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("Mapping from `span` keyword to span information")
|
||||
}
|
||||
|
||||
fn visit_map<M: MapAccess<'de>>(self, mut access: M) -> Result<Self::Value, M::Error> {
|
||||
let (_, value): (String, Span) = access.next_entry()?.unwrap();
|
||||
Ok(value)
|
||||
}
|
||||
}
|
@ -26,12 +26,14 @@
|
||||
"U8",
|
||||
"1",
|
||||
{
|
||||
"line_start": 2,
|
||||
"line_stop": 2,
|
||||
"col_start": 12,
|
||||
"col_stop": 15,
|
||||
"path": "",
|
||||
"content": " return 1u8 + 1u8;"
|
||||
"span" : {
|
||||
"line_start": 2,
|
||||
"line_stop": 2,
|
||||
"col_start": 12,
|
||||
"col_stop": 15,
|
||||
"path": "",
|
||||
"content": " return 1u8 + 1u8;"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -42,12 +44,14 @@
|
||||
"U8",
|
||||
"1",
|
||||
{
|
||||
"line_start": 2,
|
||||
"line_stop": 2,
|
||||
"col_start": 18,
|
||||
"col_stop": 21,
|
||||
"path": "",
|
||||
"content": " return 1u8 + 1u8;"
|
||||
"span": {
|
||||
"line_start": 2,
|
||||
"line_stop": 2,
|
||||
"col_start": 18,
|
||||
"col_stop": 21,
|
||||
"path": "",
|
||||
"content": " return 1u8 + 1u8;"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user