1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::fmt::Debug;

use candid::{CandidType, Nat, Principal};
use ex3_common_error_info::ErrorInfo;
use serde::Deserialize;

type CandidWalletRegisterId = Nat;
type CanisterId = Principal;
pub type ServiceResult<T> = anyhow::Result<T, ErrorInfo>;

/// When export_service, actor responses will merged by enum type, so if there is two response with same Ok type, the second response will be ignored.
/// So there is no need to create more than one response type for two boolean ok.
#[derive(CandidType, Deserialize, Debug, Clone)]
pub enum EmptyActorResponse {
    Ok(()),
    Err(ErrorInfo),
}

impl From<ServiceResult<()>> for EmptyActorResponse {
    fn from(result: ServiceResult<()>) -> Self {
        match result {
            Ok(()) => EmptyActorResponse::Ok(()),
            Err(err) => EmptyActorResponse::Err(err.into()),
        }
    }
}

/// When export_service, actor responses will merged by enum type, so if there is two response with same Ok type, the second response will be ignored.
/// So there is no need to create more than one response type for two boolean ok.
#[derive(CandidType, Deserialize, Debug)]
pub enum BooleanActorResponse {
    Ok(bool),
    Err(ErrorInfo),
}

impl From<ServiceResult<bool>> for BooleanActorResponse {
    fn from(result: ServiceResult<bool>) -> Self {
        match result {
            Ok(available) => BooleanActorResponse::Ok(available),
            Err(err) => BooleanActorResponse::Err(err.into()),
        }
    }
}

#[derive(CandidType)]
pub enum StringActorResponse {
    Ok(String),
    Err(ErrorInfo),
}

impl From<ServiceResult<String>> for StringActorResponse {
    fn from(result: ServiceResult<String>) -> Self {
        match result {
            Ok(available) => StringActorResponse::Ok(available),
            Err(err) => StringActorResponse::Err(err.into()),
        }
    }
}

/// When export_service, actor responses will merged by enum type, so if there is two response with same Ok type, the second response will be ignored.
/// So there is no need to create more than one response type for two boolean ok.
#[derive(CandidType, Deserialize, Debug)]
pub enum CanisterIdActorResponse {
    Ok(CanisterId),
    Err(ErrorInfo),
}

impl From<ServiceResult<CanisterId>> for CanisterIdActorResponse {
    fn from(result: ServiceResult<CanisterId>) -> Self {
        match result {
            Ok(available) => CanisterIdActorResponse::Ok(available),
            Err(err) => CanisterIdActorResponse::Err(err.into()),
        }
    }
}

/// When export_service, actor responses will merged by enum type, so if there is two response with same Ok type, the second response will be ignored.
/// So there is no need to create more than one response type for two boolean ok.
#[derive(CandidType, Deserialize, Debug)]
pub enum OptionCanisterIdActorResponse {
    Ok(Option<CanisterId>),
    Err(ErrorInfo),
}

impl From<ServiceResult<Option<CanisterId>>> for OptionCanisterIdActorResponse {
    fn from(result: ServiceResult<Option<CanisterId>>) -> Self {
        match result {
            Ok(available) => OptionCanisterIdActorResponse::Ok(available),
            Err(err) => OptionCanisterIdActorResponse::Err(err.into()),
        }
    }
}

/// When export_service, actor responses will merged by enum type, so if there is two response with same Ok type, the second response will be ignored.
/// So there is no need to create more than one response type for two boolean ok.
#[derive(CandidType)]
pub enum WalletRegisterIdActorResponse {
    Ok(CandidWalletRegisterId),
    Err(ErrorInfo),
}

impl From<ServiceResult<CandidWalletRegisterId>> for WalletRegisterIdActorResponse {
    fn from(result: ServiceResult<CandidWalletRegisterId>) -> Self {
        match result {
            Ok(available) => WalletRegisterIdActorResponse::Ok(available),
            Err(err) => WalletRegisterIdActorResponse::Err(err.into()),
        }
    }
}

#[derive(CandidType)]
pub enum GetStatsResponse<T> {
    Ok(T),
    Err(ErrorInfo),
}

impl<T> GetStatsResponse<T> {
    pub fn new(result: ServiceResult<T>) -> GetStatsResponse<T> {
        match result {
            Ok(stats) => GetStatsResponse::Ok(stats),
            Err(err) => GetStatsResponse::Err(err.into()),
        }
    }
}