From 5272b525b24938a24290c93c521a725e263734f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isa=C3=AFe?= Date: Wed, 8 May 2024 07:44:40 +0200 Subject: [PATCH] refactor: reorganize internal structure module (#5) * split structure module into multiple files * add dedicated test file * update doc --- src/structure/definitions.rs | 63 +++++++++++++++++ .../implementations.rs} | 69 ++----------------- src/structure/mod.rs | 15 ++++ src/structure/tests.rs | 4 ++ 4 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 src/structure/definitions.rs rename src/{structure.rs => structure/implementations.rs} (75%) create mode 100644 src/structure/mod.rs create mode 100644 src/structure/tests.rs diff --git a/src/structure/definitions.rs b/src/structure/definitions.rs new file mode 100644 index 0000000..64be02a --- /dev/null +++ b/src/structure/definitions.rs @@ -0,0 +1,63 @@ +//! main structure definitions + +// ------ IMPORTS + +use crate::{ComputeMethod, DomainDescriptor, FunctionDescriptor}; + +// ------ CONTENT + +/// Integral error +#[derive(Debug)] +pub enum IntegraalError { + /// One or more parameters are missing. + MissingParameters(&'static str), + /// Specified parameters are conflicting or ambiguous. + InconsistentParameters(&'static str), +} + +/// Main integral computation structure +/// +/// This structure is used as the entrypoint for integral definition and computation. It follows +/// a pseudo-builder patterns where the function description is reset after a computation. +/// +/// # Usage +/// +/// ## Components +/// +/// The structure is made up of three components that are used to describe the integral the user +/// wishes to compute: +/// - a [`DomainDescriptor`] instance, used to describe the space over which the integral span +/// - a [`FunctionDescriptor`] instance, used to describe the integrated function +/// - a [`ComputeMethod`] instance, used to choose which numerical integration method will be used +/// for computation +/// +/// In the future, another object might be included to control the execution backend. +/// +/// ## Example +/// +/// ```rust +/// # use integraal::{DomainDescriptor, ComputeMethod, FunctionDescriptor, Integraal, IntegraalError}; +/// # fn main() { +/// // describe domain, function & computation method +/// let domain = DomainDescriptor::Uniform { +/// start: 0.0, +/// step: 0.00001, +/// n_step: 100_001, +/// }; +/// +/// // decribe the function and numerical integration method +/// let function = FunctionDescriptor::Closure(Box::new(|x: f64| 2.0 * x)); +/// let method = ComputeMethod::Trapezoid; +/// +/// // build the integral & compute it +/// let mut integral = Integraal::default(); +/// integral.domain(domain).function(function).method(method); +/// assert!(integral.compute().is_ok()) +/// # } +/// ``` +#[derive(Default)] +pub struct Integraal<'a> { + pub(crate) domain: Option>, + pub(crate) function: Option, + pub(crate) method: Option, +} diff --git a/src/structure.rs b/src/structure/implementations.rs similarity index 75% rename from src/structure.rs rename to src/structure/implementations.rs index c2cfe41..c923b33 100644 --- a/src/structure.rs +++ b/src/structure/implementations.rs @@ -1,86 +1,31 @@ -//! main structure and computation code +//! main method implementations -use crate::parameters::ComputeMethod; -use crate::{DomainDescriptor, FunctionDescriptor}; +// ------ IMPORTS -/// Integral error -#[derive(Debug)] -pub enum IntegraalError { - /// One or more parameters are missing. - MissingParameters(&'static str), - /// Specified parameters are conflicting or ambiguous. - InconsistentParameters(&'static str), -} +use crate::{ComputeMethod, DomainDescriptor, FunctionDescriptor, Integraal, IntegraalError}; -/// Main integral computation structure -/// -/// This structure is used as the entrypoint for integral definition and computation. It follows -/// a pseudo-builder patterns where the function description is reset after a computation. -/// -/// # Usage -/// -/// ## Components -/// -/// The structure is made up of three components that are used to describe the integral the user -/// wishes to compute: -/// - a [`DomainDescriptor`] instance, used to describe the space over which the integral span -/// - a [`FunctionDescriptor`] instance, used to describe the integrated function -/// - a [`ComputeMethod`] instance, used to choose which numerical integration method will be used -/// for computation -/// -/// In the future, another object might be included to control the execution backend. -/// -/// ## Example -/// -/// ```rust -/// # use integraal::{DomainDescriptor, ComputeMethod, FunctionDescriptor, Integraal, IntegraalError}; -/// # fn main() { -/// // describe domain, function & computation method -/// let domain = DomainDescriptor::Uniform { -/// start: 0.0, -/// step: 0.00001, -/// n_step: 100_001, -/// }; -/// -/// // decribe the function and numerical integration method -/// let function = FunctionDescriptor::Closure(Box::new(|x: f64| 2.0 * x)); -/// let method = ComputeMethod::Trapezoid; -/// -/// // build the integral & compute it -/// let mut integral = Integraal::default(); -/// integral.domain(domain).function(function).method(method); -/// assert!(integral.compute().is_ok()) -/// # } -/// ``` -#[derive(Default)] -pub struct Integraal<'a> { - domain: Option>, - function: Option, - method: Option, -} +// ------ CONTENT impl<'a> Integraal<'a> { - /// Setter + /// Set the domain descriptor. pub fn domain(&mut self, domain_descriptor: DomainDescriptor<'a>) -> &mut Self { self.domain = Some(domain_descriptor); self } - /// Setter + /// Set the function descriptor. pub fn function(&mut self, function_descriptor: FunctionDescriptor) -> &mut Self { self.function = Some(function_descriptor); self } - /// Setter + /// Set the numerical integration method. pub fn method(&mut self, compute_method: ComputeMethod) -> &mut Self { self.method = Some(compute_method); self } #[allow(clippy::missing_errors_doc, clippy::too_many_lines)] - /// Main computation method - /// /// This method attempts to compute the integral. If it is successful, it will clear the /// internal [`FunctionDescriptor`] object before returning the result. /// diff --git a/src/structure/mod.rs b/src/structure/mod.rs new file mode 100644 index 0000000..091a351 --- /dev/null +++ b/src/structure/mod.rs @@ -0,0 +1,15 @@ +//! main structure and computation code + +// ------ MODULE DECLARATIONS + +mod definitions; +mod implementations; + +// ------ RE-EXPORTS + +pub use definitions::{Integraal, IntegraalError}; + +// ------ TESTS + +#[cfg(test)] +mod tests; diff --git a/src/structure/tests.rs b/src/structure/tests.rs new file mode 100644 index 0000000..ae5035c --- /dev/null +++ b/src/structure/tests.rs @@ -0,0 +1,4 @@ +#[test] +fn basic() { + assert_eq!(1 + 1, 2); +}