plotters_ternary/chart/
error.rs1use std::error::Error as StdError;
2use std::fmt;
3
4use plotters::drawing::DrawingAreaErrorKind;
5
6use crate::{coord, series::SeriesError};
7
8#[derive(Debug)]
10#[non_exhaustive]
11pub enum TernaryChartError<E: StdError + Send + Sync> {
12 Geometry(coord::Error),
14 Series(SeriesError),
16 Drawing(DrawingAreaErrorKind<E>),
18 InvalidMajorStep { value: f64 },
20 InvalidTickCount { count: usize },
22 InvalidTickStep { value: f64 },
24 InvalidTickValue { value: f64 },
26 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}