1use std::fmt;
4
5#[derive(Clone, Debug, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13 Io { file: String, message: String },
15 Json { file: String, message: String },
17 Yaml { file: String, message: String },
20 Malformed {
22 file: String,
23 path: String,
24 reason: String,
25 },
26 UnknownTheme { name: String },
28 ExtendsCycle { chain: Vec<String> },
30 UnknownAlias { path: String, target: String },
32 AliasCycle { chain: Vec<String> },
34 MissingType { path: String },
36 TypeMismatch {
39 path: String,
40 expected: String,
41 found: String,
42 },
43 InvalidValue {
45 path: String,
46 ty: String,
47 reason: String,
48 },
49 InvalidModifier { path: String, reason: String },
52 Missing { path: String },
54 UnknownOverride { path: String },
56 UnknownAccent { name: String },
58 UnknownBase { path: String, base: String },
61 BasedOnCycle { chain: Vec<String> },
64}
65
66impl Error {
67 pub fn path(&self) -> Option<&str> {
69 match self {
70 Error::Malformed { path, .. }
71 | Error::UnknownAlias { path, .. }
72 | Error::MissingType { path }
73 | Error::TypeMismatch { path, .. }
74 | Error::InvalidValue { path, .. }
75 | Error::InvalidModifier { path, .. }
76 | Error::Missing { path }
77 | Error::UnknownOverride { path }
78 | Error::UnknownBase { path, .. } => Some(path),
79 Error::AliasCycle { chain } => chain.first().map(String::as_str),
80 _ => None,
81 }
82 }
83}
84
85impl fmt::Display for Error {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 match self {
88 Error::Io { file, message } => write!(f, "`{file}`: cannot be read: {message}"),
89 Error::Json { file, message } => write!(f, "`{file}`: not valid JSON: {message}"),
90 Error::Yaml { file, message } => write!(f, "`{file}`: not valid YAML: {message}"),
91 Error::Malformed { file, path, reason } => {
92 write!(f, "`{file}`: `{path}`: {reason}")
93 }
94 Error::UnknownTheme { name } => write!(f, "no theme named `{name}`"),
95 Error::ExtendsCycle { chain } => {
96 write!(
97 f,
98 "themes extend each other in a circle: {}",
99 chain.join(" → ")
100 )
101 }
102 Error::UnknownAlias { path, target } => {
103 write!(f, "`{path}` refers to `{{{target}}}`, which is not a token")
104 }
105 Error::AliasCycle { chain } => write!(f, "alias cycle: {}", chain.join(" → ")),
106 Error::MissingType { path } => write!(f, "`{path}` has no $type"),
107 Error::TypeMismatch {
108 path,
109 expected,
110 found,
111 } => write!(f, "`{path}` is a {found}, expected a {expected}"),
112 Error::InvalidValue { path, ty, reason } => {
113 write!(f, "`{path}` is not a valid {ty}: {reason}")
114 }
115 Error::InvalidModifier { path, reason } => write!(f, "`{path}`: {reason}"),
116 Error::Missing { path } => write!(f, "`{path}` is missing"),
117 Error::UnknownOverride { path } => {
118 write!(f, "override `{path}`: the theme has no such token")
119 }
120 Error::UnknownBase { path, base } => write!(
121 f,
122 "`{path}`: no token of a component `{base}`; its own tokens only are used"
123 ),
124 Error::BasedOnCycle { chain } => write!(
125 f,
126 "components are based on each other in a circle: {}; each keeps its own tokens only",
127 chain.join(" → ")
128 ),
129 Error::UnknownAccent { name } => write!(
130 f,
131 "accent `{name}`: no colour scale of that name with steps 3, 5, 8, 9, 10 and `contrast`"
132 ),
133 }
134 }
135}
136
137impl std::error::Error for Error {}