1use std::fmt;
2
3use crate::{ContourDisplayError, coord};
4
5use super::{AnnotationError, MarkerError, PolygonError};
6
7#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
9pub enum InvalidPointPolicy {
10 #[default]
12 Error,
13 Break,
15}
16
17#[derive(Clone, Debug, PartialEq)]
19#[non_exhaustive]
20pub enum SeriesError {
21 InvalidPoint { index: usize, source: coord::Error },
23 InvalidInterpolatedPoint { sample: usize, source: coord::Error },
25 SmoothInterpolationFailed { sample: usize, parameter: f64 },
27 InvalidSmoothSampling {
29 samples_per_interval: u32,
30 maximum: u32,
31 },
32 TooManySmoothSamples { requested: usize, maximum: usize },
34 InvalidMarkerSize { size: u32 },
36 EmptyContourStyleCollection,
38 EmptyContourBandStyleCollection,
40 InvalidScalarMapResolution { value: usize },
42 InvalidScalarMapOpacity { opacity: f64 },
44 InvalidContourColorRange {
46 minimum: f64,
47 maximum: f64,
48 stroke_width: u32,
49 },
50 InvalidContourLegendStride { stride: usize },
52 ContourDisplay(ContourDisplayError),
54 ScalarMapField(ternary_contours::FieldError),
56 Polygon(PolygonError),
58 Annotation(AnnotationError),
60 Marker {
63 index: Option<usize>,
64 source: MarkerError,
65 },
66}
67
68impl fmt::Display for SeriesError {
69 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 Self::InvalidPoint { index, source } => write!(
72 formatter,
73 "invalid ternary series point at index {index}: {source}"
74 ),
75 Self::InvalidInterpolatedPoint { sample, source } => write!(
76 formatter,
77 "invalid generated ternary interpolation sample {sample}: {source}"
78 ),
79 Self::SmoothInterpolationFailed { sample, parameter } => write!(
80 formatter,
81 "spline1d could not evaluate sample {sample} at parameter {parameter:?}"
82 ),
83 Self::InvalidSmoothSampling {
84 samples_per_interval,
85 maximum,
86 } => write!(
87 formatter,
88 "smooth samples per interval must be in 1..={maximum}; received {samples_per_interval}"
89 ),
90 Self::TooManySmoothSamples { requested, maximum } => write!(
91 formatter,
92 "smooth curve requires {requested} samples; configured maximum is {maximum}"
93 ),
94 Self::InvalidMarkerSize { size } => {
95 write!(formatter, "marker size must be greater than zero: {size}")
96 }
97 Self::InvalidScalarMapOpacity { opacity } => {
98 write!(
99 formatter,
100 "scalar-map opacity must be finite and in 0..=1: {opacity:?}"
101 )
102 }
103 Self::EmptyContourBandStyleCollection => {
104 write!(
105 formatter,
106 "filled contour style collection must not be empty"
107 )
108 }
109 Self::InvalidScalarMapResolution { value } => write!(
110 formatter,
111 "scalar-map subdivisions per edge must be in 1..=64, or adaptive depth in 0..=6; received {value}"
112 ),
113 Self::EmptyContourStyleCollection => {
114 write!(formatter, "contour style collection must not be empty")
115 }
116 Self::InvalidContourColorRange {
117 minimum,
118 maximum,
119 stroke_width,
120 } => write!(
121 formatter,
122 "contour colour range must be finite and increasing with nonzero stroke width: {minimum:?}..{maximum:?}, width {stroke_width}"
123 ),
124 Self::InvalidContourLegendStride { stride } => write!(
125 formatter,
126 "contour legend stride must be greater than zero: {stride}"
127 ),
128 Self::ContourDisplay(source) => write!(formatter, "contour display error: {source}"),
129 Self::ScalarMapField(source) => write!(formatter, "scalar-map field error: {source}"),
130 Self::Polygon(source) => write!(formatter, "ternary polygon error: {source}"),
131 Self::Annotation(source) => write!(formatter, "ternary annotation error: {source}"),
132 Self::Marker {
133 index: Some(index),
134 source,
135 } => {
136 write!(
137 formatter,
138 "invalid marker for ternary series point at index {index}: {source}"
139 )
140 }
141 Self::Marker {
142 index: None,
143 source,
144 } => write!(formatter, "invalid marker style: {source}"),
145 }
146 }
147}
148
149impl std::error::Error for SeriesError {
150 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
151 match self {
152 Self::InvalidPoint { source, .. } | Self::InvalidInterpolatedPoint { source, .. } => {
153 Some(source)
154 }
155 Self::SmoothInterpolationFailed { .. }
156 | Self::InvalidSmoothSampling { .. }
157 | Self::TooManySmoothSamples { .. }
158 | Self::InvalidMarkerSize { .. }
159 | Self::EmptyContourStyleCollection
160 | Self::EmptyContourBandStyleCollection
161 | Self::InvalidScalarMapResolution { .. }
162 | Self::InvalidScalarMapOpacity { .. }
163 | Self::InvalidContourColorRange { .. }
164 | Self::InvalidContourLegendStride { .. } => None,
165 Self::Marker { source, .. } => Some(source),
166 Self::ContourDisplay(source) => Some(source),
167 Self::ScalarMapField(source) => Some(source),
168 Self::Polygon(source) => Some(source),
169 Self::Annotation(source) => Some(source),
170 }
171 }
172}