1use crate::deflate::ZipInflationError;
2use zip::result::ZipError;
3
4#[derive(thiserror::Error, Debug)]
6#[error(transparent)]
7pub struct Ck3Error(#[from] Box<Ck3ErrorKind>);
8
9impl Ck3Error {
10 pub(crate) fn new(kind: Ck3ErrorKind) -> Ck3Error {
11 Ck3Error(Box::new(kind))
12 }
13
14 pub fn kind(&self) -> &Ck3ErrorKind {
16 &self.0
17 }
18}
19
20impl From<Ck3ErrorKind> for Ck3Error {
21 fn from(err: Ck3ErrorKind) -> Self {
22 Ck3Error::new(err)
23 }
24}
25
26#[derive(thiserror::Error, Debug)]
28pub enum Ck3ErrorKind {
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 #[error("expected the binary integer: {0} to be parsed as a date")]
57 InvalidDate(i32),
58}
59
60impl From<ZipInflationError> for Ck3ErrorKind {
61 fn from(x: ZipInflationError) -> Self {
62 match x {
63 ZipInflationError::BadData { msg } => Ck3ErrorKind::ZipBadData { msg },
64 ZipInflationError::EarlyEof { written } => Ck3ErrorKind::ZipEarlyEof { written },
65 }
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn size_of_error_test() {
75 assert_eq!(std::mem::size_of::<Ck3Error>(), 8);
76 }
77}