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            Self::EmptyData => write!(f, "data series is empty"),
34            Self::UnknownColumn(name) => write!(f, "unknown column: {name}"),
35            Self::Io(err) => write!(f, "I/O error: {err}"),
36            Self::UnsupportedFormat(fmt_str) => write!(f, "unsupported format: {fmt_str}"),
37            Self::FontLoad(msg) => write!(f, "font loading error: {msg}"),
38        }
39    }
40}
41
42impl std::error::Error for PlotError {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        match self {
45            Self::Io(err) => Some(err),
46            _ => None,
47        }
48    }
49}
50
51impl From<std::io::Error> for PlotError {
52    fn from(err: std::io::Error) -> Self {
53        Self::Io(err)
54    }
55}
56
57/// A specialized Result type for plotkit operations.
58pub type Result<T> = std::result::Result<T, PlotError>;