imperator_save/
errors.rs

1use crate::deflate::ZipInflationError;
2use zip::result::ZipError;
3
4/// A Imperator Error
5#[derive(thiserror::Error, Debug)]
6#[error(transparent)]
7pub struct ImperatorError(#[from] Box<ImperatorErrorKind>);
8
9impl ImperatorError {
10    pub(crate) fn new(kind: ImperatorErrorKind) -> ImperatorError {
11        ImperatorError(Box::new(kind))
12    }
13
14    /// Return the specific type of error
15    pub fn kind(&self) -> &ImperatorErrorKind {
16        &self.0
17    }
18}
19
20impl From<ImperatorErrorKind> for ImperatorError {
21    fn from(err: ImperatorErrorKind) -> Self {
22        ImperatorError::new(err)
23    }
24}
25
26/// Specific type of error
27#[derive(thiserror::Error, Debug)]
28pub enum ImperatorErrorKind {
29    #[error("unable to parse as zip: {0}")]
30    ZipArchive(#[from] ZipError),
31
32    #[error("missing gamestate entry in zip")]
33    ZipMissingEntry,
34
35    #[error("unable to inflate zip entry: {msg}")]
36    ZipBadData { msg: String },
37
38    #[error("early eof, only able to write {written} bytes")]
39    ZipEarlyEof { written: usize },
40
41    #[error("unable to parse due to: {0}")]
42    Parse(#[source] jomini::Error),
43
44    #[error("unable to deserialize due to: {0}")]
45    Deserialize(#[source] jomini::Error),
46
47    #[error("error while writing output: {0}")]
48    Writer(#[source] jomini::Error),
49
50    #[error("unknown binary token encountered: {token_id:#x}")]
51    UnknownToken { token_id: u16 },
52
53    #[error("invalid header")]
54    InvalidHeader,
55}
56
57impl From<ZipInflationError> for ImperatorErrorKind {
58    fn from(x: ZipInflationError) -> Self {
59        match x {
60            ZipInflationError::BadData { msg } => ImperatorErrorKind::ZipBadData { msg },
61            ZipInflationError::EarlyEof { written } => ImperatorErrorKind::ZipEarlyEof { written },
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn size_of_error_test() {
72        assert_eq!(std::mem::size_of::<ImperatorError>(), 8);
73    }
74}