bitcoinleveldb_status/status.rs
1/*!
2 | A Status encapsulates the result of an
3 | operation. It may indicate success, or it may
4 | indicate an error with an associated error
5 | message.
6 |
7 | Multiple threads can invoke const methods on
8 | a Status without external synchronization, but
9 | if any of the threads may call a non-const
10 | method, all threads accessing the same Status
11 | must use external synchronization.
12 */
13
14crate::ix!();
15
16//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/status.cc]
17//-------------------------------------------[.cpp/bitcoin/src/leveldb/include/leveldb/status.h]
18
19pub struct Status {
20
21 /**
22 | OK status has a null state_. Otherwise,
23 | state_ is a new[] array of the following
24 | form:
25 |
26 | state_[0..3] == length of message
27 | state_[4] == code
28 | state_[5..] == message
29 */
30 state: *const u8,
31}
32
33pub enum StatusCode {
34 Ok = 0,
35 NotFound = 1,
36 Corruption = 2,
37 NotSupported = 3,
38 InvalidArgument = 4,
39 IOError = 5
40}
41
42impl Default for Status {
43
44 /**
45 | Create a success status.
46 |
47 */
48 fn default() -> Self {
49 todo!();
50 /*
51 : state(nullptr),
52 */
53 }
54}
55
56impl Drop for Status {
57
58 fn drop(&mut self) {
59 todo!();
60 /*
61 delete[] state_;
62 */
63 }
64}
65
66impl Status {
67
68 pub fn new_from_other(rhs: Status) -> Self {
69
70 todo!();
71 /*
72 : state(rhs.state_),
73
74 rhs.state_ = nullptr;
75 */
76 }
77
78 pub fn new_from_other_copy(rhs: &Status) -> Self {
79
80 todo!();
81 /*
82 state_ = (rhs.state_ == nullptr)
83 ? nullptr : CopyState(rhs.state_);
84 */
85 }
86
87 /**
88 | Return a success status.
89 |
90 */
91 pub fn ok() -> crate::Status {
92
93 todo!();
94 /*
95 return Status();
96 */
97 }
98
99 /**
100 | Return error status of an appropriate
101 | type.
102 |
103 */
104 pub fn not_found(
105 msg: &Slice,
106 msg2: Option<&Slice>) -> crate::Status {
107
108 let df = Slice::default();
109 let msg2: &Slice = msg2.unwrap_or(&df);
110
111 todo!();
112 /*
113 return Status(kNotFound, msg, msg2);
114 */
115 }
116
117 pub fn corruption(
118 msg: &Slice,
119 msg2: Option<&Slice>) -> crate::Status {
120
121 let df = Slice::default();
122 let msg2: &Slice = msg2.unwrap_or(&df);
123
124 todo!();
125 /*
126 return Status(kCorruption, msg, msg2);
127 */
128 }
129
130 pub fn not_supported(
131 msg: &Slice,
132 msg2: Option<&Slice>) -> crate::Status {
133
134 let df = Slice::default();
135 let msg2: &Slice = msg2.unwrap_or(&df);
136
137 todo!();
138 /*
139 return Status(kNotSupported, msg, msg2);
140 */
141 }
142
143 pub fn invalid_argument(
144 msg: &Slice,
145 msg2: Option<&Slice>) -> crate::Status {
146
147 let df = Slice::default();
148 let msg2: &Slice = msg2.unwrap_or(&df);
149
150 todo!();
151 /*
152 return Status(kInvalidArgument, msg, msg2);
153 */
154 }
155
156 pub fn io_error(
157 msg: &Slice,
158 msg2: Option<&Slice>) -> crate::Status {
159
160 let df = Slice::default();
161 let msg2: &Slice = msg2.unwrap_or(&df);
162
163 todo!();
164 /*
165 return Status(kIOError, msg, msg2);
166 */
167 }
168
169 /**
170 | Returns true iff the status indicates
171 | success.
172 |
173 */
174 pub fn is_ok(&self) -> bool {
175
176 todo!();
177 /*
178 return (state_ == nullptr);
179 */
180 }
181
182 /**
183 | Returns true iff the status indicates
184 | a NotFound error.
185 |
186 */
187 pub fn is_not_found(&self) -> bool {
188
189 todo!();
190 /*
191 return code() == kNotFound;
192 */
193 }
194
195 /**
196 | Returns true iff the status indicates
197 | a Corruption error.
198 |
199 */
200 pub fn is_corruption(&self) -> bool {
201
202 todo!();
203 /*
204 return code() == kCorruption;
205 */
206 }
207
208 /**
209 | Returns true iff the status indicates
210 | an
211 |
212 | IOError.
213 |
214 */
215 pub fn is_io_error(&self) -> bool {
216
217 todo!();
218 /*
219 return code() == kIOError;
220 */
221 }
222
223 /**
224 | Returns true iff the status indicates
225 | a NotSupportedError.
226 |
227 */
228 pub fn is_not_supported_error(&self) -> bool {
229
230 todo!();
231 /*
232 return code() == kNotSupported;
233 */
234 }
235
236 /**
237 | Returns true iff the status indicates
238 | an
239 |
240 | InvalidArgument.
241 |
242 */
243 pub fn is_invalid_argument(&self) -> bool {
244
245 todo!();
246 /*
247 return code() == kInvalidArgument;
248 */
249 }
250
251 pub fn code(&self) -> StatusCode {
252
253 todo!();
254 /*
255 return (state_ == nullptr) ? kOk : static_cast<StatusCode>(state_[4]);
256 */
257 }
258
259 #[inline] pub fn assign_from_other_copy(&mut self, rhs: &Status) -> &mut Status {
260
261 todo!();
262 /*
263 // The following condition catches both aliasing (when this == &rhs),
264 // and the common case where both rhs and *this are ok.
265 if (state_ != rhs.state_) {
266 delete[] state_;
267 state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
268 }
269 return *this;
270 */
271 }
272
273 #[inline] pub fn assign_from_other_move(&mut self, rhs: Status) -> &mut Status {
274
275 todo!();
276 /*
277 std::swap(state_, rhs.state_);
278 return *this;
279 */
280 }
281
282 pub fn copy_state(&mut self, state: *const u8) -> *const u8 {
283
284 todo!();
285 /*
286 uint32_t size;
287 memcpy(&size, state, sizeof(size));
288 char* result = new char[size + 5];
289 memcpy(result, state, size + 5);
290 return result;
291 */
292 }
293
294 pub fn new(
295 code: StatusCode,
296 msg: &Slice,
297 msg2: &Slice) -> Self {
298
299 todo!();
300 /*
301
302
303 assert(code != kOk);
304 const uint32_t len1 = static_cast<uint32_t>(msg.size());
305 const uint32_t len2 = static_cast<uint32_t>(msg2.size());
306 const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
307 char* result = new char[size + 5];
308 memcpy(result, &size, sizeof(size));
309 result[4] = static_cast<char>(code);
310 memcpy(result + 5, msg.data(), len1);
311 if (len2) {
312 result[5 + len1] = ':';
313 result[6 + len1] = ' ';
314 memcpy(result + 7 + len1, msg2.data(), len2);
315 }
316 state_ = result;
317 */
318 }
319
320 /**
321 | Return a string representation of this status
322 | suitable for printing.
323 |
324 | Returns the string "OK" for success.
325 */
326 pub fn to_string(&self) -> String {
327
328 todo!();
329 /*
330 if (state_ == nullptr) {
331 return "OK";
332 } else {
333 char tmp[30];
334 const char* type;
335 switch (code()) {
336 case kOk:
337 type = "OK";
338 break;
339 case kNotFound:
340 type = "NotFound: ";
341 break;
342 case kCorruption:
343 type = "Corruption: ";
344 break;
345 case kNotSupported:
346 type = "Not implemented: ";
347 break;
348 case kInvalidArgument:
349 type = "Invalid argument: ";
350 break;
351 case kIOError:
352 type = "IO error: ";
353 break;
354 default:
355 snprintf(tmp, sizeof(tmp),
356 "Unknown code(%d): ", static_cast<int>(code()));
357 type = tmp;
358 break;
359 }
360 std::string result(type);
361 uint32_t length;
362 memcpy(&length, state_, sizeof(length));
363 result.append(state_ + 5, length);
364 return result;
365 }
366 */
367 }
368}