Skip to main content

scala_chromatica/
error.rs

1//! Error types for colormap I/O operations
2
3use std::io;
4
5/// Error types for colormap operations
6#[derive(Debug)]
7pub enum ColorMapError {
8    /// I/O error (file read/write)
9    IoError(io::Error),
10    /// JSON parsing/serialization error
11    JsonError(serde_json::Error),
12    /// Colormap not found by name
13    NotFound(String),
14    /// Could not determine config directory
15    NoConfigDirectory,
16    /// Invalid hex color string
17    InvalidHexColor(String),
18}
19
20impl std::fmt::Display for ColorMapError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            ColorMapError::IoError(e) => write!(f, "I/O error: {}", e),
24            ColorMapError::JsonError(e) => write!(f, "JSON error: {}", e),
25            ColorMapError::NotFound(name) => write!(f, "ColorMap '{}' not found", name),
26            ColorMapError::NoConfigDirectory => write!(f, "Could not find config directory"),
27            ColorMapError::InvalidHexColor(hex) => write!(f, "Invalid hex color: '{}'", hex),
28        }
29    }
30}
31
32impl std::error::Error for ColorMapError {}
33
34impl From<io::Error> for ColorMapError {
35    fn from(err: io::Error) -> Self {
36        ColorMapError::IoError(err)
37    }
38}
39
40impl From<serde_json::Error> for ColorMapError {
41    fn from(err: serde_json::Error) -> Self {
42        ColorMapError::JsonError(err)
43    }
44}
45
46/// Result type for colormap operations
47pub type Result<T> = std::result::Result<T, ColorMapError>;