mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-27 11:24:45 +08:00
refactor(router): move fp_utils module to common_utils crate (#391)
Co-authored-by: Arun Raj M <jarnura47@gmail.com> Co-authored-by: Nishant Joshi <nishant.joshi@juspay.in>
This commit is contained in:
39
crates/common_utils/src/fp_utils.rs
Normal file
39
crates/common_utils/src/fp_utils.rs
Normal file
@ -0,0 +1,39 @@
|
||||
//! Functional programming utilities
|
||||
|
||||
/// The Applicative trait provides a pure behavior,
|
||||
/// which can be used to create values of type f a from values of type a.
|
||||
pub trait Applicative<R> {
|
||||
/// The Associative type acts as a (f a) wrapper for Self.
|
||||
type WrappedSelf<T>;
|
||||
|
||||
/// Applicative::pure(_) is abstraction with lifts any arbitrary type to underlying higher
|
||||
/// order type
|
||||
fn pure(v: R) -> Self::WrappedSelf<R>;
|
||||
}
|
||||
|
||||
impl<R> Applicative<R> for Option<R> {
|
||||
type WrappedSelf<T> = Option<T>;
|
||||
fn pure(v: R) -> Self::WrappedSelf<R> {
|
||||
Some(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, E> Applicative<R> for Result<R, E> {
|
||||
type WrappedSelf<T> = Result<T, E>;
|
||||
fn pure(v: R) -> Self::WrappedSelf<R> {
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
/// This function wraps the evaluated result of `f` into current context,
|
||||
/// based on the condition provided into the `predicate`
|
||||
pub fn when<W: Applicative<(), WrappedSelf<()> = W>, F>(predicate: bool, f: F) -> W
|
||||
where
|
||||
F: FnOnce() -> W,
|
||||
{
|
||||
if predicate {
|
||||
f()
|
||||
} else {
|
||||
W::pure(())
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ pub mod crypto;
|
||||
pub mod custom_serde;
|
||||
pub mod errors;
|
||||
pub mod ext_traits;
|
||||
pub mod fp_utils;
|
||||
pub mod pii;
|
||||
pub mod validation;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user