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
use std::fmt;
use std::io::Error as IoError;
use zip::result::ZipError;

/// An EU4 Error
#[derive(Debug)]
pub struct Eu4Error(Box<Eu4ErrorKind>);

impl Eu4Error {
    pub(crate) fn new(kind: Eu4ErrorKind) -> Eu4Error {
        Eu4Error(Box::new(kind))
    }

    /// Return the specific type of error
    pub fn kind(&self) -> &Eu4ErrorKind {
        &self.0
    }
}

/// Specific type of error
#[derive(Debug)]
pub enum Eu4ErrorKind {
    ZipCentralDirectory(ZipError),
    ZipMissingEntry(&'static str, ZipError),
    ZipExtraction(&'static str, IoError),
    ZipSize(&'static str),
    IoErr(IoError),
    UnknownHeader,
    UnknownToken {
        token_id: u16,
    },
    Deserialize {
        part: Option<String>,
        err: jomini::Error,
    },
}

impl fmt::Display for Eu4Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind() {
            Eu4ErrorKind::ZipCentralDirectory(_) => {
                write!(f, "unable to read zip central directory")
            }
            Eu4ErrorKind::ZipMissingEntry(s, _) => write!(f, "unable to locate {} in zip", s),
            Eu4ErrorKind::ZipExtraction(s, _) => write!(f, "unable to extract {} in zip", s),
            Eu4ErrorKind::ZipSize(s) => write!(f, "{} in zip is too large", s),
            Eu4ErrorKind::IoErr(_) => write!(f, "io error"),
            Eu4ErrorKind::UnknownHeader => write!(f, "unknown header encountered in zip"),
            Eu4ErrorKind::UnknownToken { token_id } => {
                write!(f, "unknown binary token encountered (id: {})", token_id)
            }
            Eu4ErrorKind::Deserialize { ref part, ref err } => match part {
                Some(p) => write!(f, "error deserializing: {}: {}", p, err),
                None => err.fmt(f),
            },
        }
    }
}

impl std::error::Error for Eu4Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self.kind() {
            Eu4ErrorKind::ZipCentralDirectory(e) => Some(e),
            Eu4ErrorKind::ZipMissingEntry(_, e) => Some(e),
            Eu4ErrorKind::ZipExtraction(_, e) => Some(e),
            Eu4ErrorKind::IoErr(e) => Some(e),
            Eu4ErrorKind::Deserialize { ref err, .. } => Some(err),
            _ => None,
        }
    }
}

impl From<jomini::Error> for Eu4Error {
    fn from(err: jomini::Error) -> Self {
        Eu4Error::new(Eu4ErrorKind::Deserialize { part: None, err })
    }
}

impl From<IoError> for Eu4Error {
    fn from(err: IoError) -> Self {
        Eu4Error::new(Eu4ErrorKind::IoErr(err))
    }
}

impl From<Eu4ErrorKind> for Eu4Error {
    fn from(err: Eu4ErrorKind) -> Self {
        Eu4Error::new(err)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn size_of_error_test() {
        assert_eq!(std::mem::size_of::<Eu4Error>(), 8);
    }
}