candid/types/
mod.rs

1//! Provides Candid type conversion and serialization.
2//!  * `CandidType` trait converts a Rust type to Candid type `types::Type`. The implementation for user-defined data types can be derived from `candid_derive` crate.
3//!  * `Serializer` trait serializes a Rust value to Candid binary format.
4//!    We do not use Serde's `Serialize` trait because Candid requires serializing types along with the values.
5//!    This is difficult to achieve in `Serialize`, especially for enum types.
6
7use 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, FuncMode, Function, Label, SharedLabel, Type, TypeId, TypeInner,
19};
20pub use type_env::TypeEnv;
21
22pub mod leb128;
23#[cfg(feature = "bignum")]
24pub mod number;
25pub mod principal;
26pub mod reference;
27pub mod reserved;
28pub mod result;
29
30pub mod arc;
31pub mod rc;
32
33pub trait CandidType {
34    // memoized type derivation
35    fn ty() -> Type {
36        let id = Self::id();
37        if let Some(t) = self::internal::find_type(&id) {
38            match *t {
39                TypeInner::Unknown => TypeInner::Knot(id).into(),
40                _ => t,
41            }
42        } else {
43            self::internal::env_add(id.clone(), TypeInner::Unknown.into());
44            let t = Self::_ty();
45            self::internal::env_add(id.clone(), t.clone());
46            self::internal::env_id(id, t.clone());
47            t
48        }
49    }
50    fn id() -> TypeId {
51        TypeId::of::<Self>()
52    }
53    fn _ty() -> Type;
54    // only serialize the value encoding
55    fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
56    where
57        S: Serializer;
58}
59
60pub trait Serializer: Sized {
61    type Error: Error;
62    type Compound: Compound<Error = Self::Error>;
63    fn serialize_bool(self, v: bool) -> Result<(), Self::Error>;
64    #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
65    #[cfg(feature = "bignum")]
66    fn serialize_int(self, v: &crate::Int) -> Result<(), Self::Error>;
67    fn serialize_i128(self, v: i128) -> Result<(), Self::Error>;
68    #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
69    #[cfg(feature = "bignum")]
70    fn serialize_nat(self, v: &crate::Nat) -> Result<(), Self::Error>;
71    fn serialize_u128(self, v: u128) -> Result<(), Self::Error>;
72    fn serialize_nat8(self, v: u8) -> Result<(), Self::Error>;
73    fn serialize_nat16(self, v: u16) -> Result<(), Self::Error>;
74    fn serialize_nat32(self, v: u32) -> Result<(), Self::Error>;
75    fn serialize_nat64(self, v: u64) -> Result<(), Self::Error>;
76    fn serialize_int8(self, v: i8) -> Result<(), Self::Error>;
77    fn serialize_int16(self, v: i16) -> Result<(), Self::Error>;
78    fn serialize_int32(self, v: i32) -> Result<(), Self::Error>;
79    fn serialize_int64(self, v: i64) -> Result<(), Self::Error>;
80    fn serialize_float32(self, v: f32) -> Result<(), Self::Error>;
81    fn serialize_float64(self, v: f64) -> Result<(), Self::Error>;
82    fn serialize_text(self, v: &str) -> Result<(), Self::Error>;
83    fn serialize_null(self, v: ()) -> Result<(), Self::Error>;
84    fn serialize_empty(self) -> Result<(), Self::Error>;
85    fn serialize_option<T>(self, v: Option<&T>) -> Result<(), Self::Error>
86    where
87        T: CandidType + ?Sized;
88    fn serialize_struct(self) -> Result<Self::Compound, Self::Error>;
89    fn serialize_vec(self, len: usize) -> Result<Self::Compound, Self::Error>;
90    fn serialize_blob(self, v: &[u8]) -> Result<(), Self::Error>;
91    fn serialize_variant(self, index: u64) -> Result<Self::Compound, Self::Error>;
92    fn serialize_principal(self, v: &[u8]) -> Result<(), Self::Error>;
93    fn serialize_function(self, v: &[u8], meth: &str) -> Result<(), Self::Error>;
94}
95
96pub trait Compound {
97    type Error;
98    fn serialize_element<T>(&mut self, v: &T) -> Result<(), Self::Error>
99    where
100        T: CandidType + ?Sized;
101    // Used for simulating serde(with = "serde_bytes"). We can remove this when specialization is stable in Rust,
102    // or generalize this function to take a closure for with.
103    #[doc(hidden)]
104    fn serialize_blob(&mut self, blob: &[u8]) -> Result<(), Self::Error>;
105}