canic_core/api/ic/
call.rs

1use crate::{
2    api::prelude::*,
3    cdk::candid::CandidType,
4    workflow::ic::call::{
5        CallBuilder as WorkflowCallBuilder, CallResult as WorkflowCallResult, CallWorkflow,
6    },
7};
8use candid::utils::{ArgumentDecoder, ArgumentEncoder};
9use serde::de::DeserializeOwned;
10
11///
12/// Call
13///
14/// Public IC call façade.
15///
16
17pub struct Call;
18
19impl Call {
20    #[must_use]
21    pub fn bounded_wait(canister_id: impl Into<Principal>, method: &str) -> CallBuilder {
22        CallBuilder {
23            inner: CallWorkflow::bounded_wait(canister_id, method),
24        }
25    }
26
27    #[must_use]
28    pub fn unbounded_wait(canister_id: impl Into<Principal>, method: &str) -> CallBuilder {
29        CallBuilder {
30            inner: CallWorkflow::unbounded_wait(canister_id, method),
31        }
32    }
33}
34
35///
36/// CallBuilder (api)
37///
38
39pub struct CallBuilder {
40    inner: WorkflowCallBuilder,
41}
42
43impl CallBuilder {
44    // ---------- arguments ----------
45
46    #[must_use]
47    pub fn with_arg<A>(self, arg: A) -> Self
48    where
49        A: CandidType,
50    {
51        Self {
52            inner: self.inner.with_arg(arg),
53        }
54    }
55
56    #[must_use]
57    pub fn with_args<A>(self, args: A) -> Self
58    where
59        A: ArgumentEncoder,
60    {
61        Self {
62            inner: self.inner.with_args(args),
63        }
64    }
65
66    pub fn try_with_arg<A>(self, arg: A) -> Result<Self, PublicError>
67    where
68        A: CandidType,
69    {
70        Ok(Self {
71            inner: self.inner.try_with_arg(arg).map_err(PublicError::from)?,
72        })
73    }
74
75    pub fn try_with_args<A>(self, args: A) -> Result<Self, PublicError>
76    where
77        A: ArgumentEncoder,
78    {
79        Ok(Self {
80            inner: self.inner.try_with_args(args).map_err(PublicError::from)?,
81        })
82    }
83
84    // ---------- cycles ----------
85
86    #[must_use]
87    pub fn with_cycles(self, cycles: u128) -> Self {
88        Self {
89            inner: self.inner.with_cycles(cycles),
90        }
91    }
92
93    // ---------- execution ----------
94
95    pub async fn execute(self) -> Result<CallResult, PublicError> {
96        Ok(CallResult {
97            inner: self.inner.execute().await.map_err(PublicError::from)?,
98        })
99    }
100}
101
102///
103/// CallResult (api)
104///
105/// Public, stable result wrapper.
106///
107
108pub struct CallResult {
109    inner: WorkflowCallResult,
110}
111
112impl CallResult {
113    pub fn candid<R>(&self) -> Result<R, PublicError>
114    where
115        R: CandidType + DeserializeOwned,
116    {
117        self.inner.candid().map_err(PublicError::from)
118    }
119
120    pub fn candid_tuple<R>(&self) -> Result<R, PublicError>
121    where
122        R: for<'de> ArgumentDecoder<'de>,
123    {
124        self.inner.candid_tuple().map_err(PublicError::from)
125    }
126}