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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use std::env::VarError;
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use std::io::Error as IoError;
use std::result;

use rmp_serde::decode::Error as DecodeError;
use rmp_serde::encode::Error as EncodeError;

#[cfg(feature = "storage-sqlite")]
use libsqlite3_sys::Error as SqliteError;

#[cfg(feature = "storage-redis")]
use redis::RedisError;

#[cfg(feature = "storage-zbox")]
use http::{Error as HttpError, StatusCode};

#[cfg(feature = "storage-zbox")]
use serde_json::Error as JsonError;

#[cfg(feature = "storage-zbox-native")]
use reqwest::Error as ReqwestError;

#[cfg(feature = "storage-zbox-jni")]
use jni::errors::Error as JniError;

/// The error type for operations with [`Repo`] and [`File`].
///
/// [`Repo`]: struct.Repo.html
/// [`File`]: struct.File.html
#[derive(Debug)]
pub enum Error {
    RefOverflow,
    RefUnderflow,

    InitCrypto,
    NoAesHardware,
    Hashing,
    InvalidCost,
    InvalidCipher,
    Encrypt,
    Decrypt,

    InvalidUri,
    InvalidSuperBlk,
    Corrupted,
    Opened,
    WrongVersion,
    NoEntity,
    NotInSync,
    RepoClosed,

    InTrans,
    NotInTrans,
    NoTrans,
    Uncompleted,
    InUse,

    NoContent,

    InvalidArgument,
    InvalidPath,
    NotFound,
    AlreadyExists,
    IsRoot,
    IsDir,
    IsFile,
    NotDir,
    NotFile,
    NotEmpty,
    NoVersion,

    ReadOnly,
    CannotRead,
    CannotWrite,
    NotWrite,
    NotFinish,
    Closed,

    Encode(EncodeError),
    Decode(DecodeError),
    Var(VarError),
    Io(IoError),

    #[cfg(feature = "storage-sqlite")]
    Sqlite(SqliteError),

    #[cfg(feature = "storage-redis")]
    Redis(RedisError),

    #[cfg(feature = "storage-zbox")]
    Http(HttpError),
    #[cfg(feature = "storage-zbox")]
    HttpStatus(StatusCode),
    #[cfg(feature = "storage-zbox")]
    Json(JsonError),

    #[cfg(feature = "storage-zbox-native")]
    Reqwest(ReqwestError),

    #[cfg(feature = "storage-zbox-jni")]
    Jni(JniError),

    #[cfg(feature = "storage-zbox-wasm")]
    RequestError,
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            Error::RefOverflow => write!(f, "Refcnt overflow"),
            Error::RefUnderflow => write!(f, "Refcnt underflow"),

            Error::InitCrypto => write!(f, "InitCrypto crypto error"),
            Error::NoAesHardware => write!(f, "No AES hardware"),
            Error::Hashing => write!(f, "Hashing out of memory"),
            Error::InvalidCost => write!(f, "Invalid cost"),
            Error::InvalidCipher => write!(f, "Invalid cipher"),
            Error::Encrypt => write!(f, "Encrypt error"),
            Error::Decrypt => write!(f, "Decrypt error"),

            Error::InvalidUri => write!(f, "Invalid Uri"),
            Error::InvalidSuperBlk => write!(f, "Invalid super block"),
            Error::Corrupted => write!(f, "Volume is corrupted"),
            Error::Opened => write!(f, "Repo is opened"),
            Error::WrongVersion => write!(f, "Version not match"),
            Error::NoEntity => write!(f, "Entity not found"),
            Error::NotInSync => write!(f, "Repo is not in sync"),
            Error::RepoClosed => write!(f, "Repo is closed"),

            Error::InTrans => write!(f, "Already in transaction"),
            Error::NotInTrans => write!(f, "Not in transaction"),
            Error::NoTrans => write!(f, "Transaction not found"),
            Error::Uncompleted => write!(f, "Transaction uncompleted"),
            Error::InUse => write!(f, "Entity is in use"),

            Error::NoContent => write!(f, "Content not found"),

            Error::InvalidArgument => write!(f, "Invalid argument"),
            Error::InvalidPath => write!(f, "Invalid path"),
            Error::NotFound => write!(f, "File not found"),
            Error::AlreadyExists => write!(f, "File already exists"),
            Error::IsRoot => write!(f, "File is root"),
            Error::IsDir => write!(f, "Path is dir"),
            Error::IsFile => write!(f, "Path is file"),
            Error::NotDir => write!(f, "Path is not dir"),
            Error::NotFile => write!(f, "Path is not file"),
            Error::NotEmpty => write!(f, "Directory is not empty"),
            Error::NoVersion => write!(f, "File has no version"),

