Skip to main content

palette_core/
error.rs

1use std::sync::Arc;
2
3/// Errors produced when loading or parsing theme manifests.
4#[derive(Debug, thiserror::Error)]
5pub enum PaletteError {
6    /// TOML deserialization failed.
7    #[error("failed to parse manifest: {0}")]
8    Parse(#[from] toml::de::Error),
9
10    /// File read failed.
11    #[error("failed to read {path}: {source}")]
12    Io {
13        /// Path that could not be read.
14        path: Arc<str>,
15        /// Underlying I/O error.
16        source: std::io::Error,
17    },
18
19    /// Manifest has no `[base]` section.
20    #[error("manifest missing required [base] section")]
21    MissingBase,
22
23    /// Manifest has no `[meta]` section (required for registry).
24    #[error("manifest missing required [meta] section")]
25    MissingMeta,
26
27    /// A hex color string in the manifest is malformed.
28    #[error("invalid hex `{value}` in [{section}].{field}")]
29    InvalidHex {
30        /// TOML section containing the bad value.
31        section: Arc<str>,
32        /// Field name within the section.
33        field: Arc<str>,
34        /// The malformed hex string.
35        value: Arc<str>,
36    },
37
38    /// A style modifier string in the manifest is malformed.
39    #[error("invalid style `{value}` in [{section}].{field}")]
40    InvalidStyle {
41        /// TOML section containing the bad value.
42        section: Arc<str>,
43        /// Field name within the section.
44        field: Arc<str>,
45        /// The malformed style string.
46        value: Arc<str>,
47    },
48
49    /// A field key in the manifest is not recognized for its section.
50    #[error("unknown field `{field}` in [{section}]")]
51    UnknownField {
52        /// TOML section containing the unrecognized key.
53        section: Arc<str>,
54        /// The unrecognized field name.
55        field: Arc<str>,
56    },
57
58    /// No built-in or registered preset matches the given ID.
59    #[error("unknown preset: {0}")]
60    UnknownPreset(Arc<str>),
61
62    /// A gradient has fewer than 2 color stops.
63    #[error("gradient requires at least 2 stops, got {count}")]
64    InsufficientStops {
65        /// Number of stops provided.
66        count: usize,
67    },
68
69    /// Gradient stop positions are not monotonically increasing.
70    #[error("gradient stop positions are not sorted")]
71    UnsortedStops,
72
73    /// A gradient stop position is outside the normalized `[0, 1]` range.
74    #[error("gradient stop position must be in [0, 1], got {position}")]
75    InvalidGradientPosition {
76        /// The out-of-range position value.
77        position: f64,
78    },
79
80    /// A gradient mixes shorthand and explicit stop syntaxes.
81    #[error("gradient [{gradient}] mixes shorthand and explicit stop syntax")]
82    MixedGradientStopKinds {
83        /// Name of the gradient definition.
84        gradient: Arc<str>,
85    },
86
87    /// A gradient stop references a token path that does not exist.
88    #[error(
89        "invalid gradient reference in [{gradient}] stop {stop_index}: \"{reference}\" is not a known color field"
90    )]
91    InvalidGradientRef {
92        /// Name of the gradient definition.
93        gradient: Arc<str>,
94        /// Zero-based index of the offending stop.
95        stop_index: usize,
96        /// The raw `"section.field"` string that failed validation.
97        reference: Arc<str>,
98    },
99
100    /// A gradient specifies an unrecognized interpolation color space.
101    #[error("invalid color space \"{value}\" in [gradient.{gradient}]")]
102    InvalidColorSpace {
103        /// Name of the gradient definition.
104        gradient: Arc<str>,
105        /// The unrecognized color space string.
106        value: Arc<str>,
107    },
108}