1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use serde::ser::Error;
mod impls;
pub mod internal;
pub mod subtype;
pub use self::internal::{get_type, Field, Function, Label, Type, TypeId};
pub mod number;
pub mod principal;
pub mod reference;
pub mod reserved;
pub trait CandidType {
fn ty() -> Type {
let id = Self::id();
if let Some(t) = self::internal::find_type(&id) {
match t {
Type::Unknown => Type::Knot(id),
_ => t,
}
} else {
self::internal::env_add(id.clone(), Type::Unknown);
let t = Self::_ty();
self::internal::env_add(id.clone(), t.clone());
self::internal::env_id(id, t.clone());
t
}
}
fn id() -> TypeId {
TypeId::of::<Self>()
}
fn _ty() -> Type;
fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: Serializer;
}
pub trait Serializer: Sized {
type Error: Error;
type Compound: Compound<Error = Self::Error>;
fn serialize_bool(self, v: bool) -> Result<(), Self::Error>;
fn serialize_int(self, v: &crate::Int) -> Result<(), Self::Error>;
fn serialize_nat(self, v: &crate::Nat) -> Result<(), Self::Error>;
fn serialize_nat8(self, v: u8) -> Result<(), Self::Error>;
fn serialize_nat16(self, v: u16) -> Result<(), Self::Error>;
fn serialize_nat32(self, v: u32) -> Result<(), Self::Error>;
fn serialize_nat64(self, v: u64) -> Result<(), Self::Error>;
fn serialize_int8(self, v: i8) -> Result<(), Self::Error>;
fn serialize_int16(self, v: i16) -> Result<(), Self::Error>;
fn serialize_int32(self, v: i32) -> Result<(), Self::Error>;
fn serialize_int64(self, v: i64) -> Result<(), Self::Error>;
fn serialize_float32(self, v: f32) -> Result<(), Self::Error>;
fn serialize_float64(self, v: f64) -> Result<(), Self::Error>;
fn serialize_text(self, v: &str) -> Result<(), Self::Error>;
fn serialize_null(self, v: ()) -> Result<(), Self::Error>;
fn serialize_empty(self) -> Result<(), Self::Error>;
fn serialize_option<T: ?Sized>(self, v: Option<&T>) -> Result<(), Self::Error>
where
T: CandidType;
fn serialize_struct(self) -> Result<Self::Compound, Self::Error>;
fn serialize_vec(self, len: usize) -> Result<Self::Compound, Self::Error>;
fn serialize_blob(self, v: &[u8]) -> Result<(), Self::Error>;
fn serialize_variant(self, index: u64) -> Result<Self::Compound, Self::Error>;
fn serialize_principal(self, v: &[u8]) -> Result<(), Self::Error>;
fn serialize_function(self, v: &[u8], meth: &str) -> Result<(), Self::Error>;
}
pub trait Compound {
type Error;
fn serialize_element<T: ?Sized>(&mut self, v: &T) -> Result<(), Self::Error>
where
T: CandidType;
#[doc(hidden)]
fn serialize_blob(&mut self, blob: &[u8]) -> Result<(), Self::Error>;
}