Skip to main content

canic_core/dto/
state.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::state::{AppMode, AppStatus};
4
5//
6// AppCommand
7//
8
9#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
10pub enum AppCommand {
11    SetStatus(AppStatus),
12    SetCyclesFundingEnabled(bool),
13}
14
15//
16// AppCommandResponse
17//
18
19#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
20pub enum AppCommandResponse {
21    Status(SetStateResponse<AppStatus>),
22    CyclesFundingEnabled(SetStateResponse<bool>),
23}
24
25//
26// SetStateResponse
27//
28
29#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
30pub struct SetStateResponse<T> {
31    pub previous: T,
32    pub current: T,
33    pub changed: bool,
34}
35
36//
37// AppStateInput
38//
39
40#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
41pub struct AppStateInput {
42    pub mode: AppMode,
43    pub cycles_funding_enabled: bool,
44}
45
46//
47// AppStateResponse
48//
49
50#[derive(CandidType, Deserialize)]
51pub struct AppStateResponse {
52    pub mode: AppMode,
53    pub cycles_funding_enabled: bool,
54}
55
56//
57// SubnetAuthStateInput
58//
59
60#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
61pub struct SubnetAuthStateInput {}
62
63//
64// SubnetStateInput
65//
66
67#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
68pub struct SubnetStateInput {
69    pub auth: SubnetAuthStateInput,
70}
71
72//
73// SubnetStateResponse
74//
75
76#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
77pub struct SubnetStateResponse {
78    pub auth: SubnetAuthStateInput,
79}
80
81//
82// BootstrapStatusResponse
83//
84
85#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
86pub struct BootstrapStatusResponse {
87    pub ready: bool,
88    pub phase: String,
89    pub last_error: Option<String>,
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use candid::{Decode, Encode};
96    use serde::de::DeserializeOwned;
97    use std::fmt::Debug;
98
99    #[test]
100    fn app_mode_roundtrips_candid_through_dto_path() {
101        assert_enum_candid_contract(AppMode::Readonly);
102    }
103
104    #[test]
105    fn app_status_roundtrips_candid_through_dto_path() {
106        assert_enum_candid_contract(AppStatus::Readonly);
107    }
108
109    fn assert_enum_candid_contract<T>(value: T)
110    where
111        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
112    {
113        let bytes = Encode!(&value).expect("encode state enum");
114        let decoded = Decode!(&bytes, T).expect("decode state enum");
115
116        assert_eq!(decoded, value);
117    }
118}