Skip to content

Commit

Permalink
feat: add thiserror for a real error impl
Browse files Browse the repository at this point in the history
  • Loading branch information
imrn99 committed Oct 21, 2024
1 parent f6cd666 commit 9943b8d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions integraal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion integraal/src/structure/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
4 changes: 2 additions & 2 deletions integraal/src/structure/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl<'a, X: Scalar> Integraal<'a, X> {
/// - `Ok(X: Scalar)` -- The computation was successfuly done
/// - `Err(IntegraalError)` -- The computation failed for the reason specified by the enum.
pub fn compute(&mut self) -> Result<X, IntegraalError> {
// 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",
));
Expand Down

0 comments on commit 9943b8d

Please sign in to comment.