1use crate::errors::{OakError, OakErrorKind};
4
5use core::fmt::{Display, Formatter};
6use std::error::Error;
7
8impl Error for OakError {}
9
10impl Display for OakError {
11 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
12 Display::fmt(&self.kind, f)
13 }
14}
15
16impl Error for OakErrorKind {}
17
18impl Display for OakErrorKind {
19 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
20 match self {
21 OakErrorKind::IoError { error, source_id } => {
22 if let Some(id) = source_id {
23 write!(f, "I/O error at source {}: {}", id, error)
24 }
25 else {
26 write!(f, "I/O error: {}", error)
27 }
28 }
29 OakErrorKind::SyntaxError { message, offset, source_id } => {
30 if let Some(id) = source_id {
31 write!(f, "Syntax error at source {}:{}: {}", id, offset, message)
32 }
33 else {
34 write!(f, "Syntax error at {}: {}", offset, message)
35 }
36 }
37 OakErrorKind::UnexpectedCharacter { character, offset, source_id } => {
38 if let Some(id) = source_id {
39 write!(f, "Unexpected character '{}' at source {}:{}", character, id, offset)
40 }
41 else {
42 write!(f, "Unexpected character '{}' at {}", character, offset)
43 }
44 }
45 OakErrorKind::UnexpectedToken { token, offset, source_id } => {
46 if let Some(id) = source_id {
47 write!(f, "Unexpected token '{}' at source {}:{}", token, id, offset)
48 }
49 else {
50 write!(f, "Unexpected token '{}' at {}", token, offset)
51 }
52 }
53 OakErrorKind::UnexpectedEof { offset, source_id } => {
54 if let Some(id) = source_id {
55 write!(f, "Unexpected end of file at source {}:{}", id, offset)
56 }
57 else {
58 write!(f, "Unexpected end of file at {}", offset)
59 }
60 }
61 OakErrorKind::CustomError { message } => {
62 write!(f, "{}", message)
63 }
64 OakErrorKind::InvalidTheme { message } => {
65 write!(f, "Invalid theme: {}", message)
66 }
67 OakErrorKind::UnsupportedFormat { format } => {
68 write!(f, "Unsupported format: {}", format)
69 }
70 OakErrorKind::ColorParseError { color } => {
71 write!(f, "Color parsing error: {}", color)
72 }
73 OakErrorKind::FormatError { message } => {
74 write!(f, "Format error: {}", message)
75 }
76 OakErrorKind::SemanticError { message } => {
77 write!(f, "Semantic error: {}", message)
78 }
79 OakErrorKind::ProtocolError { message } => {
80 write!(f, "Protocol error: {}", message)
81 }
82 OakErrorKind::ExpectedToken { expected, offset, source_id } => {
83 if let Some(id) = source_id {
84 write!(f, "Expected '{}' at source {}:{}", expected, id, offset)
85 }
86 else {
87 write!(f, "Expected '{}' at {}", expected, offset)
88 }
89 }
90 OakErrorKind::ExpectedName { name_kind, offset, source_id } => {
91 if let Some(id) = source_id {
92 write!(f, "Expected {} at source {}:{}", name_kind, id, offset)
93 }
94 else {
95 write!(f, "Expected {} at {}", name_kind, offset)
96 }
97 }
98 OakErrorKind::TrailingCommaNotAllowed { offset, source_id } => {
99 if let Some(id) = source_id {
100 write!(f, "Trailing comma not allowed at source {}:{}", id, offset)
101 }
102 else {
103 write!(f, "Trailing comma not allowed at {}", offset)
104 }
105 }
106 OakErrorKind::TestFailure { path, expected, actual } => {
107 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)
108 }
109 OakErrorKind::TestRegenerated { path } => {
110 write!(f, "\x1b[33;1mREGEN\x1b[0m \x1b[36m{}\x1b[0m\n(Please verify and rerun)", path.display())
111 }
112 OakErrorKind::SerdeError { message } => {
113 write!(f, "Serde error: {}", message)
114 }
115 OakErrorKind::DeserializeError { message } => {
116 write!(f, "Deserialize error: {}", message)
117 }
118 OakErrorKind::XmlError { message } => {
119 write!(f, "XML error: {}", message)
120 }
121 OakErrorKind::ZipError { message } => {
122 write!(f, "Zip error: {}", message)
123 }
124 OakErrorKind::ParseError { message } => {
125 write!(f, "Parse error: {}", message)
126 }
127 OakErrorKind::InternalError { message } => {
128 write!(f, "Internal error: {}", message)
129 }
130 }
131 }
132}