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 Zero = 0,
12
13 Ok = 200,
15
16 Created = 201,
18
19 NoContent = 204,
21
22 BadRequest = 400,
24
25 Forbidden = 403,
27
28 NotFound = 404,
30
31 UnprocessableEntity = 422,
33
34 Locked = 423,
36
37 TooManyRequests = 429,
39
40 TooManyRequestsChangePassword = 430,
42
43 ChangePasswordForbidden = 463,
45
46 SecretExpired = 464,
48
49 EmptyPassword = 465,
51
52 NewPasswordIsEqualToOld = 466,
54
55 InvalidPassword = 467,
57
58 InvalidSecret = 468,
60
61 PasswordExpired = 469,
63
64 TicketNotFound = 470,
66
67 TicketExpired = 471,
69
70 NotAuthorized = 472,
72
73 AuthenticationFailed = 473,
75
76 NotReady = 474,
78
79 FailOpenTransaction = 475,
81
82 FailCommit = 476,
84
85 FailStore = 477,
87
88 InternalServerError = 500,
90
91 NotImplemented = 501,
93
94 ServiceUnavailable = 503,
96
97 InvalidIdentifier = 904,
98
99 DatabaseModifiedError = 999,
101
102 DiskFull = 1021,
104
105 DuplicateKey = 1022,
107
108 SizeTooLarge = 1118,
110
111 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 _ => 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}