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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use reqwest::header::InvalidHeaderValue;
use serde_urlencoded::ser::Error as UrlEncodedError;
use thiserror::Error;
use url::ParseError;

use crate::{license::License, license_file::LicenseFileDataset, machine_file::MachineFileDataset};

#[derive(Error, Debug)]
pub enum Error {
    #[error("Unexpected error: {0}")]
    UnexpectedError(String),

    #[error("Invalid URL")]
    InvalidUrl,

    #[error("System clock is out of sync")]
    SystemClockUnsynced,

    #[error("Decryption error: {0}")]
    DecryptionError(String),

    #[error("HTTP client error: {0}")]
    HttpClient(#[from] reqwest::Error),

    #[error("URL parse error: {0}")]
    UrlParse(#[from] ParseError),

    #[error("JSON error: {0}")]
    JsonError(#[from] serde_json::Error),

    #[error("Invalid header value: {0}")]
    InvalidHeader(#[from] InvalidHeaderValue),

    #[error("URL encoding error: {0}")]
    UrlEncode(#[from] UrlEncodedError),

    #[error("Rate limit exceeded")]
    RateLimitExceeded {
        window: String,
        count: u32,
        limit: u32,
        remaining: u32,
        reset: u64,
        retry_after: u64,
    },

    #[error("License key is missing")]
    LicenseKeyMissing,

    #[error("License scheme is missing")]
    LicenseSchemeMissing,

    #[error("License scheme is not supported")]
    LicenseSchemeNotSupported,

    #[error("License is not signed")]
    LicenseNotSigned,

    #[error("License key is not genuine")]
    LicenseKeyNotGenuine,

    #[error("Public key is missing")]
    PublicKeyMissing,

    #[error("Public key is invalid")]
    PublicKeyInvalid,

    #[error("License scheme unsupported")]
    LicenseSchemeUnsupported,

    #[error("{0}")]
    CerificateFileInvalid(String),

    #[error("{0}")]
    CertificateFileNotGenuine(String),

    #[error("{0}")]
    CertificateFileNotSupported(String),

    #[error("Cerificate file expired")]
    CerificateFileExpired,

    #[error("License file invalid: {0}")]
    LicenseFileInvalid(String),

    #[error("License file not genuine: {0}")]
    LicenseFileNotGenuine(String),

    #[error("License file not supported: {0}")]
    LicenseFileNotSupported(String),

    #[error("License file not encrypted")]
    LicenseFileNotEncrypted,

    #[error("License file expired")]
    LicenseFileExpired(LicenseFileDataset),

    #[error("Machine file invalid")]
    MachineFileInvalid(String),

    #[error("Machine file not genuine")]
    MachineFileNotGenuine(String),

    #[error("Machine file not supported")]
    MachineFileNotSupported(String),

    #[error("License file expired")]
    MachineFileExpired(MachineFileDataset),

    #[error("API error: {detail}")]
    KeygenApiError {
        code: String,
        detail: String,
        body: serde_json::Value,
    },

    #[error("Token not allowed")]
    TokenNotAllowed { code: String, detail: String },

    #[error("Token format invalid")]
    TokenFormatInvalid { code: String, detail: String },

    #[error("Token invalid")]
    TokenInvalid { code: String, detail: String },

    #[error("Token expired")]
    TokenExpired { code: String, detail: String },

    #[error("License suspended")]
    LicenseSuspended { code: String, detail: String },

    #[error("License expired")]
    LicenseExpired { code: String, detail: String },

    #[error("License not allowed")]
    LicenseNotAllowed { code: String, detail: String },

    #[error("License not activated")]
    LicenseNotActivated {
        code: String,
        detail: String,
        license: License,
    },

    #[error("License key invalid")]
    LicenseKeyInvalid { code: String, detail: String },

    #[error("License token invalid")]
    LicenseTokenInvalid { code: String, detail: String },

    #[error("License has too many machines")]
    LicenseTooManyMachines { code: String, detail: String },

    #[error("License has too many cores")]
    LicenseTooManyCores { code: String, detail: String },

    #[error("License has too many processes")]
    LicenseTooManyProcesses { code: String, detail: String },

    #[error("Machine already activated")]
    MachineAlreadyActivated { code: String, detail: String },

    #[error("Machine limit exceeded")]
    MachineLimitExceeded { code: String, detail: String },

    #[error("Machine no longer exists")]
    MachineNotFound,

    #[error("Process limit exceeded")]
    ProcessLimitExceeded { code: String, detail: String },

    #[error("Process no longer exists")]
    ProcessNotFound,

    #[error("Component conflict")]
    ComponentConflict { code: String, detail: String },

    #[error("Component already activated")]
    ComponentAlreadyActivated { code: String, detail: String },

    #[error("Component is not activated")]
    ComponentNotActivated { code: String, detail: String },

    #[error("Environment error")]
    EnvironmentError { code: String, detail: String },

    #[error("Heartbeat dead")]
    HeartbeatDead { code: String, detail: String },

    #[error("Heartbeat ping failed")]
    HeartbeatPingFailed { code: String, detail: String },

    #[error("Heartbeat is required")]
    HeartbeatRequired { code: String, detail: String },

    #[error("Validation fingerprint scope is missing")]
    ValidationFingerprintMissing { code: String, detail: String },

    #[error("Validation components scope is missing")]
    ValidationComponentsMissing { code: String, detail: String },

    #[error("Validation product scope is missing")]
    ValidationProductMissing { code: String, detail: String },

    #[error("Not found")]
    NotFound { code: String, detail: String },
}

pub trait ErrorMeta {
    fn code(&self) -> String;
    fn detail(&self) -> String;
}

impl ErrorMeta for Error {
    fn code(&self) -> String {
        match self {
            Error::KeygenApiError { code, .. }
            | Error::TokenNotAllowed { code, .. }
            | Error::TokenFormatInvalid { code, .. }
            | Error::TokenInvalid { code, .. }
            | Error::TokenExpired { code, .. }
            | Error::LicenseSuspended { code, .. }
            | Error::LicenseExpired { code, .. }
            | Error::LicenseNotAllowed { code, .. }
            | Error::LicenseNotActivated { code, .. }
            | Error::LicenseKeyInvalid { code, .. }
            | Error::LicenseTokenInvalid { code, .. }
            | Error::LicenseTooManyMachines { code, .. }
            | Error::LicenseTooManyCores { code, .. }
            | Error::LicenseTooManyProcesses { code, .. }
            | Error::MachineAlreadyActivated { code, .. }
            | Error::MachineLimitExceeded { code, .. }
            | Error::ProcessLimitExceeded { code, .. }
            | Error::ComponentConflict { code, .. }
            | Error::ComponentAlreadyActivated { code, .. }
            | Error::ComponentNotActivated { code, .. }
            | Error::EnvironmentError { code, .. }
            | Error::HeartbeatDead { code, .. }
            | Error::HeartbeatPingFailed { code, .. }
            | Error::HeartbeatRequired { code, .. }
            | Error::ValidationFingerprintMissing { code, .. }
            | Error::ValidationComponentsMissing { code, .. }
            | Error::ValidationProductMissing { code, .. }
            | Error::NotFound { code, .. } => code.to_string(),
            _ => "ERROR".to_string(),
        }
    }

    fn detail(&self) -> String {
        match self {
            Error::KeygenApiError { detail, .. }
            | Error::TokenNotAllowed { detail, .. }
            | Error::TokenFormatInvalid { detail, .. }
            | Error::TokenInvalid { detail, .. }
            | Error::TokenExpired { detail, .. }
            | Error::LicenseSuspended { detail, .. }
            | Error::LicenseExpired { detail, .. }
            | Error::LicenseNotAllowed { detail, .. }
            | Error::LicenseNotActivated { detail, .. }
            | Error::LicenseKeyInvalid { detail, .. }
            | Error::LicenseTokenInvalid { detail, .. }
            | Error::LicenseTooManyMachines { detail, .. }
            | Error::LicenseTooManyCores { detail, .. }
            | Error::LicenseTooManyProcesses { detail, .. }
            | Error::MachineAlreadyActivated { detail, .. }
            | Error::MachineLimitExceeded { detail, .. }
            | Error::ProcessLimitExceeded { detail, .. }
            | Error::ComponentConflict { detail, .. }
            | Error::ComponentAlreadyActivated { detail, .. }
            | Error::ComponentNotActivated { detail, .. }
            | Error::EnvironmentError { detail, .. }
            | Error::HeartbeatDead { detail, .. }
            | Error::HeartbeatPingFailed { detail, .. }
            | Error::HeartbeatRequired { detail, .. }
            | Error::ValidationFingerprintMissing { detail, .. }
            | Error::ValidationComponentsMissing { detail, .. }
            | Error::ValidationProductMissing { detail, .. }
            | Error::NotFound { detail, .. } => detail.to_string(),
            _ => self.to_string(),
        }
    }
}