Skip to main content

trueno_viz/
error.rs

1//! Error types for trueno-viz operations.
2
3use std::io;
4use thiserror::Error;
5
6/// Result type alias using [`Error`].
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors that can occur in trueno-viz operations.
10#[derive(Error, Debug)]
11pub enum Error {
12    /// I/O error (file operations, etc.).
13    #[error("I/O error: {0}")]
14    Io(#[from] io::Error),
15
16    /// PNG encoding error.
17    #[error("PNG encoding error: {0}")]
18    PngEncoding(#[from] png::EncodingError),
19
20    /// Invalid dimensions for framebuffer or plot.
21    #[error("Invalid dimensions: {width}x{height}")]
22    InvalidDimensions {
23        /// Width value.
24        width: u32,
25        /// Height value.
26        height: u32,
27    },
28
29    /// Data length mismatch between x and y arrays.
30    #[error("Data length mismatch: x has {x_len} elements, y has {y_len} elements")]
31    DataLengthMismatch {
32        /// Length of x data.
33        x_len: usize,
34        /// Length of y data.
35        y_len: usize,
36    },
37
38    /// Empty data provided where non-empty is required.
39    #[error("Empty data provided")]
40    EmptyData,
41
42    /// Scale domain error (e.g., log of non-positive value).
43    #[error("Scale domain error: {0}")]
44    ScaleDomain(String),
45
46    /// Color parsing error.
47    #[error("Invalid color: {0}")]
48    InvalidColor(String),
49
50    /// Rendering error.
51    #[error("Rendering error: {0}")]
52    Rendering(String),
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_error_display() {
61        let err = Error::InvalidDimensions { width: 0, height: 100 };
62        assert!(err.to_string().contains("Invalid dimensions"));
63    }
64
65    #[test]
66    fn test_data_length_mismatch() {
67        let err = Error::DataLengthMismatch { x_len: 10, y_len: 20 };
68        assert!(err.to_string().contains("10"));
69        assert!(err.to_string().contains("20"));
70    }
71}