Skip to main content

plotters_ternary/chart/
error.rs

1use std::error::Error as StdError;
2use std::fmt;
3
4use plotters::drawing::DrawingAreaErrorKind;
5
6use crate::{coord, series::SeriesError};
7
8/// An error produced while laying out or drawing a ternary chart.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum TernaryChartError<E: StdError + Send + Sync> {
12    /// Backend-independent geometry or viewport preparation failed.
13    Geometry(coord::Error),
14    /// Backend-independent series preparation failed.
15    Series(SeriesError),
16    /// Plotters could not complete a drawing operation.
17    Drawing(DrawingAreaErrorKind<E>),
18    /// The common major-grid step is unusable.
19    InvalidMajorStep { value: f64 },
20    /// A tick specification could not produce a meaningful sequence.
21    InvalidTickCount { count: usize },
22    /// A tick step is not finite, positive, or safely bounded.
23    InvalidTickStep { value: f64 },
24    /// An explicit tick value is not finite or lies outside unit composition space.
25    InvalidTickValue { value: f64 },
26    /// Caption and margins left no usable plotting rectangle.
27    InsufficientDrawingArea { width: u32, height: u32 },
28}
29
30impl<E: StdError + Send + Sync> fmt::Display for TernaryChartError<E> {
31    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Geometry(error) => write!(formatter, "ternary geometry error: {error}"),
34            Self::Series(error) => write!(formatter, "ternary series error: {error}"),
35            Self::Drawing(error) => write!(formatter, "Plotters drawing error: {error}"),
36            Self::InvalidMajorStep { value } => write!(
37                formatter,
38                "major grid step must be finite, in (0, 1], and produce at most 10,000 intervals: {value:?}"
39            ),
40            Self::InvalidTickCount { count } => {
41                write!(
42                    formatter,
43                    "tick count must be at least one interval; received {count}"
44                )
45            }
46            Self::InvalidTickStep { value } => write!(
47                formatter,
48                "tick step must be finite, in (0, 1], and produce at most 10,000 intervals: {value:?}"
49            ),
50            Self::InvalidTickValue { value } => write!(
51                formatter,
52                "tick value must be finite and within the unit composition range: {value:?}"
53            ),
54            Self::InsufficientDrawingArea { width, height } => write!(
55                formatter,
56                "caption and margins left an insufficient plotting area: {width}x{height}"
57            ),
58        }
59    }
60}
61
62impl<E: StdError + Send + Sync + 'static> StdError for TernaryChartError<E> {
63    fn source(&self) -> Option<&(dyn StdError + 'static)> {
64        match self {
65            Self::Geometry(error) => Some(error),
66            Self::Series(error) => Some(error),
67            Self::Drawing(error) => Some(error),
68            Self::InvalidMajorStep { .. }
69            | Self::InvalidTickCount { .. }
70            | Self::InvalidTickStep { .. }
71            | Self::InvalidTickValue { .. }
72            | Self::InsufficientDrawingArea { .. } => None,
73        }
74    }
75}
76
77impl<E: StdError + Send + Sync> From<coord::Error> for TernaryChartError<E> {
78    fn from(error: coord::Error) -> Self {
79        Self::Geometry(error)
80    }
81}
82
83impl<E: StdError + Send + Sync> From<SeriesError> for TernaryChartError<E> {
84    fn from(error: SeriesError) -> Self {
85        Self::Series(error)
86    }
87}
88
89impl<E: StdError + Send + Sync> From<DrawingAreaErrorKind<E>> for TernaryChartError<E> {
90    fn from(error: DrawingAreaErrorKind<E>) -> Self {
91        Self::Drawing(error)
92    }
93}