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
use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum StarTrustError {
    #[error("IoError: {0}")]
    IoError(std::io::Error),
    #[error("GameStateError: {0}")]
    GameStateError(String),
    #[error("ParseFloatError: {0}")]
    ParseFloatError(std::num::ParseFloatError),
    #[error("ParseIntError: {0}")]
    ParseIntError(std::num::ParseIntError),
    #[error("TryFromPrimitiveError")]
    TryFromPrimitiveError(String),
    #[error("GeneralError: {0}")]
    GeneralError(String),
}

impl From<std::io::Error> for StarTrustError {
    fn from(value: std::io::Error) -> Self {
        StarTrustError::IoError(value)
    }
}

impl From<std::num::ParseFloatError> for StarTrustError {
    fn from(value: std::num::ParseFloatError) -> Self {
        StarTrustError::ParseFloatError(value)
    }
}

impl From<std::num::ParseIntError> for StarTrustError {
    fn from(value: std::num::ParseIntError) -> Self {
        StarTrustError::ParseIntError(value)
    }
}

impl<T: TryFromPrimitive> From<TryFromPrimitiveError<T>> for StarTrustError {
    fn from(value: TryFromPrimitiveError<T>) -> Self {
        StarTrustError::TryFromPrimitiveError(format!("{}", value))
    }
}

pub type StResult<T> = std::result::Result<T, StarTrustError>;

// impl <T> From<std::io::Result<T>> for StResult<T> {
//     fn from(value: std::io::Result<T>) -> StResult<T> {
//         value.map_err(|e| {
//             let e = e.into();
//             e
//         })
//     }
// }