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
use crate::deflate::ZipInflationError;
use zip::result::ZipError;
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct Eu4Error(#[from] Box<Eu4ErrorKind>);
impl Eu4Error {
pub(crate) fn new(kind: Eu4ErrorKind) -> Eu4Error {
Eu4Error(Box::new(kind))
}
pub fn kind(&self) -> &Eu4ErrorKind {
&self.0
}
}
impl From<Eu4ErrorKind> for Eu4Error {
fn from(err: Eu4ErrorKind) -> Self {
Eu4Error::new(err)
}
}
#[derive(thiserror::Error, Debug)]
pub enum Eu4ErrorKind {
#[error("unable to parse as zip: {0}")]
ZipArchive(#[from] ZipError),
#[error("unable to inflate zip entry: {msg}")]
ZipBadData { msg: String },
#[error("early eof, only able to write {written} bytes")]
ZipEarlyEof { written: usize },
#[error("unknown header found in zip entry. Must be EU4txt or EU4bin")]
ZipHeader,
#[error("unknown header found in file. Must be EU4txt, EU4bin, or a zip file.")]
UnknownHeader,
#[error("unable to parse due to: {0}")]
Parse(#[source] jomini::Error),
#[error("unable to deserialize due to: {0}")]
Deserialize(#[source] jomini::Error),
#[error("error while writing output: {0}")]
Writer(#[source] jomini::Error),
#[error("unknown binary token encountered: {token_id:#x}")]
UnknownToken { token_id: u16 },
#[error("country tags must be 3 letters in length")]
CountryTagIncorrectSize,
#[error("country tags must contain only ascii letters")]
CountryTagInvalidCharacters,
#[error("expected the binary integer: {0} to be parsed as a date")]
InvalidDate(i32),
}
impl From<ZipInflationError> for Eu4ErrorKind {
fn from(x: ZipInflationError) -> Self {
match x {
ZipInflationError::BadData { msg } => Eu4ErrorKind::ZipBadData { msg },
ZipInflationError::EarlyEof { written } => Eu4ErrorKind::ZipEarlyEof { written },
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_error_test() {
assert_eq!(std::mem::size_of::<Eu4Error>(), 8);
}
}