Skip to main content

egui_pop_tokens/
error.rs

1//! What can be wrong with a token file or a theme.
2
3use std::fmt;
4
5/// A problem in a token file or met while resolving a theme.
6///
7/// Reading a file returns one as an error. Resolving a theme never fails:
8/// it reports its problems in [`crate::ResolvedTokens::problems`] and
9/// falls back where it must.
10#[derive(Clone, Debug, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13    /// The file cannot be read from disk.
14    Io { file: String, message: String },
15    /// The file is not valid JSON.
16    Json { file: String, message: String },
17    /// The file is not valid YAML, or holds a value YAML misreads (an
18    /// unquoted `#colour` or `{alias}`).
19    Yaml { file: String, message: String },
20    /// The file is valid JSON but not a token file.
21    Malformed {
22        file: String,
23        path: String,
24        reason: String,
25    },
26    /// A theme named by a spec or by `$extends` is not known.
27    UnknownTheme { name: String },
28    /// Themes extend each other in a circle.
29    ExtendsCycle { chain: Vec<String> },
30    /// An alias names a token that does not exist.
31    UnknownAlias { path: String, target: String },
32    /// Aliases lead back to where they started.
33    AliasCycle { chain: Vec<String> },
34    /// A token has no `$type`, on itself, its groups or what it aliases.
35    MissingType { path: String },
36    /// A token has another type than expected (by its alias target, or by
37    /// the contract).
38    TypeMismatch {
39        path: String,
40        expected: String,
41        found: String,
42    },
43    /// A value that cannot be read as its type.
44    InvalidValue {
45        path: String,
46        ty: String,
47        reason: String,
48    },
49    /// A modifier in a token's `$extensions` that cannot apply (an alpha
50    /// outside 0 to 1, or on a token that is not a colour).
51    InvalidModifier { path: String, reason: String },
52    /// A token of the contract is not given by the theme.
53    Missing { path: String },
54    /// An override names a token the theme does not have.
55    UnknownOverride { path: String },
56    /// An accent names no usable colour scale.
57    UnknownAccent { name: String },
58    /// A component is based on a component no token names
59    /// (`<component>.based-on`): it keeps its own tokens only.
60    UnknownBase { path: String, base: String },
61    /// Components are based on each other in a circle: each keeps its own
62    /// tokens only.
63    BasedOnCycle { chain: Vec<String> },
64}
65
66impl Error {
67    /// The token the problem is about, if it is about one.
68    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 {}