use optionstratlib_backtest::error::BacktestError;
use optionstratlib_core::error::DecimalError;
use optionstratlib_math::error::{CurveError, SurfaceError};
use rust_decimal::Decimal;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum GraphError {
#[error("Render error: {0}")]
Render(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
Curve(CurveError),
#[error(transparent)]
Surface(SurfaceError),
#[error(transparent)]
Decimal(Box<DecimalError>),
#[error(transparent)]
Backtest(Box<BacktestError>),
#[error("invalid dimensions {width}x{height}: both must be 1 to 16384 pixels")]
InvalidDimensions {
width: u32,
height: u32,
},
#[error(
"{width}x{height} is too small for this chart; it needs at least {min_width}x{min_height}"
)]
TooSmall {
width: u32,
height: u32,
min_width: u32,
min_height: u32,
},
#[error("{width}x{height} leaves no room for the chart under its title, legend or colour bar")]
NoRoom {
width: u32,
height: u32,
},
#[error("series {name:?} has line width {width}: expected a finite width in 0..=100 px")]
InvalidLineWidth {
name: String,
width: f64,
},
#[error("series {name:?} has {x} x values and {y} y values")]
LengthMismatch {
name: String,
x: usize,
y: usize,
},
#[error("surface {name:?} has {x} x, {y} y and {z} z values")]
SurfaceLengthMismatch {
name: String,
x: usize,
y: usize,
z: usize,
},
#[error("surface {name:?} has a {columns}x{rows} grid; at least 2x2 is needed")]
DegenerateSurface {
name: String,
columns: usize,
rows: usize,
},
#[error("surface {name:?} has more than one z value at ({x}, {y})")]
DuplicateSurfacePoint {
name: String,
x: Decimal,
y: Decimal,
},
#[error("surface {name:?} is an incomplete grid: {found} of {expected} points")]
IncompleteSurface {
name: String,
expected: usize,
found: usize,
},
#[error("value {0} cannot be represented as f64")]
NotRepresentable(Decimal),
#[error("unsupported colour {color:?}: {reason}")]
InvalidColor {
color: String,
reason: String,
},
#[error("the bundled font has no glyph for {character:?} (U+{code:04X}) in label {text:?}")]
MissingGlyph {
character: char,
code: u32,
text: String,
},
#[error("{character:?} (U+{code:04X}) cannot appear in XML; found in {text:?}")]
NotXmlText {
character: char,
code: u32,
text: String,
},
#[error("unsupported text: {0}")]
UnsupportedText(String),
}
impl From<BacktestError> for GraphError {
#[cold]
#[inline(never)]
fn from(err: BacktestError) -> Self {
GraphError::Backtest(Box::new(err))
}
}
impl From<DecimalError> for GraphError {
#[cold]
#[inline(never)]
fn from(err: DecimalError) -> Self {
GraphError::Decimal(Box::new(err))
}
}
impl From<CurveError> for GraphError {
#[cold]
#[inline(never)]
fn from(err: CurveError) -> Self {
GraphError::Curve(err)
}
}
impl From<SurfaceError> for GraphError {
#[cold]
#[inline(never)]
fn from(err: SurfaceError) -> Self {
GraphError::Surface(err)
}
}