Skip to main content

plotters_ternary/series/
error.rs

1use std::fmt;
2
3use crate::{ContourDisplayError, coord};
4
5use super::{AnnotationError, MarkerError, PolygonError};
6
7/// How invalid compositions affect a ternary series.
8#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
9pub enum InvalidPointPolicy {
10    /// Stop preparation and report the source point index.
11    #[default]
12    Error,
13    /// End the current line run, then continue with later valid points.
14    Break,
15}
16
17/// Backend-independent failures while preparing ternary series geometry.
18#[derive(Clone, Debug, PartialEq)]
19#[non_exhaustive]
20pub enum SeriesError {
21    /// A source composition could not be validated or projected.
22    InvalidPoint { index: usize, source: coord::Error },
23    /// A generated interpolation sample is materially invalid.
24    InvalidInterpolatedPoint { sample: usize, source: coord::Error },
25    /// `spline1d` could not evaluate a parameter inside the source domain.
26    SmoothInterpolationFailed { sample: usize, parameter: f64 },
27    /// The fixed fallback sampling density is zero or exceeds its guardrail.
28    InvalidSmoothSampling {
29        samples_per_interval: u32,
30        maximum: u32,
31    },
32    /// The requested curve would exceed the bounded fallback sample count.
33    TooManySmoothSamples { requested: usize, maximum: usize },
34    /// Marker size zero has no useful rendering semantics.
35    InvalidMarkerSize { size: u32 },
36    /// An ordered contour style collection contained no styles.
37    EmptyContourStyleCollection,
38    /// An ordered filled-contour style collection contained no styles.
39    EmptyContourBandStyleCollection,
40    /// Scalar-map micro-triangulation was zero or exceeded the guardrail.
41    InvalidScalarMapResolution { value: usize },
42    /// Scalar-map opacity was not a finite value in the inclusive zero-to-one range.
43    InvalidScalarMapOpacity { opacity: f64 },
44    /// A continuous contour colour range or stroke width was invalid.
45    InvalidContourColorRange {
46        minimum: f64,
47        maximum: f64,
48        stroke_width: u32,
49    },
50    /// An automatic contour legend stride was zero.
51    InvalidContourLegendStride { stride: usize },
52    /// Contour label or colour-bar rendering configuration was invalid.
53    ContourDisplay(ContourDisplayError),
54    /// Scalar-map field access failed despite an already validated field.
55    ScalarMapField(ternary_contours::FieldError),
56    /// Polygon validation, projection, or clipping preparation failed.
57    Polygon(PolygonError),
58    /// A ternary text annotation could not be prepared.
59    Annotation(AnnotationError),
60    /// A scientific marker configuration was invalid for an optional source
61    /// index. `None` denotes a legacy uniform marker before point preparation.
62    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}