1use std::fmt;
5
6#[derive(Debug)]
8#[non_exhaustive]
9pub enum ChartError {
10 EmptyData,
12
13 LengthMismatch {
15 expected: usize,
17 got: usize,
19 },
20
21 InvalidData {
23 layer: usize,
25 detail: String,
27 },
28
29 DimensionMismatch {
31 layer: usize,
33 x_len: usize,
35 y_len: usize,
37 },
38
39 InvalidParameter(String),
41
42 Gfx(esoc_gfx::error::GfxError),
44
45 Io(std::io::Error),
47}
48
49impl fmt::Display for ChartError {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 match self {
52 Self::EmptyData => f.write_str("no data provided"),
53 Self::LengthMismatch { expected, got } => {
54 write!(f, "length mismatch: expected {expected}, got {got}")
55 }
56 Self::InvalidData { layer, detail } => {
57 write!(f, "layer {layer}: {detail}")
58 }
59 Self::DimensionMismatch {
60 layer,
61 x_len,
62 y_len,
63 } => {
64 write!(f, "layer {layer}: x has {x_len} elements but y has {y_len}")
65 }
66 Self::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
67 Self::Gfx(err) => write!(f, "graphics error: {err}"),
68 Self::Io(err) => write!(f, "I/O error: {err}"),
69 }
70 }
71}
72
73impl std::error::Error for ChartError {
74 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75 match self {
76 Self::Gfx(err) => Some(err),
77 Self::Io(err) => Some(err),
78 _ => None,
79 }
80 }
81}
82
83impl From<esoc_gfx::error::GfxError> for ChartError {
84 fn from(err: esoc_gfx::error::GfxError) -> Self {
85 Self::Gfx(err)
86 }
87}
88
89impl From<std::io::Error> for ChartError {
90 fn from(err: std::io::Error) -> Self {
91 Self::Io(err)
92 }
93}
94
95pub type Result<T> = std::result::Result<T, ChartError>;