From d08c35c77c43a38d0c65d66c241d21fd548f6666 Mon Sep 17 00:00:00 2001 From: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:27:05 +0530 Subject: [PATCH] refactor(router): move `fp_utils` module to `common_utils` crate (#391) Co-authored-by: Arun Raj M Co-authored-by: Nishant Joshi --- .../{router/src/utils => common_utils/src}/fp_utils.rs | 10 +++++++++- crates/common_utils/src/lib.rs | 1 + crates/router/src/utils.rs | 7 ++----- 3 files changed, 12 insertions(+), 6 deletions(-) rename crates/{router/src/utils => common_utils/src}/fp_utils.rs (55%) diff --git a/crates/router/src/utils/fp_utils.rs b/crates/common_utils/src/fp_utils.rs similarity index 55% rename from crates/router/src/utils/fp_utils.rs rename to crates/common_utils/src/fp_utils.rs index f38574a41c..1c3fd31863 100644 --- a/crates/router/src/utils/fp_utils.rs +++ b/crates/common_utils/src/fp_utils.rs @@ -1,6 +1,13 @@ +//! 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 { + /// The Associative type acts as a (f a) wrapper for Self. type WrappedSelf; + /// Applicative::pure(_) is abstraction with lifts any arbitrary type to underlying higher + /// order type fn pure(v: R) -> Self::WrappedSelf; } @@ -18,7 +25,8 @@ impl Applicative for Result { } } -// This function allows lazy evaluation of the `f` argument +/// This function wraps the evaluated result of `f` into current context, +/// based on the condition provided into the `predicate` pub fn when = W>, F>(predicate: bool, f: F) -> W where F: FnOnce() -> W, diff --git a/crates/common_utils/src/lib.rs b/crates/common_utils/src/lib.rs index fd73041480..384ca42b22 100644 --- a/crates/common_utils/src/lib.rs +++ b/crates/common_utils/src/lib.rs @@ -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; diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs index 526f1afbf3..ada3ff962d 100644 --- a/crates/router/src/utils.rs +++ b/crates/router/src/utils.rs @@ -1,7 +1,6 @@ pub(crate) mod custom_serde; pub(crate) mod db_utils; mod ext_traits; -mod fp_utils; #[cfg(feature = "kv_store")] pub(crate) mod storage_partitioning; @@ -9,14 +8,12 @@ pub(crate) mod storage_partitioning; pub(crate) use common_utils::{ crypto, ext_traits::{ByteSliceExt, BytesExt, Encode, StringExt, ValueExt}, + fp_utils::when, validation::validate_email, }; use nanoid::nanoid; -pub(crate) use self::{ - ext_traits::{OptionExt, ValidateCall}, - fp_utils::when, -}; +pub(crate) use self::ext_traits::{OptionExt, ValidateCall}; use crate::consts; pub mod error_parser {