Skip to main content

canic_core/dto/
state.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::state::AppMode;
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// AppStatus
38//
39
40#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
41pub enum AppStatus {
42    Active,
43    Readonly,
44    Stopped,
45}
46
47//
48// AppStateInput
49//
50
51#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
52pub struct AppStateInput {
53    pub mode: AppMode,
54    pub cycles_funding_enabled: bool,
55}
56
57//
58// AppStateResponse
59//
60
61#[derive(CandidType, Deserialize)]
62pub struct AppStateResponse {
63    pub mode: AppMode,
64    pub cycles_funding_enabled: bool,
65}
66
67//
68// SubnetAuthStateInput
69//
70
71#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
72pub struct SubnetAuthStateInput {}
73
74//
75// SubnetStateInput
76//
77
78#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
79pub struct SubnetStateInput {
80    pub auth: SubnetAuthStateInput,
81}
82
83//
84// SubnetStateResponse
85//
86
87#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
88pub struct SubnetStateResponse {
89    pub auth: SubnetAuthStateInput,
90}
91
92//
93// BootstrapStatusResponse
94//
95
96#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
97pub struct BootstrapStatusResponse {
98    pub ready: bool,
99    pub phase: String,
100    pub last_error: Option<String>,
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use candid::{Decode, Encode};
107    use serde::de::DeserializeOwned;
108    use std::fmt::Debug;
109
110    #[test]
111    fn app_mode_roundtrips_candid_through_dto_path() {
112        assert_enum_candid_contract(AppMode::Readonly);
113    }
114
115    fn assert_enum_candid_contract<T>(value: T)
116    where
117        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
118    {
119        let bytes = Encode!(&value).expect("encode state enum");
120        let decoded = Decode!(&bytes, T).expect("decode state enum");
121
122        assert_eq!(decoded, value);
123    }
124}