thermite/
error.rs

1use std::{
2    error::Error,
3    io,
4    num::{ParseIntError, TryFromIntError},
5    path::{PathBuf, StripPrefixError},
6};
7
8use thiserror::Error;
9use ureq::http::header::ToStrError;
10
11pub type Result<T, E = ThermiteError> = std::result::Result<T, E>;
12
13#[derive(Error, Debug)]
14pub enum ThermiteError {
15    #[error("No such file {0:?}")]
16    MissingFile(Box<PathBuf>),
17    #[error(transparent)]
18    Io(#[from] io::Error),
19    #[error("{0}")]
20    Unknown(String),
21    #[error("Error making network request: {0}")]
22    Network(#[from] ureq::Error),
23    #[error(transparent)]
24    Zip(#[from] zip::result::ZipError),
25    #[error("Error parsing JSON: {0}")]
26    Json(Box<dyn Error + Send + Sync + 'static>),
27    #[error("Error resolving dependency {0}")]
28    Dep(String),
29    #[error("Error stripping directory prefix {0}\nIs the mod formatted correctly?")]
30    Prefix(#[from] StripPrefixError),
31    #[error("Sanity check failed: {0}")]
32    Sanity(Box<dyn Error + Send + Sync + 'static>),
33    #[error("Attempted to save a file but the path was None")]
34    MissingPath,
35    #[error("Error converting string to integer: {0}")]
36    ParseInt(#[from] ParseIntError),
37    #[error("Unable to convert integer: {0}")]
38    IntConversion(#[from] TryFromIntError),
39    #[error("Error parsing mod name: {0}")]
40    Name(String),
41    #[error("Expected string to be UTF8")]
42    UTF8,
43    #[error("Error converting header to string")]
44    ToStr(#[from] ToStrError),
45}
46
47// ureq::Error is ~240 bytes so we store it in a box
48// impl From<ureq::Error> for ThermiteError {
49//     fn from(value: ureq::Error) -> Self {
50//         Self::NetworkError(Box::new(value))
51//     }
52// }
53
54impl From<json5::Error> for ThermiteError {
55    fn from(value: json5::Error) -> Self {
56        Self::Json(value.into())
57    }
58}
59
60impl From<serde_json::Error> for ThermiteError {
61    fn from(value: serde_json::Error) -> Self {
62        Self::Json(value.into())
63    }
64}
65
66#[cfg(test)]
67mod test {
68    use ureq::Error;
69
70    use super::ThermiteError;
71
72    #[test]
73    fn from_ureq() {
74        let err = ureq::get("http://your_mother:8008")
75            .call()
76            .expect_err("How");
77
78        let thermite_err = ThermiteError::from(err);
79
80        if let ThermiteError::Network(u) = thermite_err {
81            assert!(matches!(u, Error::Io(_)), "u was {u:?}");
82        } else {
83            panic!("Unexpected error type: {:?}", thermite_err);
84        }
85    }
86}