v_common_api/
app.rs

1extern crate nanoid;
2
3use nanoid::nanoid;
4use serde::Serialize;
5use serde::Serializer;
6
7#[derive(PartialEq, Debug, Clone, Copy)]
8#[repr(u16)]
9pub enum ResultCode {
10    /// 0
11    Zero = 0,
12
13    /// 200
14    Ok = 200,
15
16    /// 201
17    Created = 201,
18
19    /// 204
20    NoContent = 204,
21
22    /// 400
23    BadRequest = 400,
24
25    /// 403
26    Forbidden = 403,
27
28    /// 404
29    NotFound = 404,
30
31    /// 422
32    UnprocessableEntity = 422,
33
34    /// 423
35    Locked = 423,
36
37    /// 429
38    TooManyRequests = 429,
39
40    /// 430
41    TooManyRequestsChangePassword = 430,
42
43    /// 463
44    ChangePasswordForbidden = 463,
45
46    /// 464
47    SecretExpired = 464,
48
49    /// 465
50    EmptyPassword = 465,
51
52    /// 466
53    NewPasswordIsEqualToOld = 466,
54
55    /// 467
56    InvalidPassword = 467,
57
58    /// 468
59    InvalidSecret = 468,
60
61    /// 469
62    PasswordExpired = 469,
63
64    /// 470
65    TicketNotFound = 470,
66
67    /// 471
68    TicketExpired = 471,
69
70    /// 472
71    NotAuthorized = 472,
72
73    /// 473
74    AuthenticationFailed = 473,
75
76    /// 474
77    NotReady = 474,
78
79    /// 475
80    FailOpenTransaction = 475,
81
82    /// 476
83    FailCommit = 476,
84
85    /// 477
86    FailStore = 477,
87
88    /// 500
89    InternalServerError = 500,
90
91    /// 501
92    NotImplemented = 501,
93
94    /// 503
95    ServiceUnavailable = 503,
96
97    InvalidIdentifier = 904,
98
99    /// 999
100    DatabaseModifiedError = 999,
101
102    /// 1021
103    DiskFull = 1021,
104
105    /// 1022
106    DuplicateKey = 1022,
107
108    /// 1118
109    SizeTooLarge = 1118,
110
111    /// 4000
112    ConnectError = 4000,
113}
114
115#[derive(Debug, PartialEq)]
116pub enum OptAuthorize {
117    NO,
118    YES,
119}
120
121impl Serialize for ResultCode {
122    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
123    where
124        S: Serializer,
125    {
126        serializer.serialize_u16(*self as u16)
127    }
128}
129
130impl ResultCode {
131    pub fn from_i64(value: i64) -> ResultCode {
132        match value {
133            0 => ResultCode::Zero,
134            200 => ResultCode::Ok,
135            201 => ResultCode::Created,
136            204 => ResultCode::NoContent,
137            400 => ResultCode::BadRequest,
138            403 => ResultCode::Forbidden,
139            404 => ResultCode::NotFound,
140            422 => ResultCode::UnprocessableEntity,
141            429 => ResultCode::TooManyRequests,
142            464 => ResultCode::SecretExpired,
143            465 => ResultCode::EmptyPassword,
144            466 => ResultCode::NewPasswordIsEqualToOld,
145            467 => ResultCode::InvalidPassword,
146            468 => ResultCode::InvalidSecret,
147            469 => ResultCode::PasswordExpired,
148            470 => ResultCode::TicketNotFound,
149            471 => ResultCode::TicketExpired,
150            472 => ResultCode::NotAuthorized,
151            473 => ResultCode::AuthenticationFailed,
152            474 => ResultCode::NotReady,
153            475 => ResultCode::FailOpenTransaction,
154            476 => ResultCode::FailCommit,
155            477 => ResultCode::FailStore,
156            500 => ResultCode::InternalServerError,
157            501 => ResultCode::NotImplemented,
158            503 => ResultCode::ServiceUnavailable,
159            904 => ResultCode::InvalidIdentifier,
160            999 => ResultCode::DatabaseModifiedError,
161            1021 => ResultCode::DiskFull,
162            1022 => ResultCode::DuplicateKey,
163            1118 => ResultCode::SizeTooLarge,
164            4000 => ResultCode::ConnectError,
165            // ...
166            _ => ResultCode::Zero,
167        }
168    }
169}
170
171pub fn generate_unique_uri(prefix: &str, postfix: &str) -> String {
172    let alphabet: [char; 36] = [
173        '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
174        'w', 'x', 'y', 'z',
175    ];
176
177    format!("{}{}{}", prefix, nanoid!(24, &alphabet), postfix)
178}