            Error::ReadOnly => write!(f, "Opened as read only"),
            Error::CannotRead => write!(f, "Cannot read file"),
            Error::CannotWrite => write!(f, "Cannot write file"),
            Error::NotWrite => write!(f, "File does not write yet"),
            Error::NotFinish => write!(f, "File does not finish yet"),
            Error::Closed => write!(f, "File is closed"),

            Error::Encode(ref err) => err.fmt(f),
            Error::Decode(ref err) => err.fmt(f),
            Error::Var(ref err) => err.fmt(f),
            Error::Io(ref err) => err.fmt(f),

            #[cfg(feature = "storage-sqlite")]
            Error::Sqlite(ref err) => err.fmt(f),

            #[cfg(feature = "storage-redis")]
            Error::Redis(ref err) => err.fmt(f),

            #[cfg(feature = "storage-zbox")]
            Error::Http(ref err) => err.fmt(f),
            #[cfg(feature = "storage-zbox")]
            Error::HttpStatus(status_code) => {
                write!(f, "Http status {}", status_code)
            }
            #[cfg(feature = "storage-zbox")]
            Error::Json(ref err) => err.fmt(f),

            #[cfg(feature = "storage-zbox-native")]
            Error::Reqwest(ref err) => err.fmt(f),

            #[cfg(feature = "storage-zbox-jni")]
            Error::Jni(ref err) => err.fmt(f),

            #[cfg(feature = "storage-zbox-wasm")]
            Error::RequestError => write!(f, "Http request failed"),
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::RefOverflow => "Refcnt overflow",
            Error::RefUnderflow => "Refcnt underflow",

            Error::InitCrypto => "Initialise crypto error",
            Error::NoAesHardware => "No AES hardware",
            Error::Hashing => "Hashing out of memory",
            Error::InvalidCost => "Invalid cost",
            Error::InvalidCipher => "Invalid cipher",
            Error::Encrypt => "Encrypt error",
            Error::Decrypt => "Decrypt error",

            Error::InvalidUri => "Invalid Uri",
            Error::InvalidSuperBlk => "Invalid super block",
            Error::Corrupted => "Volume is corrupted",
            Error::Opened => "Repo is opened",
            Error::WrongVersion => "Version not match",
            Error::NoEntity => "Entity not found",
            Error::NotInSync => "Repo is not in sync",
            Error::RepoClosed => "Repo is closed",

            Error::InTrans => "Already in transaction",
            Error::NotInTrans => "Not in transaction",
            Error::NoTrans => "Transaction not found",
            Error::Uncompleted => "Transaction uncompleted",
            Error::InUse => "Entity is in use",

            Error::NoContent => "Content not found",

            Error::InvalidArgument => "Invalid argument",
            Error::InvalidPath => "Invalid path",
            Error::NotFound => "File not found",
            Error::AlreadyExists => "File already exists",
            Error::IsRoot => "File is root",
            Error::IsDir => "Path is dir",
            Error::IsFile => "Path is file",
            Error::NotDir => "Path is not dir",
            Error::NotFile => "Path is not file",
            Error::NotEmpty => "Directory is not empty",
            Error::NoVersion => "File has no version",

            Error::ReadOnly => "Opened as read only",
            Error::CannotRead => "Cannot read file",
            Error::CannotWrite => "Cannot write file",
            Error::NotWrite => "File does not write yet",
            Error::NotFinish => "File does not finish yet",
            Error::Closed => "File is closed",

            Error::Encode(ref err) => err.description(),
            Error::Decode(ref err) => err.description(),
            Error::Var(ref err) => err.description(),
            Error::Io(ref err) => err.description(),

            #[cfg(feature = "storage-sqlite")]
            Error::Sqlite(ref err) => err.description(),

            #[cfg(feature = "storage-redis")]
            Error::Redis(ref err) => err.description(),

            #[cfg(feature = "storage-zbox")]
            Error::Http(ref err) => err.description(),
            #[cfg(feature = "storage-zbox")]
            Error::HttpStatus(_) => "Http status error",
            #[cfg(feature = "storage-zbox")]
            Error::Json(ref err) => err.description(),

            #[cfg(feature = "storage-zbox-native")]
            Error::Reqwest(ref err) => err.description(),

            #[cfg(feature = "storage-zbox-jni")]
            Error::Jni(ref err) => err.description(),

