From 640e8725c7dc0b5af3e139c783c1a1c08b4796b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isa=C3=AFe?= Date: Mon, 21 Oct 2024 20:56:21 +0200 Subject: [PATCH] feat: add `thiserror` for a real error impl (#38) --- Cargo.toml | 3 ++- integraal/Cargo.toml | 1 + integraal/src/structure/definitions.rs | 6 +++++- integraal/src/structure/implementations.rs | 4 ++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7522366..ddfc93c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ integraal = { version = "0.0.4", path = "./integraal" } integraal-examples = { version = "0.0.4", path = "./examples" } # external +num-traits = "0.2.19" rand = "0.9.0-alpha.2" rustversion = "1.0.15" -num-traits = "0.2.19" +thiserror = "1.0.64" diff --git a/integraal/Cargo.toml b/integraal/Cargo.toml index 1bb1299..73415da 100644 --- a/integraal/Cargo.toml +++ b/integraal/Cargo.toml @@ -24,6 +24,7 @@ romberg = [] # gated because it is not implemented for all input ki [dependencies] num-traits.workspace = true rand = { workspace = true, features = ["small_rng"], optional = true } +thiserror.workspace = true [build-dependencies] rustversion.workspace = true diff --git a/integraal/src/structure/definitions.rs b/integraal/src/structure/definitions.rs index 95d9cfc..ef46e04 100644 --- a/integraal/src/structure/definitions.rs +++ b/integraal/src/structure/definitions.rs @@ -7,15 +7,19 @@ use crate::{ComputeMethod, DomainDescriptor, FunctionDescriptor, Scalar}; // ------ CONTENT /// Integral error -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)] pub enum IntegraalError { /// Some parameters do not fit the requirements of the computation method. + #[error("{0}")] BadParameters(&'static str), /// Specified parameters are conflicting or ambiguous. + #[error("{0}")] InconsistentParameters(&'static str), /// One or more parameters are missing. + #[error("{0}")] MissingParameters(&'static str), /// A given method isn't implemented for the specified parameters (e.g. due to requirements). + #[error("{0}")] Unimplemented(&'static str), } diff --git a/integraal/src/structure/implementations.rs b/integraal/src/structure/implementations.rs index a425f75..7ac8972 100644 --- a/integraal/src/structure/implementations.rs +++ b/integraal/src/structure/implementations.rs @@ -42,8 +42,8 @@ impl<'a, X: Scalar> Integraal<'a, X> { /// - `Ok(X: Scalar)` -- The computation succeeded. /// - `Err(IntegraalError)` -- The computation failed for the reason specified by the enum. pub fn compute(&mut self) -> Result { - // ensure all data is defined - if self.domain.is_none() | self.function.is_none() | self.method.is_none() { + // ensure all data is defined; evaluate function first because it is reset after all computations + if self.function.is_none() | self.domain.is_none() | self.method.is_none() { return Err(IntegraalError::MissingParameters( "one or more parameter is missing", ));