pub use crate::candid::encode::EncodeError;
use crate::candid::encode::{Encode, Writer};
use crate::candid::{
self, Decode, Error, Limits, Reader, Schema,
projection::{absent, projection},
};
use candidate_types::Principal;
use serde::{Deserialize, Serialize};
projection! {
encoding CanisterArgs {
1_313_628_723 => canister_id: Principal,
}
}
projection! {
encoding TakeSnapshotArgs {
435_757_295 => replace_snapshot: Option<Vec<u8>>,
1_313_628_723 => canister_id: Principal,
2_590_416_362 => uninstall_code: Option<bool>,
3_599_109_322 => sender_canister_version: Option<u64>,
}
}
projection! {
encoding SnapshotArgs {
1_313_628_723 => canister_id: Principal,
4_045_790_262 => snapshot_id: Vec<u8>,
}
}
projection! {
encoding LoadSnapshotArgs {
1_313_628_723 => canister_id: Principal,
3_599_109_322 => sender_canister_version: Option<u64>,
4_045_790_262 => snapshot_id: Vec<u8>,
}
}
projection! {
both ("management-snapshot-fixtures") enum Status {
1_130_484_237 => Stopped,
2_990_082_932 => Stopping,
3_949_555_199 => Running,
}
}
projection! {
both ("management-snapshot-fixtures") Settings {
570_880_087 => controllers: Vec<Principal> [required, max = 10],
}
}
projection! {
both ("management-snapshot-fixtures") CanisterStatus {
100_394_802 => status: Status [required],
2096228120 => version: u64 [required],
2_336_062_691 => settings: Settings [required],
2_928_387_969 => module_hash: Option<Vec<u8>> [strict_optional, max = 32],
}
}
projection! {
both ("management-snapshot-fixtures") CaptureStatus {
100_394_802 => status: Status [required],
2_336_062_691 => settings: Settings [required],
2_928_387_969 => module_hash: Option<Vec<u8>> [strict_optional, max = 32],
}
}
pub fn decode_capture_status(bytes: &[u8], limits: Limits) -> Result<CaptureStatus, Error> {
candid::decode(bytes, limits)
}
projection! {
both ("management-snapshot-fixtures") Snapshot {
23_515 => id: Vec<u8> [required, max = 256],
1_926_775_388 => total_size: u64 [required],
3_569_147_778 => taken_at_timestamp: u64 [required],
}
}
projection! {
both ("management-snapshot-fixtures") SnapshotData {
1_170_392_685 => chunk: Vec<u8> [required, max = 1_048_576],
}
}
projection! {
both ("management-snapshot-fixtures") SnapshotAllocation {
4_045_790_262 => snapshot_id: Vec<u8> [required, max = 256],
}
}
struct Inventory(Vec<Snapshot>);
impl Decode for Inventory {
const SCHEMA: Schema = <Vec<Snapshot> as Decode>::SCHEMA;
fn read(reader: &mut Reader<'_, '_>, id: i64) -> Result<Self, Error> {
reader.limited_value(id, 1024).map(Self)
}
}
pub fn decode_canister_status(bytes: &[u8], limits: Limits) -> Result<CanisterStatus, Error> {
candid::decode(bytes, limits)
}
pub fn decode_list_snapshots(bytes: &[u8], limits: Limits) -> Result<Vec<Snapshot>, Error> {
candid::decode::<Inventory>(bytes, limits).map(|value| value.0)
}
pub fn decode_take_snapshot(bytes: &[u8], limits: Limits) -> Result<Snapshot, Error> {
candid::decode(bytes, limits)
}
pub fn decode_snapshot_data(bytes: &[u8], limits: Limits) -> Result<SnapshotData, Error> {
candid::decode(bytes, limits)
}
pub fn decode_snapshot_allocation(
bytes: &[u8],
limits: Limits,
) -> Result<SnapshotAllocation, Error> {
candid::decode(bytes, limits)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Float32Bits(pub u32);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Float64Bits(pub u64);
macro_rules! float_bits {
($name:ident, $float:ty, $id:literal) => {
impl Decode for $name {
const SCHEMA: Schema = Schema::Primitive($id);
fn read(reader: &mut Reader<'_, '_>, id: i64) -> Result<Self, Error> {
reader
.value::<$float>(id)
.map(|value| Self(value.to_bits()))
}
}
impl Encode for $name {
const SCHEMA: Schema = Schema::Primitive($id);
fn write(&self, writer: &mut Writer<'_>) -> Result<(), EncodeError> {
writer.bytes(&self.0.to_le_bytes())
}
}
};
}
float_bits!(Float32Bits, f32, -13);
float_bits!(Float64Bits, f64, -14);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Reserved;
impl Decode for Reserved {
const SCHEMA: Schema = Schema::Primitive(-16);
fn read(_: &mut Reader<'_, '_>, id: i64) -> Result<Self, Error> {
if id == -16 {
Ok(Self)
} else {
Err(Error::Type)
}
}
}
impl Encode for Reserved {
const SCHEMA: Schema = Schema::Primitive(-16);
fn write(&self, _: &mut Writer<'_>) -> Result<(), EncodeError> {
Ok(())
}
}
projection! {
both enum Global {
5_083_781 => F32(Float32Bits),
5_084_452 => F64(Float64Bits),
5_232_968 => I32(i32),
5_233_639 => I64(i64),
1_311_016_833 => V128(u128),
}
}
projection! {
both ("management-snapshot-fixtures") enum SnapshotSource {
833_703_953 => MetadataUpload(Reserved),
2_359_638_948 => TakenFromCanister(Reserved),
}
}
projection! {
both enum Timer {
373_703_110 => Active(u64),
1_781_536_619 => Inactive,
}
}
projection! {
both enum LowMemoryHook {
1_043_953_570 => ConditionNotSatisfied,
3_210_470_543 => Executed,
3_870_990_435 => Ready,
}
}
projection! {
both SnapshotChunk {
1_158_164_430 => hash: Vec<u8> [required, max = 32],
}
}
projection! {
both ("management-snapshot-fixtures") SnapshotMetadata {
339_281_328 => globals: Vec<Option<Global>> [known_elements, max = 4096],
783_802_816 => canister_version: u64 [required],
842_117_339 => source: Option<SnapshotSource> [known_optional],
864_574_658 => certified_data: Vec<u8> [required, max = 32],
903_275_689 => global_timer: Option<Timer> [known_optional],
1_282_538_240 => on_low_wasm_memory_hook_status: Option<LowMemoryHook> [known_optional],
1_299_009_433 => wasm_module_size: u64 [required],
1_710_030_651 => stable_memory_size: u64 [required],
1_767_008_372 => wasm_chunk_store: Vec<SnapshotChunk> [required, max = 1024],
3_569_147_778 => taken_at_timestamp: u64 [required],
3_598_099_748 => wasm_memory_size: u64 [required],
}
}
pub fn decode_snapshot_metadata(bytes: &[u8], limits: Limits) -> Result<SnapshotMetadata, Error> {
candid::decode(bytes, limits)
}
projection! {
both Extent {
1_280_549_057 => size: u64 [required],
1_614_780_307 => offset: u64 [required],
}
}
projection! {
both Offset {
1_614_780_307 => offset: u64 [required],
}
}
projection! {
both enum ReadKind {
265_441_191 => WasmModule(Extent),
1_405_019_644 => WasmMemory(Extent),
2_465_514_309 => StableMemory(Extent),
4_161_627_090 => WasmChunk(SnapshotChunk),
}
}
projection! {
both enum WriteKind {
265_441_191 => WasmModule(Offset),
1_405_019_644 => WasmMemory(Offset),
2_465_514_309 => StableMemory(Offset),
4_161_627_090 => WasmChunk,
}
}
projection! {
encoding ReadDataArgs {
1_191_829_844 => kind: ReadKind,
1_313_628_723 => canister_id: Principal,
4_045_790_262 => snapshot_id: Vec<u8>,
}
}
projection! {
encoding UploadDataArgs {
1_170_392_685 => chunk: Vec<u8>,
1_191_829_844 => kind: WriteKind,
1_313_628_723 => canister_id: Principal,
4_045_790_262 => snapshot_id: Vec<u8>,
}
}
projection! {
encoding UploadMetadataArgs {
339_281_328 => globals: Vec<Global>,
435_757_295 => replace_snapshot: Option<Vec<u8>>,
864_574_658 => certified_data: Vec<u8>,
903_275_689 => global_timer: Option<Timer>,
1_282_538_240 => on_low_wasm_memory_hook_status: Option<LowMemoryHook>,
1_299_009_433 => wasm_module_size: u64,
1_313_628_723 => canister_id: Principal,
1_710_030_651 => stable_memory_size: u64,
3_598_099_748 => wasm_memory_size: u64,
}
}
pub fn decode_empty_response(bytes: &[u8], limits: Limits) -> Result<(), Error> {
candid::decode_empty(bytes, limits)
}
#[cfg(feature = "management-snapshot-fixtures")]
pub mod fixtures;