1use serde::ser::Error;
8
9mod impls;
10pub mod internal;
11pub mod subtype;
12pub mod type_env;
13#[cfg_attr(docsrs, doc(cfg(feature = "value")))]
14#[cfg(feature = "value")]
15pub mod value;
16
17pub use self::internal::{
18 get_type, Field, FieldDoc, FuncMode, Function, Label, SharedLabel, Type, TypeDoc, TypeDocs,
19 TypeId, TypeInner,
20};
21pub use type_env::TypeEnv;
22
23pub mod bounded_vec;
24pub mod leb128;
25#[cfg(feature = "bignum")]
26pub mod number;
27pub mod principal;
28pub mod reference;
29pub mod reserved;
30pub mod result;
31
32pub mod arc;
33pub mod rc;
34
35pub trait CandidType {
36 fn ty() -> Type {
38 let id = Self::id();
39 if let Some(t) = self::internal::find_type(&id) {
40 match *t {
41 TypeInner::Unknown => TypeInner::Knot(id).into(),
42 _ => t,
43 }
44 } else {
45 self::internal::env_add(id.clone(), TypeInner::Unknown.into());
46 let t = Self::_ty();
47 self::internal::env_add(id.clone(), t.clone());
48 self::internal::env_id(id.clone(), t.clone());
49 self::internal::env_doc(id, Self::_ty_doc());
50 t
51 }
52 }
53 fn id() -> TypeId {
54 TypeId::of::<Self>()
55 }
56 fn _ty() -> Type;
57 fn _ty_doc() -> internal::TypeDoc {
58 internal::TypeDoc::default()
59 }
60 fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
62 where
63 S: Serializer;
64}
65
66pub trait Serializer: Sized {
67 type Error: Error;
68 type Compound: Compound<Error = Self::Error>;
69 fn serialize_bool(self, v: bool) -> Result<(), Self::Error>;
70 #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
71 #[cfg(feature = "bignum")]
72 fn serialize_int(self, v: &crate::Int) -> Result<(), Self::Error>;
73 fn serialize_i128(self, v: i128) -> Result<(), Self::Error>;
74 #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
75 #[cfg(feature = "bignum")]
76 fn serialize_nat(self, v: &crate::Nat) -> Result<(), Self::Error>;
77 fn serialize_u128(self, v: u128) -> Result<(), Self::Error>;
78 fn serialize_nat8(self, v: u8) -> Result<(), Self::Error>;
79 fn serialize_nat16(self, v: u16) -> Result<(), Self::Error>;
80 fn serialize_nat32(self, v: u32) -> Result<(), Self::Error>;
81 fn serialize_nat64(self, v: u64) -> Result<(), Self::Error>;
82 fn serialize_int8(self, v: i8) -> Result<(), Self::Error>;
83 fn serialize_int16(self, v: i16) -> Result<(), Self::Error>;
84 fn serialize_int32(self, v: i32) -> Result<(), Self::Error>;
85 fn serialize_int64(self, v: i64) -> Result<(), Self::Error>;
86 fn serialize_float32(self, v: f32) -> Result<(), Self::Error>;
87 fn serialize_float64(self, v: f64) -> Result<(), Self::Error>;
88 fn serialize_text(self, v: &str) -> Result<(), Self::Error>;
89 fn serialize_null(self, v: ()) -> Result<(), Self::Error>;
90 fn serialize_empty(self) -> Result<(), Self::Error>;
91 fn serialize_option<T>(self, v: Option<&T>) -> Result<(), Self::Error>
92 where
93 T: CandidType + ?Sized;
94 fn serialize_struct(self) -> Result<Self::Compound, Self::Error>;
95 fn serialize_vec(self, len: usize) -> Result<Self::Compound, Self::Error>;
96 fn serialize_blob(self, v: &[u8]) -> Result<(), Self::Error>;
97 fn serialize_variant(self, index: u64) -> Result<Self::Compound, Self::Error>;
98 fn serialize_principal(self, v: &[u8]) -> Result<(), Self::Error>;
99 fn serialize_function(self, v: &[u8], meth: &str) -> Result<(), Self::Error>;
100}
101
102pub trait Compound {
103 type Error;
104 fn serialize_element<T>(&mut self, v: &T) -> Result<(), Self::Error>
105 where
106 T: CandidType + ?Sized;
107 #[doc(hidden)]
110 fn serialize_blob(&mut self, blob: &[u8]) -> Result<(), Self::Error>;
111 fn try_write_raw_elements(&mut self, _bytes: &[u8]) -> Result<bool, Self::Error> {
114 Ok(false)
115 }
116}