            #[cfg(feature = "storage-zbox-wasm")]
            Error::RequestError => "Http request error",
        }
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::Encode(ref err) => Some(err),
            Error::Decode(ref err) => Some(err),
            Error::Var(ref err) => Some(err),
            Error::Io(ref err) => Some(err),

            #[cfg(feature = "storage-sqlite")]
            Error::Sqlite(ref err) => Some(err),

            #[cfg(feature = "storage-redis")]
            Error::Redis(ref err) => Some(err),

            #[cfg(feature = "storage-zbox")]
            Error::Http(ref err) => Some(err),
            #[cfg(feature = "storage-zbox")]
            Error::Json(ref err) => Some(err),

            #[cfg(feature = "storage-zbox-native")]
            Error::Reqwest(ref err) => Some(err),

            #[cfg(feature = "storage-zbox-jni")]
            Error::Jni(ref err) => Some(err),

            _ => None,
        }
    }
}

impl From<EncodeError> for Error {
    fn from(err: EncodeError) -> Error {
        Error::Encode(err)
    }
}

impl From<DecodeError> for Error {
    fn from(err: DecodeError) -> Error {
        Error::Decode(err)
    }
}

impl From<VarError> for Error {
    fn from(err: VarError) -> Error {
        Error::Var(err)
    }
}

impl From<IoError> for Error {
    fn from(err: IoError) -> Error {
        Error::Io(err)
    }
}

#[cfg(feature = "storage-sqlite")]
impl From<SqliteError> for Error {
    fn from(err: SqliteError) -> Error {
        Error::Sqlite(err)
    }
}

#[cfg(feature = "storage-redis")]
impl From<RedisError> for Error {
    fn from(err: RedisError) -> Error {
        Error::Redis(err)
    }
}

#[cfg(feature = "storage-zbox")]
impl From<HttpError> for Error {
    fn from(err: HttpError) -> Error {
        Error::Http(err)
    }
}

#[cfg(feature = "storage-zbox")]
impl From<JsonError> for Error {
    fn from(err: JsonError) -> Error {
        Error::Json(err)
    }
}

#[cfg(feature = "storage-zbox-native")]
impl From<ReqwestError> for Error {
    fn from(err: ReqwestError) -> Error {
        Error::Reqwest(err)
    }
}

#[cfg(feature = "storage-zbox-jni")]
impl From<JniError> for Error {
    fn from(err: JniError) -> Error {
        Error::Jni(err)
    }
}

impl Into<i32> for Error {
    fn into(self) -> i32 {
        match self {
            Error::RefOverflow => -1000,
            Error::RefUnderflow => -1001,

            Error::InitCrypto => -1010,
            Error::NoAesHardware => -1011,
            Error::Hashing => -1012,
            Error::InvalidCost => -1013,
            Error::InvalidCipher => -1014,
            Error::Encrypt => -1015,
            Error::Decrypt => -1016,

            Error::InvalidUri => -1020,
            Error::InvalidSuperBlk => -1021,
            Error::Corrupted => -1022,
            Error::Opened => -1023,
            Error::WrongVersion => -1024,
            Error::NoEntity => -1025,
            Error::NotInSync => -1026,
            Error::RepoClosed => -1027,

            Error::InTrans => -1030,
            Error::NotInTrans => -1031,
            Error::NoTrans => -1032,
            Error::Uncompleted => -1033,
            Error::InUse => -1034,

            Error::NoContent => -1040,

            Error::InvalidArgument => -1050,
            Error::InvalidPath => -1051,
            Error::NotFound => -1052,
            Error::AlreadyExists => -1053,
            Error::IsRoot => -1054,
            Error::IsDir => -1055,
            Error::IsFile => -1056,
            Error::NotDir => -1057,
            Error::NotFile => -1058,
            Error::NotEmpty => -1059,
            Error::NoVersion => -1060,

            Error::ReadOnly => -1070,
            Error::CannotRead => -1071,
            Error::CannotWrite => -1072,
            Error::NotWrite => -1073,
            Error::NotFinish => -1074,
            Error::Closed => -1075,

            Error::Encode(_) => -2000,
            Error::Decode(_) => -2010,
            Error::Var(_) => -2020,
            Error::Io(_) => -2030,

            #[cfg(feature = "storage-sqlite")]
            Error::Sqlite(_) => -2040,

            #[cfg(feature = "storage-redis")]
            Error::Redis(_) => -2050,

            #[cfg(feature = "storage-zbox")]
            Error::Http(_) => -2060,
            #[cfg(feature = "storage-zbox")]
            Error::HttpStatus(_) => -2061,
            #[cfg(feature = "storage-zbox")]
            Error::Json(_) => -2062,

            #[cfg(feature = "storage-zbox-native")]
            Error::Reqwest(_) => -2063,

            #[cfg(feature = "storage-zbox-jni")]
            Error::Jni(_) => -2064,

            #[cfg(feature = "storage-zbox-wasm")]
            Error::RequestError => -2065,
        }
    }
}

