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// BootstrapStatusResponse
58//
59
60#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
61pub struct BootstrapStatusResponse {
62    pub ready: bool,
63    pub phase: String,
64    pub last_error: Option<String>,
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use candid::{Decode, Encode};
71    use serde::de::DeserializeOwned;
72    use std::fmt::Debug;
73
74    #[test]
75    fn app_mode_roundtrips_candid_through_dto_path() {
76        assert_enum_candid_contract(AppMode::Readonly);
77    }
78
79    #[test]
80    fn app_status_roundtrips_candid_through_dto_path() {
81        assert_enum_candid_contract(AppStatus::Readonly);
82    }
83
84    fn assert_enum_candid_contract<T>(value: T)
85    where
86        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
87    {
88        let bytes = Encode!(&value).expect("encode state enum");
89        let decoded = Decode!(&bytes, T).expect("decode state enum");
90
91        assert_eq!(decoded, value);
92    }
93}