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
use std::convert::Infallible;
use std::{error, fmt, io};

/// Represents an error that can occur while using this library.
#[derive(Debug)]
pub enum Error<U = Infallible> {
    /// An [`io::Error`] occured either while reading or writing.
    Io(io::Error),
    /// A merge error occured while trying to merge values.
    Merge(U),
    /// An invalid [`crate::CompressionType`] has been encountered.
    InvalidCompressionType,
    /// This grenad file format is invalid.
    InvalidFormatVersion,
}

impl<U> Error<U> {
    pub(crate) fn convert_merge_error<V>(self) -> Error<V> {
        match self {
            Error::Io(io) => Error::Io(io),
            Error::InvalidCompressionType => Error::InvalidCompressionType,
            _ => panic!("cannot convert a merge error"),
        }
    }
}

impl<U: fmt::Display> fmt::Display for Error<U> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(io) => write!(f, "{}", io),
            Error::Merge(e) => write!(f, "merge error: {}", e),
            Error::InvalidCompressionType => f.write_str("invalid compression type"),
            Error::InvalidFormatVersion => f.write_str("invalid format version"),
        }
    }
}

impl<U: fmt::Display + fmt::Debug> error::Error for Error<U> {}

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

impl<U> From<Infallible> for Error<U> {
    fn from(_err: Infallible) -> Error<U> {
        unreachable!()
    }
}