impl PartialEq for Error {
    fn eq(&self, other: &Error) -> bool {
        match (self, other) {
            (&Error::RefOverflow, &Error::RefOverflow) => true,
            (&Error::RefUnderflow, &Error::RefUnderflow) => true,

            (&Error::InitCrypto, &Error::InitCrypto) => true,
            (&Error::NoAesHardware, &Error::NoAesHardware) => true,
            (&Error::Hashing, &Error::Hashing) => true,
            (&Error::InvalidCost, &Error::InvalidCost) => true,
            (&Error::InvalidCipher, &Error::InvalidCipher) => true,
            (&Error::Encrypt, &Error::Encrypt) => true,
            (&Error::Decrypt, &Error::Decrypt) => true,

            (&Error::InvalidUri, &Error::InvalidUri) => true,
            (&Error::InvalidSuperBlk, &Error::InvalidSuperBlk) => true,
            (&Error::Corrupted, &Error::Corrupted) => true,
            (&Error::Opened, &Error::Opened) => true,
            (&Error::WrongVersion, &Error::WrongVersion) => true,
            (&Error::NoEntity, &Error::NoEntity) => true,
            (&Error::NotInSync, &Error::NotInSync) => true,
            (&Error::RepoClosed, &Error::RepoClosed) => true,

            (&Error::InTrans, &Error::InTrans) => true,
            (&Error::NotInTrans, &Error::NotInTrans) => true,
            (&Error::NoTrans, &Error::NoTrans) => true,
            (&Error::Uncompleted, &Error::Uncompleted) => true,
            (&Error::InUse, &Error::InUse) => true,

            (&Error::NoContent, &Error::NoContent) => true,

            (&Error::InvalidArgument, &Error::InvalidArgument) => true,
            (&Error::InvalidPath, &Error::InvalidPath) => true,
            (&Error::NotFound, &Error::NotFound) => true,
            (&Error::AlreadyExists, &Error::AlreadyExists) => true,
            (&Error::IsRoot, &Error::IsRoot) => true,
            (&Error::IsDir, &Error::IsDir) => true,
            (&Error::IsFile, &Error::IsFile) => true,
            (&Error::NotDir, &Error::NotDir) => true,
            (&Error::NotFile, &Error::NotFile) => true,
            (&Error::NotEmpty, &Error::NotEmpty) => true,
            (&Error::NoVersion, &Error::NoVersion) => true,

            (&Error::ReadOnly, &Error::ReadOnly) => true,
            (&Error::CannotRead, &Error::CannotRead) => true,
            (&Error::CannotWrite, &Error::CannotWrite) => true,
            (&Error::NotWrite, &Error::NotWrite) => true,
            (&Error::NotFinish, &Error::NotFinish) => true,
            (&Error::Closed, &Error::Closed) => true,

            (&Error::Encode(_), &Error::Encode(_)) => true,
            (&Error::Decode(_), &Error::Decode(_)) => true,
            (&Error::Var(_), &Error::Var(_)) => true,
            (&Error::Io(ref a), &Error::Io(ref b)) => a.kind() == b.kind(),

            #[cfg(feature = "storage-sqlite")]
            (&Error::Sqlite(ref a), &Error::Sqlite(ref b)) => a == b,

            #[cfg(feature = "storage-redis")]
            (&Error::Redis(ref a), &Error::Redis(ref b)) => {
                a.kind() == b.kind()
            }

            #[cfg(feature = "storage-zbox")]
            (&Error::HttpStatus(a), &Error::HttpStatus(b)) => a == b,

            #[cfg(feature = "storage-zbox-native")]
            (&Error::Reqwest(ref a), &Error::Reqwest(ref b)) => {
                a.status() == b.status()
            }

            #[cfg(feature = "storage-zbox-jni")]
            (&Error::Jni(ref a), &Error::Jni(ref b)) => {
                a.kind().description() == b.kind().description()
            }

            #[cfg(feature = "storage-zbox-wasm")]
            (&Error::RequestError, &Error::RequestError) => true,

            (_, _) => false,
        }
    }
}

/// A specialized [`Result`] type for ZboxFS operations.
///
/// See the [`zbox::Error`] for all the  errors.
///
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
/// [`zbox::Error`]: enum.Error.html
pub type Result<T> = result::Result<T, Error>;