use crate::candid::encode::{Encode, Writer};
use crate::candid::{
self, Decode, Error, Limits, Reader, Schema,
projection::{absent, projection},
};
pub use crate::candid::{Nat, encode::EncodeError};
use candidate_types::Principal;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Int(pub num_bigint::BigInt);
impl std::fmt::Display for Int {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
projection! {
both CertificateReply {
156_956_385 => method: String [required],
278_101_296 => blob_hash: String [required],
}
}
projection! {
encoding AccountRequest {
2_707_029_165 => account: Principal,
}
}
projection! {
Gauges {
390_851_327 => chunk_size_bytes: u64 [required],
2_550_751_805 => chunk_count: u64 [required],
2_562_436_397 => blob_count: u64 [required],
2_947_807_759 => blob_size_bytes: u64 [required],
}
}
projection! {
Account {
52_836_537 => account_owner_id: Principal [required],
2_564_671_811 => cycle_balance: Int [required],
2_695_045_597 => storage_usage: Gauges [required],
3_475_686_851 => last_charge_ts_ns: u64 [required],
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ErrorDetail;
impl Decode for ErrorDetail {
const SCHEMA: Schema = Schema::Primitive(-16);
fn read(reader: &mut Reader<'_, '_>, id: i64) -> Result<Self, Error> {
reader.skip(id)?;
Ok(Self)
}
}
projection! {
enum AccountError {
566_069_724 => AccountNotFound,
878_697_764 => OperationFailed(ErrorDetail),
1_653_746_489 => InsufficientBalance(ErrorDetail),
2_022_095_248 => InsufficientCycles(ErrorDetail),
2_102_411_630 => NotAuthorized(ErrorDetail),
3_028_638_836 => InvalidArgument(ErrorDetail),
3_638_018_673 => AccountBalanceOverflow,
4_052_296_485 => TopUpWithoutCycles,
}
}
projection! {
enum AccountResult {
17_724 => Ok(Account),
3_456_837 => Err(AccountError),
}
}
projection! {
encoding TopUpArgs {
2_504_008_430 => target_balance: Option<Nat>,
2_707_029_165 => account: Option<Principal>,
}
}
macro_rules! codec {
($decode:ident, $encode:ident, $ty:ty, $doc:literal) => {
#[doc = $doc]
pub fn $decode(bytes: &[u8], limits: Limits) -> Result<$ty, Error> {
candid::decode(bytes, limits)
}
#[doc = $doc]
pub fn $encode(value: &$ty, maximum_bytes: usize) -> Result<Vec<u8>, EncodeError> {
candid::encode::encode(value, maximum_bytes)
}
};
}
codec!(
decode_root,
encode_root,
String,
"Exactly one root text argument; syntax admission belongs to the caller."
);
codec!(
decode_roots,
encode_roots,
Vec<Vec<u8>>,
"Exactly one `vec blob` batch; no text-root fallback."
);
codec!(
decode_liveness,
encode_liveness,
Vec<bool>,
"Exactly one `vec bool` liveness result."
);
codec!(
decode_certificate,
encode_certificate,
CertificateReply,
"Exactly one plain provider certificate record."
);
pub fn decode_account(bytes: &[u8], limits: Limits) -> Result<AccountResult, Error> {
candid::decode(bytes, limits)
}
pub fn decode_empty(bytes: &[u8], limits: Limits) -> Result<(), Error> {
candid::decode_empty(bytes, limits)
}
pub fn encode_empty(maximum_bytes: usize) -> Result<Vec<u8>, EncodeError> {
const FRAME: &[u8] = b"DIDL\0\0";
if maximum_bytes < FRAME.len() {
return Err(EncodeError::OutputLimit);
}
let mut bytes = Vec::new();
bytes
.try_reserve_exact(FRAME.len())
.map_err(|_| EncodeError::Allocation)?;
bytes.extend_from_slice(FRAME);
Ok(bytes)
}
pub fn encode_top_up(
value: Option<&TopUpArgs>,
maximum_bytes: usize,
) -> Result<Vec<u8>, EncodeError> {
candid::encode::encode(&value, maximum_bytes)
}