Skip to content
Open
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
27 changes: 19 additions & 8 deletions rust/candid/src/parser/types.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
use crate::types::Label;
use crate::Result;
use pretty::RcDoc;
use crate::{CandidType, Deserialize};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chenyan-dfinity I tried to use CandidType below each place where I added Deserialize, but I got some macro errors. I think the issue is about the macro being used from the crate that defines it?


#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub enum IDLType {
#[serde(rename(serialize = "prim", deserialize = "prim"))]
PrimT(PrimType),
#[serde(rename(serialize = "var", deserialize = "var"))]
VarT(String),
#[serde(rename(serialize = "func", deserialize = "func"))]
FuncT(FuncType),
#[serde(rename(serialize = "opt", deserialize = "opt"))]
OptT(Box<IDLType>),
#[serde(rename(serialize = "vec", deserialize = "vec"))]
VecT(Box<IDLType>),
#[serde(rename(serialize = "record", deserialize = "record"))]
RecordT(Vec<TypeField>),
#[serde(rename(serialize = "variant", deserialize = "variant"))]
VariantT(Vec<TypeField>),
#[serde(rename(serialize = "service", deserialize = "service"))]
ServT(Vec<Binding>),
#[serde(rename(serialize = "class", deserialize = "class"))]
ClassT(Vec<IDLType>, Box<IDLType>),
#[serde(rename(serialize = "principal", deserialize = "principal"))]
PrincipalT,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub struct IDLTypes {
pub args: Vec<IDLType>,
}
Expand All @@ -25,7 +36,7 @@ macro_rules! enum_to_doc {
(pub enum $name:ident {
$($variant:ident),*,
}) => {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize)]
pub enum $name {
$($variant),*
}
Expand Down Expand Up @@ -72,7 +83,7 @@ pub enum FuncMode {
Query,
}}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub struct FuncType {
pub modes: Vec<FuncMode>,
pub args: Vec<IDLType>,
Expand All @@ -90,25 +101,25 @@ impl FuncType {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub struct TypeField {
pub label: Label,
pub typ: IDLType,
}

#[derive(Debug)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub enum Dec {
TypD(Binding),
ImportD(String),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub struct Binding {
pub id: String,
pub typ: IDLType,
}

#[derive(Debug)]
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Hash)]
pub struct IDLProg {
pub decs: Vec<Dec>,
pub actor: Option<IDLType>,
Expand Down
3 changes: 2 additions & 1 deletion rust/candid/src/types/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use num_enum::TryFromPrimitive;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use crate::Deserialize;

// This is a re-implementation of std::any::TypeId to get rid of 'static constraint.
// The current TypeId doesn't consider lifetime while computing the hash, which is
Expand Down Expand Up @@ -212,7 +213,7 @@ impl fmt::Display for Type {
}
}

#[derive(Debug, Eq, Clone)]
#[derive(Debug, Eq, Clone, Deserialize)]
pub enum Label {
Id(u32),
Named(String),
Expand Down