Improve error handling of Tolerance

This commit is contained in:
Hanno Braun 2022-04-12 17:06:30 +02:00
parent 518d51387d
commit 5f7ba6ae83

View File

@ -184,11 +184,13 @@ impl Tolerance {
/// Construct a `Tolerance` from a [`Scalar`] /// Construct a `Tolerance` from a [`Scalar`]
/// ///
/// Returns an error, if the passed scalar is not larger than zero. /// Returns an error, if the passed scalar is not larger than zero.
pub fn from_scalar(scalar: impl Into<Scalar>) -> Result<Self, Scalar> { pub fn from_scalar(
scalar: impl Into<Scalar>,
) -> Result<Self, InvalidTolerance> {
let scalar = scalar.into(); let scalar = scalar.into();
if scalar <= Scalar::ZERO { if scalar <= Scalar::ZERO {
return Err(scalar); return Err(InvalidTolerance(scalar));
} }
Ok(Self(scalar)) Ok(Self(scalar))
@ -209,6 +211,10 @@ where
} }
} }
#[derive(Debug, thiserror::Error)]
#[error("Invalid tolerance ({0}); must be above zero")]
pub struct InvalidTolerance(Scalar);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use fj_math::{Point, Scalar}; use fj_math::{Point, Scalar};