Skip to main content

plotkit_core/
error.rs

1//! Error types for plotkit.
2
3use std::fmt;
4
5/// Errors that can occur in plotkit operations.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum PlotError {
9    /// X and Y series have different lengths.
10    SeriesLengthMismatch {
11        /// Expected length (from the first series).
12        expected: usize,
13        /// Actual length of the mismatched series.
14        got: usize,
15    },
16    /// Data series is empty.
17    EmptyData,
18    /// Referenced column not found (for DataFrame integrations).
19    UnknownColumn(String),
20    /// An I/O error occurred.
21    Io(std::io::Error),
22    /// The requested output format is not supported.
23    UnsupportedFormat(String),
24    /// Failed to load or render with a font.
25    FontLoad(String),
26}
27
28impl fmt::Display for PlotError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Self::SeriesLengthMismatch { expected, got } => {
32                write!(f, "series length mismatch: expected {expected}, got {got}")
33            }
34            Self::EmptyData => write!(f, "data series is empty"),
35            Self::UnknownColumn(name) => write!(f, "unknown column: {name}"),
36            Self::Io(err) => write!(f, "I/O error: {err}"),
37            Self::UnsupportedFormat(fmt_str) => write!(f, "unsupported format: {fmt_str}"),
38            Self::FontLoad(msg) => write!(f, "font loading error: {msg}"),
39        }
40    }
41}
42
43impl std::error::Error for PlotError {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            Self::Io(err) => Some(err),
47            _ => None,
48        }
49    }
50}
51
52impl From<std::io::Error> for PlotError {
53    fn from(err: std::io::Error) -> Self {
54        Self::Io(err)
55    }
56}
57
58/// A specialized Result type for plotkit operations.
59pub type Result<T> = std::result::Result<T, PlotError>;