oak_core/errors/
display.rs1use crate::errors::{OakError, OakErrorKind};
4
5use core::fmt::{Display, Formatter};
6use std::error::Error;
7
8impl Error for OakError {
9    fn source(&self) -> Option<&(dyn Error + 'static)> {
10        self.kind.source()
11    }
12}
13
14impl Display for OakError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
16        Display::fmt(&self.kind, f)
17    }
18}
19
20impl Error for OakErrorKind {
21    fn source(&self) -> Option<&(dyn Error + 'static)> {
22        match self {
23            OakErrorKind::IoError { error, .. } => Some(error),
24            _ => None,
25        }
26    }
27}
28
29impl Display for OakErrorKind {
30    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
31        match self {
32            OakErrorKind::IoError { error, url } => {
33                if let Some(url) = url {
34                    write!(f, "I/O error at {}: {}", url, error)
35                }
36                else {
37                    write!(f, "I/O error: {}", error)
38                }
39            }
40            OakErrorKind::SyntaxError { message, source } => {
41                write!(f, "Syntax error at {:?}: {}", source, message)
42            }
43            OakErrorKind::UnexpectedCharacter { character, source } => {
44                write!(f, "Unexpected character '{}' at {:?}", character, source)
45            }
46            OakErrorKind::CustomError { message } => {
47                write!(f, "{}", message)
48            }
49        }
50    }
51}