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
use std::{fmt, io};
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("The {0} could not be found.")]
NotFound(ErrorValue),
#[error("The {0} is invalid: {1}.")]
Invalid(ErrorValue, String),
#[error("An error occured while working with the provided resource: {0}")]
Resource(Box<dyn std::error::Error + Send + Sync>),
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Resource(error.into())
}
}
impl From<binrw::Error> for Error {
fn from(error: binrw::Error) -> Self {
Error::Resource(error.into())
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorValue {
Path(String),
#[cfg(feature = "excel")]
Sheet(String),
#[cfg(feature = "exd")]
Row {
row: u32,
subrow: u16,
sheet: Option<String>,
},
#[cfg(feature = "sqpack")]
File(Vec<u8>),
Other(String),
}
impl fmt::Display for ErrorValue {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Path(path) => write!(formatter, "path {path:?}"),
#[cfg(feature = "excel")]
Self::Sheet(sheet) => write!(formatter, "Excel sheet {sheet:?}"),
#[cfg(feature = "exd")]
Self::Row { row, subrow, sheet } => write!(
formatter,
"Excel row {}/{row}:{subrow}",
sheet.as_deref().unwrap_or("(none)"),
),
#[cfg(feature = "sqpack")]
Self::File(file) => write!(formatter, "SqPack file ({} bytes)", file.len()),
Self::Other(value) => write!(formatter, "{value}"),
}
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;