#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(feature = "std")]
use std::string::String;
use core::fmt::{Display, Formatter, Result};
#[derive(Debug, Clone, PartialEq)]
pub enum LoessError {
EmptyInput,
InvalidInput(String),
MismatchedInputs {
x_len: usize,
y_len: usize,
},
InvalidNumericValue(String),
TooFewPoints {
got: usize,
min: usize,
},
InvalidFraction(f64),
InvalidIterations(usize),
InvalidIntervals(f64),
InvalidTolerance(f64),
InvalidChunkSize {
got: usize,
min: usize,
},
InvalidOverlap {
overlap: usize,
chunk_size: usize,
},
InvalidWindowCapacity {
got: usize,
min: usize,
},
InvalidMinPoints {
got: usize,
window_capacity: usize,
},
UnsupportedFeature {
adapter: &'static str,
feature: &'static str,
},
DuplicateParameter {
parameter: &'static str,
},
InvalidCell(f64),
InsufficientVertices {
required: usize,
limit: usize,
cell: f64,
cell_provided: bool,
limit_provided: bool,
},
}
impl Display for LoessError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Self::EmptyInput => write!(f, "Input arrays are empty"),
Self::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
Self::MismatchedInputs { x_len, y_len } => {
write!(f, "Length mismatch: x has {x_len} points, y has {y_len}")
}
Self::InvalidNumericValue(s) => write!(f, "Invalid numeric value: {s}"),
Self::TooFewPoints { got, min } => {
write!(f, "Too few points: got {got}, need at least {min}")
}
Self::InvalidFraction(frac) => {
write!(f, "Invalid fraction: {frac} (must be > 0 and <= 1)")
}
Self::InvalidIterations(iter) => {
write!(f, "Invalid iterations: {iter} (must be in [0, 1000])")
}
Self::InvalidIntervals(level) => {
write!(f, "Invalid interval level: {level} (must be > 0 and < 1)")
}
Self::InvalidTolerance(tol) => {
write!(f, "Invalid tolerance: {tol} (must be > 0 and finite)")
}
Self::InvalidChunkSize { got, min } => {
write!(f, "Invalid chunk_size: {got} (must be at least {min})")
}
Self::InvalidOverlap {
overlap,
chunk_size,
} => {
write!(
f,
"Invalid overlap: {overlap} (must be less than chunk_size {chunk_size})"
)
}
Self::InvalidWindowCapacity { got, min } => {
write!(f, "Invalid window_capacity: {got} (must be at least {min})")
}
Self::InvalidMinPoints {
got,
window_capacity,
} => {
write!(
f,
"Invalid min_points: {got} (must be between 2 and window_capacity {window_capacity})"
)
}
Self::UnsupportedFeature { adapter, feature } => {
write!(f, "Adapter '{adapter}' does not support feature: {feature}")
}
Self::DuplicateParameter { parameter } => {
write!(
f,
"Parameter '{parameter}' was set multiple times. Each parameter can only be configured once."
)
}
Self::InvalidCell(cell) => {
write!(f, "Invalid cell size: {cell} (must be in range (0, 1])")
}
Self::InsufficientVertices {
required,
limit,
cell,
cell_provided,
limit_provided,
} => {
let cell_desc = if *cell_provided {
format!("user-provided cell size {cell}")
} else {
format!("default cell size {cell}")
};
let limit_desc = if *limit_provided {
format!("user-provided limit {limit}")
} else {
format!("default limit (N = {limit})")
};
if !*cell_provided && *limit_provided {
write!(
f,
"Insufficient vertices: {cell_desc} does not work with {limit_desc}. Try passing a larger cell size manually."
)
} else {
write!(
f,
"Insufficient vertices: {cell_desc} requires ~{required} vertices, but {limit_desc} is too small"
)
}
}
}
}
}
#[cfg(feature = "std")]
impl Error for LoessError {}