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
pub enum Error {
    NotAnArchive,
    ChecksumMismatch,
    UnexpectedEnd,
    CorruptArchive,
    UnknownChunkingAlgorithm,
    UnknownCompression,
    IO(std::io::Error),
    DictionaryDecode(prost::DecodeError),
    DictionaryEncode(prost::EncodeError),
    #[cfg(feature = "lzma-compression")]
    LZMA(lzma::LzmaError),
    Http(reqwest::Error),
    ThreadJoin(tokio::task::JoinError),
}

impl std::error::Error for Error {}

#[cfg(feature = "lzma-compression")]
impl From<lzma::LzmaError> for Error {
    fn from(e: lzma::LzmaError) -> Self {
        Self::LZMA(e)
    }
}
impl From<prost::DecodeError> for Error {
    fn from(e: prost::DecodeError) -> Self {
        Self::DictionaryDecode(e)
    }
}

impl From<prost::EncodeError> for Error {
    fn from(e: prost::EncodeError) -> Self {
        Self::DictionaryEncode(e)
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self::IO(e)
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Self::Http(e)
    }
}

impl From<tokio::task::JoinError> for Error {
    fn from(e: tokio::task::JoinError) -> Self {
        Self::ThreadJoin(e)
    }
}

impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NotAnArchive => write!(f, "not an archive"),
            Error::ChecksumMismatch => write!(f, "checksum mismatch"),
            Error::UnexpectedEnd => write!(f, "unexpected end"),
            Error::CorruptArchive => write!(f, "corrupt archive"),
            Error::UnknownChunkingAlgorithm => write!(f, "unknown chunking algorithm"),
            Error::UnknownCompression => write!(f, "unknown compression"),
            Error::IO(e) => write!(f, "i/o error: {:?}", e),
            Error::DictionaryEncode(e) => write!(f, "failed to encode dictionary: {:?}", e),
            Error::DictionaryDecode(e) => write!(f, "failed to decode dictionary: {:?}", e),
            #[cfg(feature = "lzma-compression")]
            Error::LZMA(e) => write!(f, "lzma compression error: {:?}", e),
            Error::Http(e) => write!(f, "http error: {:?}", e),
            Error::ThreadJoin(e) => write!(f, "error joining thread: {:?}", e),
        }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NotAnArchive => write!(f, "is not an archive"),
            Error::ChecksumMismatch => write!(f, "checksum mismatch"),
            Error::UnexpectedEnd => write!(f, "unexpected end"),
            Error::CorruptArchive => write!(f, "corrupt archive"),
            Error::UnknownChunkingAlgorithm => write!(f, "unknown chunking algorithm"),
            Error::UnknownCompression => write!(f, "unknown compression"),
            Error::IO(e) => write!(f, "i/o error: {}", e),
            Error::DictionaryEncode(e) => write!(f, "failed to encode dictionary: {}", e),
            Error::DictionaryDecode(e) => write!(f, "failed to decode dictionary: {}", e),
            #[cfg(feature = "lzma-compression")]
            Error::LZMA(e) => write!(f, "lzma compression error: {}", e),
            Error::Http(e) => write!(f, "http error: {}", e),
            Error::ThreadJoin(e) => write!(f, "error joining thread: {}", e),
        }
    }
}