Skip to main content

rooky_core/
errors.rs

1#[derive(Debug)]
2pub enum ChessError {
3    NotFound(&'static str),
4    InvalidPgn(std::io::Error),
5}
6impl std::error::Error for ChessError {
7    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
8        match self {
9            Self::InvalidPgn(err) => Some(err),
10            Self::NotFound(_) => None,
11        }
12    }
13    fn description(&self) -> &str {
14        match self {
15            Self::NotFound(msg) => msg,
16            Self::InvalidPgn(e) => Box::leak(format!("Invalid PGN: {e}").into_boxed_str()),
17        }
18    }
19    fn cause(&self) -> Option<&dyn std::error::Error> {
20        match self {
21            Self::InvalidPgn(ref err) => Some(err),
22            Self::NotFound(_) => None,
23        }
24    }
25}
26impl From<std::io::Error> for ChessError {
27    fn from(err: std::io::Error) -> Self {
28        Self::InvalidPgn(err)
29    }
30}
31impl std::fmt::Display for ChessError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Self::NotFound(msg) => write!(f, "Not found: {msg}"),
35            Self::InvalidPgn(err) => write!(f, "Invalid PGN: {err}"),
36        }
37    }
38}