use std::error::Error as StdError;
use std::fmt;
use plotters::drawing::DrawingAreaErrorKind;
use crate::{coord, series::SeriesError};
#[derive(Debug)]
#[non_exhaustive]
pub enum TernaryChartError<E: StdError + Send + Sync> {
Geometry(coord::Error),
Series(SeriesError),
Drawing(DrawingAreaErrorKind<E>),
InvalidMajorStep { value: f64 },
InvalidTickCount { count: usize },
InvalidTickStep { value: f64 },
InvalidTickValue { value: f64 },
InsufficientDrawingArea { width: u32, height: u32 },
}
impl<E: StdError + Send + Sync> fmt::Display for TernaryChartError<E> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Geometry(error) => write!(formatter, "ternary geometry error: {error}"),
Self::Series(error) => write!(formatter, "ternary series error: {error}"),
Self::Drawing(error) => write!(formatter, "Plotters drawing error: {error}"),
Self::InvalidMajorStep { value } => write!(
formatter,
"major grid step must be finite, in (0, 1], and produce at most 10,000 intervals: {value:?}"
),
Self::InvalidTickCount { count } => {
write!(
formatter,
"tick count must be at least one interval; received {count}"
)
}
Self::InvalidTickStep { value } => write!(
formatter,
"tick step must be finite, in (0, 1], and produce at most 10,000 intervals: {value:?}"
),
Self::InvalidTickValue { value } => write!(
formatter,
"tick value must be finite and within the unit composition range: {value:?}"
),
Self::InsufficientDrawingArea { width, height } => write!(
formatter,
"caption and margins left an insufficient plotting area: {width}x{height}"
),
}
}
}
impl<E: StdError + Send + Sync + 'static> StdError for TernaryChartError<E> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Geometry(error) => Some(error),
Self::Series(error) => Some(error),
Self::Drawing(error) => Some(error),
Self::InvalidMajorStep { .. }
| Self::InvalidTickCount { .. }
| Self::InvalidTickStep { .. }
| Self::InvalidTickValue { .. }
| Self::InsufficientDrawingArea { .. } => None,
}
}
}
impl<E: StdError + Send + Sync> From<coord::Error> for TernaryChartError<E> {
fn from(error: coord::Error) -> Self {
Self::Geometry(error)
}
}
impl<E: StdError + Send + Sync> From<SeriesError> for TernaryChartError<E> {
fn from(error: SeriesError) -> Self {
Self::Series(error)
}
}
impl<E: StdError + Send + Sync> From<DrawingAreaErrorKind<E>> for TernaryChartError<E> {
fn from(error: DrawingAreaErrorKind<E>) -> Self {
Self::Drawing(error)
}
}