#[allow(unreachable_pub)]
pub use crate::NotSorted;
#[allow(unreachable_pub)]
pub use crate::builder::{TooFewElements, TooFewKnots, TooSmallWorkspace};
use core::{convert::From, fmt};
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum BSplineError {
TooFewElements(TooFewElements),
TooFewKnots(TooFewKnots),
TooSmallWorkspace(TooSmallWorkspace),
InvalidDegree(InvalidDegree),
NotSorted(NotSorted),
IncongruousElementsKnots(IncongruousElementsKnots),
IncongruousElementsDegree(IncongruousElementsDegree),
}
impl fmt::Display for BSplineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BSplineError::TooFewElements(inner) => inner.fmt(f),
BSplineError::NotSorted(inner) => inner.fmt(f),
BSplineError::InvalidDegree(inner) => inner.fmt(f),
BSplineError::TooSmallWorkspace(inner) => inner.fmt(f),
BSplineError::TooFewKnots(inner) => inner.fmt(f),
BSplineError::IncongruousElementsKnots(inner) => inner.fmt(f),
BSplineError::IncongruousElementsDegree(inner) => inner.fmt(f),
}
}
}
impl From<TooFewElements> for BSplineError {
fn from(from: TooFewElements) -> Self {
BSplineError::TooFewElements(from)
}
}
impl From<TooFewKnots> for BSplineError {
fn from(from: TooFewKnots) -> Self {
BSplineError::TooFewKnots(from)
}
}
impl From<InvalidDegree> for BSplineError {
fn from(from: InvalidDegree) -> Self {
BSplineError::InvalidDegree(from)
}
}
impl From<NotSorted> for BSplineError {
fn from(from: NotSorted) -> Self {
BSplineError::NotSorted(from)
}
}
impl From<TooSmallWorkspace> for BSplineError {
fn from(from: TooSmallWorkspace) -> Self {
BSplineError::TooSmallWorkspace(from)
}
}
impl From<IncongruousElementsKnots> for BSplineError {
fn from(from: IncongruousElementsKnots) -> Self {
BSplineError::IncongruousElementsKnots(from)
}
}
impl From<IncongruousElementsDegree> for BSplineError {
fn from(from: IncongruousElementsDegree) -> Self {
BSplineError::IncongruousElementsDegree(from)
}
}
#[cfg(feature = "std")]
impl Error for BSplineError {}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct InvalidDegree {
degree: usize,
}
impl InvalidDegree {
pub fn new(degree: usize) -> Self {
InvalidDegree { degree }
}
}
impl fmt::Display for InvalidDegree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"The degree of the resulting curve is {} and such not valid.
Only strictly positive degrees less than the number of elements are allowed.",
self.degree
)
}
}
#[cfg(feature = "std")]
impl Error for InvalidDegree {}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum BSplineBuildMode {
Open,
Clamped,
Legacy,
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct IncongruousElementsKnots {
elements: usize,
knots: usize,
mode: BSplineBuildMode,
}
impl IncongruousElementsKnots {
pub fn open(elements: usize, knots: usize) -> Self {
IncongruousElementsKnots {
elements,
knots,
mode: BSplineBuildMode::Open,
}
}
pub fn clamped(elements: usize, knots: usize) -> Self {
IncongruousElementsKnots {
elements,
knots,
mode: BSplineBuildMode::Clamped,
}
}
pub fn legacy(elements: usize, knots: usize) -> Self {
IncongruousElementsKnots {
elements,
knots,
mode: BSplineBuildMode::Legacy,
}
}
}
impl fmt::Display for IncongruousElementsKnots {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.mode {
BSplineBuildMode::Open => {
write!(
f,
"Found {} elements (#e) and {} knots (#k), but for an open bspline
#e <= #k <= 2*(#e-1) must hold.",
self.elements, self.knots
)
}
BSplineBuildMode::Clamped => {
write!(
f,
"Found {} elements and {} knots, but for a clamped bspline there
must be at least as many elements as there are knots.",
self.elements, self.knots
)
}
BSplineBuildMode::Legacy => {
write!(
f,
"Found {} elements (#e) and {} knots (#k), but for a legacy bspline
#e+2 <= #k <= 2*(#e+1) must hold.",
self.elements, self.knots
)
}
}
}
}
#[cfg(feature = "std")]
impl Error for IncongruousElementsKnots {}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct IncongruousElementsDegree {
elements: usize,
degree: usize,
mode: BSplineBuildMode,
}
impl IncongruousElementsDegree {
pub fn open(elements: usize, degree: usize) -> Self {
IncongruousElementsDegree {
elements,
degree,
mode: BSplineBuildMode::Open,
}
}
pub fn clamped(elements: usize, degree: usize) -> Self {
IncongruousElementsDegree {
elements,
degree,
mode: BSplineBuildMode::Clamped,
}
}
pub fn legacy(elements: usize, degree: usize) -> Self {
IncongruousElementsDegree {
elements,
degree,
mode: BSplineBuildMode::Legacy,
}
}
}
impl fmt::Display for IncongruousElementsDegree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.mode {
BSplineBuildMode::Open => {
write!(
f,
"Found {} elements and degree of {}, but for an open bspline
there must be more elements than the degree of the spline.",
self.elements, self.degree
)
}
BSplineBuildMode::Clamped => {
write!(
f,
"Found {} elements and a degree of {}.
However, the degree of a clamped bspline
must be less than the number of elements.",
self.elements, self.degree
)
}
BSplineBuildMode::Legacy => {
write!(
f,
"Found {} elements and degree of {}, but for a legacy bspline
there must be more elements than the degree of the spline.",
self.elements, self.degree
)
}
}
}
}
#[cfg(feature = "std")]
impl Error for IncongruousElementsDegree {}