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
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
    io,
};

#[derive(Debug)]
pub enum FileCenterError {
    MongoDBError(crate::mongodb::error::Error),
    DocumentError(crate::bson::document::ValueAccessError),
    FileSizeThresholdError,
    VersionError,
    DatabaseTooNewError { supported_latest: i32, current: i32 },
    IOError(io::Error),
    IDTokenError(&'static str),
}

impl Display for FileCenterError {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        match self {
            FileCenterError::MongoDBError(err) => Display::fmt(err, f),
            FileCenterError::DocumentError(err) => Display::fmt(err, f),
            FileCenterError::FileSizeThresholdError => {
                f.write_str("the file size threshold is incorrect")
            },
            FileCenterError::VersionError => f.write_str("the version is incorrect"),
            FileCenterError::DatabaseTooNewError {
                supported_latest,
                current,
            } => f.write_fmt(format_args!(
                "the current database version is {}, but this library only supports to {}",
                current, supported_latest
            )),
            FileCenterError::IOError(err) => Display::fmt(err, f),
            FileCenterError::IDTokenError(err) => f.write_str(err),
        }
    }
}

impl Error for FileCenterError {}

impl From<crate::mongodb::error::Error> for FileCenterError {
    #[inline]
    fn from(err: crate::mongodb::error::Error) -> Self {
        FileCenterError::MongoDBError(err)
    }
}

impl From<crate::bson::document::ValueAccessError> for FileCenterError {
    #[inline]
    fn from(err: crate::bson::document::ValueAccessError) -> Self {
        FileCenterError::DocumentError(err)
    }
}

impl From<io::Error> for FileCenterError {
    #[inline]
    fn from(err: io::Error) -> Self {
        FileCenterError::IOError(err)
    }
}