Skip to main content

canic_core/dto/
state.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::state::{FleetMode, FleetStatus};
4
5/// Explicit input for changing automatic cycles funding.
6#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
7pub struct SetCyclesFundingRequest {
8    pub enabled: bool,
9}
10
11/// Explicit input for changing the Fleet's public status.
12#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
13pub struct SetFleetStatusRequest {
14    pub status: FleetStatus,
15}
16
17//
18// FleetCommand
19//
20
21#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
22pub enum FleetCommand {
23    SetStatus(FleetStatus),
24    SetCyclesFundingEnabled(bool),
25}
26
27//
28// FleetCommandResponse
29//
30
31#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
32pub enum FleetCommandResponse {
33    Status(SetStateResponse<FleetStatus>),
34    CyclesFundingEnabled(SetStateResponse<bool>),
35}
36
37//
38// SetStateResponse
39//
40
41#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
42pub struct SetStateResponse<T> {
43    pub previous: T,
44    pub current: T,
45    pub changed: bool,
46}
47
48//
49// FleetStateInput
50//
51
52#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
53pub struct FleetStateInput {
54    pub mode: FleetMode,
55    pub cycles_funding_enabled: bool,
56}
57
58//
59// FleetStateResponse
60//
61
62#[derive(CandidType, Deserialize)]
63pub struct FleetStateResponse {
64    pub mode: FleetMode,
65    pub cycles_funding_enabled: bool,
66}
67
68//
69// BootstrapStatusResponse
70//
71
72#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
73pub struct BootstrapStatusResponse {
74    pub ready: bool,
75    pub phase: String,
76    pub last_error: Option<String>,
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use candid::{Decode, Encode};
83    use serde::de::DeserializeOwned;
84    use std::fmt::Debug;
85
86    #[test]
87    fn fleet_mode_roundtrips_candid_through_dto_path() {
88        assert_enum_candid_contract(FleetMode::Readonly);
89    }
90
91    #[test]
92    fn fleet_status_roundtrips_candid_through_dto_path() {
93        assert_enum_candid_contract(FleetStatus::Readonly);
94    }
95
96    fn assert_enum_candid_contract<T>(value: T)
97    where
98        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
99    {
100        let bytes = Encode!(&value).expect("encode state enum");
101        let decoded = Decode!(&bytes, T).expect("decode state enum");
102
103        assert_eq!(decoded, value);
104    }
105}