ic_kit_sys/
types.rs

1use std::error;
2use std::fmt;
3
4/// The result of `candid::encode_args(())` which is used as the default argument.
5pub const CANDID_EMPTY_ARG: &[u8] = &[68, 73, 68, 76, 0, 0];
6
7/// Rejection code from calling another canister.
8#[allow(missing_docs)]
9#[repr(i32)]
10#[derive(Debug, Clone, Copy)]
11pub enum RejectionCode {
12    NoError = 0,
13    SysFatal = 1,
14    SysTransient = 2,
15    DestinationInvalid = 3,
16    CanisterReject = 4,
17    CanisterError = 5,
18    Unknown,
19}
20
21impl From<i32> for RejectionCode {
22    fn from(code: i32) -> Self {
23        match code {
24            0 => RejectionCode::NoError,
25            1 => RejectionCode::SysFatal,
26            2 => RejectionCode::SysTransient,
27            3 => RejectionCode::DestinationInvalid,
28            4 => RejectionCode::CanisterReject,
29            5 => RejectionCode::CanisterError,
30            _ => RejectionCode::Unknown,
31        }
32    }
33}
34
35impl From<u32> for RejectionCode {
36    fn from(code: u32) -> Self {
37        RejectionCode::from(code as i32)
38    }
39}
40
41#[derive(Debug)]
42pub enum CallError {
43    /// Indicates that the `ic0::call_perform` failed and the call is not queued.
44    CouldNotSend,
45    /// The rejection callback wsa called from the IC, the call failed with the given rejection
46    /// code and message.
47    Rejected(RejectionCode, String),
48    /// The call happened successfully, but there was an error during deserialization of the
49    /// response.
50    /// The raw response is captured here.
51    ResponseDeserializationError(Vec<u8>),
52}
53
54impl fmt::Display for CallError {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        match self {
57            CallError::CouldNotSend => f.write_str("Could not send message"),
58            CallError::Rejected(c, m) => write!(f, "Call rejected (code={:?}): '{}'", c, m),
59            CallError::ResponseDeserializationError(..) => {
60                f.write_str("Could not deserialize the response.")
61            }
62        }
63    }
64}
65
66impl error::Error for CallError {}
67
68/// A possible error value when dealing with stable memory.
69#[derive(Debug, Eq, PartialEq)]
70pub enum StableMemoryError {
71    /// No more stable memory could be allocated.
72    OutOfMemory,
73    /// Attempted to read more stable memory than had been allocated.
74    OutOfBounds,
75}
76
77impl fmt::Display for StableMemoryError {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        match self {
80            Self::OutOfMemory => f.write_str("Out of memory"),
81            Self::OutOfBounds => f.write_str("Read exceeds allocated memory"),
82        }
83    }
84}
85
86impl error::Error for StableMemoryError {}