Skip to main content

junobuild_utils/serializers/
types.rs

1use candid::Principal as CandidPrincipal;
2
3/// Represents a wrapper around the `candid::Principal` type.
4///
5/// This struct is designed to encapsulate a Candid Principal, allowing for
6/// integration and usage within Juno hooks contexts.
7///
8/// # Fields
9/// - `value`: The `Principal` this struct wraps.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct JsonDataPrincipal {
12    pub value: CandidPrincipal,
13}
14
15/// Represents a large integer value for document data, particularly useful for interacting with
16/// languages or environments that support large numeric types, such as the `bigint` in JavaScript.
17///
18/// # Fields
19/// - `value`: A `u64` integer representing the large numeric value encapsulated by this struct.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct JsonDataBigInt {
22    pub value: u64,
23}
24
25/// Represents an arbitrary precision natural number for document data,
26/// mirroring Candid's `nat` type. Useful for values that exceed `u64::MAX`
27/// or must be compatible with Candid's `nat` type.
28///
29/// # Fields
30/// - `value`: A `u128` integer representing the natural number.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
32pub struct JsonDataNat {
33    pub value: u128,
34}
35
36/// Represents a byte array value for document data, mirroring JavaScript's `Uint8Array`.
37///
38/// This struct is useful for transporting raw binary data across the JSON boundary,
39/// where it is serialized as `{ "__uint8array__": number[] }` (see custom serde impl).
40///
41/// # Fields
42/// - `value`: The underlying bytes.
43#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct JsonDataUint8Array {
45    pub value: Vec<u8>,
46}
47/// Alias for [`JsonDataPrincipal`].
48pub type DocDataPrincipal = JsonDataPrincipal;
49
50/// Alias for [`JsonDataBigInt`].
51pub type DocDataBigInt = JsonDataBigInt;
52
53/// Alias for [`JsonDataUint8Array`].
54pub type DocDataUint8Array = JsonDataUint8Array;