Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit b4a877d

Browse files
committed
Add ExceptT into the Fake monad stack
1 parent 305bb0a commit b4a877d

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/Fake.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module Fake (
1111

1212
import qualified Aeson as A
1313
import Control.Monad (forM, replicateM)
14-
import Control.Monad.IO.Class (liftIO, MonadIO)
14+
import Control.Monad.Except (ExceptT)
15+
import qualified Control.Monad.Except as Except
16+
import Control.Monad.IO.Class (MonadIO, liftIO)
1517
import Control.Monad.State.Class (MonadState)
1618
import Control.Monad.State.Strict (StateT)
1719
import qualified Control.Monad.State.Strict as State
@@ -39,7 +41,7 @@ import qualified Text.Regex.TDFA.ReadRegex as R
3941
-- >>> let exec expr = runFakeT (Just 1) (eval expr)
4042

4143

42-
newtype Fake a = Fake { runFake :: StateT Env IO a }
44+
newtype Fake a = Fake { runFake :: ExceptT String (StateT Env IO) a }
4345
deriving
4446
( Functor
4547
, Applicative
@@ -51,7 +53,10 @@ newtype Fake a = Fake { runFake :: StateT Env IO a }
5153
runFakeT :: Maybe Int -> Fake a -> IO a
5254
runFakeT seed fake = do
5355
env <- newEnv seed
54-
State.evalStateT (runFake fake) env
56+
result <- State.evalStateT (Except.runExceptT (runFake fake)) env
57+
case result of
58+
Left errorMsg -> error errorMsg
59+
Right result' -> pure result'
5560

5661
type State a = StateT Env IO a
5762

@@ -106,7 +111,7 @@ randomDouble lower upper = do
106111
--
107112
-- >>> exec "randomBool"
108113
-- Bool False
109-
randomBool :: Monad m => StateT Env m Value
114+
randomBool :: (RandomGen g, MonadState g m) => m Value
110115
randomBool = Bool <$> State.state random
111116

112117

@@ -249,7 +254,7 @@ fromFile fileName = do
249254
--
250255
-- >>> exec "randomChar()"
251256
-- String "\629160"
252-
randomChar :: Monad m => StateT Env m Value
257+
randomChar :: (RandomGen g, MonadState g m) => m Value
253258
randomChar = charToString <$> State.state random
254259
where
255260
charToString :: Char -> Value
@@ -277,8 +282,8 @@ eval (DoubleLiteral x) = pure $ Number x
277282
eval (FunctionCall "uuid4" []) = String . UUID.toText <$> State.state random
278283
eval (FunctionCall "uuid1" []) = String . UUID.toText <$> liftIO uuid1
279284
eval (FunctionCall "null" []) = pure Null
280-
eval (FunctionCall "randomBool" []) = Fake randomBool
281-
eval (FunctionCall "randomChar" []) = Fake randomChar
285+
eval (FunctionCall "randomBool" []) = randomBool
286+
eval (FunctionCall "randomChar" []) = randomChar
282287
eval (FunctionCall "randomInt" [lower, upper]) = randomInt lower upper
283288
eval (FunctionCall "randomDouble" [lower, upper]) = randomDouble lower upper
284289
eval (FunctionCall "array" args) = Array . V.fromList <$> mapM eval args

0 commit comments

Comments
 (0)