#![doc = include_str!("README.md")]
use std::borrow::Cow;
pub use crate::Principal;
use crate::cdk::call::Call as RawCall;
pub use crate::cdk::call::{
CallFailed, CallPerformFailed, CallRejected, InsufficientLiquidCycleBalance, RejectCode,
Response, UnrecognizedRejectCode, WaitPolicy,
};
#[derive(Debug)]
#[must_use = "a prepared call does nothing until execute is polled"]
pub struct Call<'m, 'a> {
target: Principal,
method: Cow<'m, str>,
args: Cow<'a, [u8]>,
wait: WaitPolicy,
cycles: u128,
}
impl<'m> Call<'m, 'static> {
pub fn new(target: Principal, method: impl Into<Cow<'m, str>>, wait: WaitPolicy) -> Self {
Self {
target,
method: method.into(),
args: Cow::Borrowed(&[]),
wait,
cycles: 0,
}
}
}
impl<'m, 'a> Call<'m, 'a> {
pub fn with_raw_args<'b>(self, args: &'b [u8]) -> Call<'m, 'b> {
Call {
target: self.target,
method: self.method,
args: Cow::Borrowed(args),
wait: self.wait,
cycles: self.cycles,
}
}
pub fn take_raw_args(self, args: Vec<u8>) -> Call<'m, 'static> {
Call {
target: self.target,
method: self.method,
args: Cow::Owned(args),
wait: self.wait,
cycles: self.cycles,
}
}
pub const fn with_cycles(mut self, cycles: u128) -> Self {
self.cycles = cycles;
self
}
pub const fn with_wait_policy(mut self, wait: WaitPolicy) -> Self {
self.wait = wait;
self
}
pub const fn target(&self) -> Principal {
self.target
}
pub fn method(&self) -> &str {
self.method.as_ref()
}
pub fn raw_args(&self) -> &[u8] {
self.args.as_ref()
}
pub const fn attached_cycles(&self) -> u128 {
self.cycles
}
pub const fn wait_policy(&self) -> WaitPolicy {
self.wait
}
pub fn get_cost(&self) -> u128 {
self.raw().get_cost()
}
pub async fn execute(self) -> Result<Response, CallFailed> {
self.raw().await
}
fn raw(&self) -> RawCall<'_, '_> {
RawCall::unbounded_wait(self.target, self.method.as_ref())
.with_wait_policy(self.wait)
.with_cycles(self.cycles)
.with_raw_args(self.args.as_ref())
}
}
#[cfg(test)]
mod tests;