oak_core/errors/
display.rs

1//! Display implementations for error types.
2
3use 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, offset, url } => {
41                if let Some(url) = url {
42                    write!(f, "Syntax error at {}:{}: {}", url, offset, message)
43                }
44                else {
45                    write!(f, "Syntax error at {}: {}", offset, message)
46                }
47            }
48            OakErrorKind::UnexpectedCharacter { character, offset, url } => {
49                if let Some(url) = url {
50                    write!(f, "Unexpected character '{}' at {}:{}", character, url, offset)
51                }
52                else {
53                    write!(f, "Unexpected character '{}' at {}", character, offset)
54                }
55            }
56            OakErrorKind::UnexpectedToken { token, offset, url } => {
57                if let Some(url) = url {
58                    write!(f, "Unexpected token '{}' at {}:{}", token, url, offset)
59                }
60                else {
61                    write!(f, "Unexpected token '{}' at {}", token, offset)
62                }
63            }
64            OakErrorKind::UnexpectedEof { offset, url } => {
65                if let Some(url) = url {
66                    write!(f, "Unexpected end of file at {}:{}", url, offset)
67                }
68                else {
69                    write!(f, "Unexpected end of file at {}", offset)
70                }
71            }
72            OakErrorKind::CustomError { message } => {
73                write!(f, "{}", message)
74            }
75            OakErrorKind::InvalidTheme { message } => {
76                write!(f, "Invalid theme: {}", message)
77            }
78            OakErrorKind::UnsupportedFormat { format } => {
79                write!(f, "Unsupported format: {}", format)
80            }
81            OakErrorKind::ColorParseError { color } => {
82                write!(f, "Color parsing error: {}", color)
83            }
84            OakErrorKind::FormatError { message } => {
85                write!(f, "Format error: {}", message)
86            }
87            OakErrorKind::SemanticError { message } => {
88                write!(f, "Semantic error: {}", message)
89            }
90            OakErrorKind::ProtocolError { message } => {
91                write!(f, "Protocol error: {}", message)
92            }
93            OakErrorKind::ExpectedToken { expected, offset, .. } => {
94                write!(f, "Expected '{}' at {}", expected, offset)
95            }
96            OakErrorKind::ExpectedName { name_kind, offset, .. } => {
97                write!(f, "Expected {} at {}", name_kind, offset)
98            }
99            OakErrorKind::TrailingCommaNotAllowed { offset, .. } => {
100                write!(f, "Trailing comma not allowed at {}", offset)
101            }
102            OakErrorKind::TestFailure { path, expected, actual } => {
103                write!(f, "\x1b[31;1mFAIL\x1b[0m \x1b[36m{}\x1b[0m\n\x1b[32m- Exp:\x1b[0m {}\n\x1b[31m- Act:\x1b[0m {}", path.display(), expected, actual)
104            }
105            OakErrorKind::TestRegenerated { path } => {
106                write!(f, "\x1b[33;1mREGEN\x1b[0m \x1b[36m{}\x1b[0m\n(Please verify and rerun)", path.display())
107            }
108        }
109    }
110}