Skip to content

Commit

Permalink
refactor: reorganize internal structure module (#5)
Browse files Browse the repository at this point in the history
* split structure module into multiple files

* add dedicated test file

* update doc
  • Loading branch information
imrn99 authored May 8, 2024
1 parent f9e8fbe commit 5272b52
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 62 deletions.
63 changes: 63 additions & 0 deletions src/structure/definitions.rs
Original file line number Diff line number Diff line change
@@ -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<DomainDescriptor<'a>>,
pub(crate) function: Option<FunctionDescriptor>,
pub(crate) method: Option<ComputeMethod>,
}
69 changes: 7 additions & 62 deletions src/structure.rs → src/structure/implementations.rs
Original file line number Diff line number Diff line change
@@ -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<DomainDescriptor<'a>>,
function: Option<FunctionDescriptor>,
method: Option<ComputeMethod>,
}
// ------ 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.
///
Expand Down
15 changes: 15 additions & 0 deletions src/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/structure/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn basic() {
assert_eq!(1 + 1, 2);
}

0 comments on commit 5272b52

Please sign in to comment.