-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib): Make MonadPass more flexible
- Loading branch information
1 parent
e749c33
commit bda1591
Showing
4 changed files
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,57 @@ | ||
module Data.Gibberish.MonadPass | ||
( PassT (..), | ||
( Pass (), | ||
PassT (..), | ||
runPass, | ||
evalPass, | ||
usingPass, | ||
runPassT, | ||
evalPassT, | ||
usingPassT, | ||
) where | ||
|
||
import Data.Gibberish.Types (GenPassOptions ()) | ||
|
||
import Control.Monad.Random.Class (MonadRandom ()) | ||
import Control.Monad.Reader (MonadReader (), ReaderT (), runReaderT) | ||
import Control.Monad.Trans.Random (RandT (), runRandT) | ||
import Control.Monad.Trans.Random (RandT (), evalRandT, runRandT) | ||
import Data.Functor.Identity (Identity (..)) | ||
|
||
-- | Password/Passphrase generation monad parameterized by the type @gen@ of the generator | ||
-- to carry | ||
type Pass gen = PassT gen Identity | ||
|
||
-- | Run a generation computation with the given options and initial generator | ||
runPass :: Pass gen a -> gen -> (a, gen) | ||
runPass action = runIdentity . runPassT action | ||
|
||
-- | Evaluate a generation computation with the given options and initial | ||
-- generator, discarding the final generator | ||
evalPass :: Pass gen a -> gen -> a | ||
evalPass action = runIdentity . evalPassT action | ||
|
||
-- | Shorter and more readable alias for @flip runPassT@. | ||
usingPass :: gen -> Pass gen a -> (a, gen) | ||
usingPass = flip runPass | ||
|
||
-- | Password/Passphrase generation monad | ||
newtype PassT g m a = PassT {unPass :: ReaderT GenPassOptions (RandT g m) a} | ||
-- | Password/Passphrase generation transformer monad parameterized by : | ||
-- | ||
-- * @gen@ - the generator. | ||
-- * @m@ - the inner monad. | ||
newtype PassT gen m a = PassT {unPass :: RandT gen m a} | ||
deriving newtype | ||
( Applicative, | ||
Functor, | ||
Monad, | ||
MonadFail, | ||
MonadRandom, | ||
MonadReader GenPassOptions | ||
MonadRandom | ||
) | ||
|
||
-- | Run a generation computation with the given options and initial generator | ||
runPassT :: PassT g m a -> GenPassOptions -> g -> m (a, g) | ||
runPassT act = runRandT . runReaderT (unPass act) | ||
runPassT :: PassT g m a -> g -> m (a, g) | ||
runPassT = runRandT . unPass | ||
|
||
-- | Evaluate a generation computation with the given options and initial | ||
-- generator, discarding the final generator | ||
evalPassT :: Functor m => PassT g m a -> GenPassOptions -> g -> m a | ||
evalPassT act opts gen = fst <$> runPassT act opts gen | ||
evalPassT :: Monad m => PassT g m a -> g -> m a | ||
evalPassT = evalRandT . unPass | ||
|
||
-- | Like @runGenT@, but the computation is the last argument | ||
usingPassT :: GenPassOptions -> g -> PassT g m a -> m (a, g) | ||
usingPassT opts gen act = runPassT act opts gen | ||
-- | Shorter and more readable alias for @flip runPassT@. | ||
usingPassT :: g -> PassT g m a -> m (a, g) | ||
usingPassT = flip runPassT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters