module Hix.Monad where import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE) import Control.Monad.Trans.Reader (ReaderT (runReaderT)) import Path (Abs, Dir, Path) import Hix.Data.Error (Error (BootstrapError, EnvError, GhciError, NewError), tryIO) data Env = Env { Env -> Path Abs Dir cwd :: Path Abs Dir } deriving stock (Env -> Env -> Bool forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a /= :: Env -> Env -> Bool $c/= :: Env -> Env -> Bool == :: Env -> Env -> Bool $c== :: Env -> Env -> Bool Eq, Int -> Env -> ShowS [Env] -> ShowS Env -> String forall a. (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a showList :: [Env] -> ShowS $cshowList :: [Env] -> ShowS show :: Env -> String $cshow :: Env -> String showsPrec :: Int -> Env -> ShowS $cshowsPrec :: Int -> Env -> ShowS Show, forall x. Rep Env x -> Env forall x. Env -> Rep Env x forall a. (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a $cto :: forall x. Rep Env x -> Env $cfrom :: forall x. Env -> Rep Env x Generic) type M a = ReaderT Env (ExceptT Error IO) a throwM :: Error -> M a throwM :: forall a. Error -> M a throwM = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. (MonadTrans t, Monad m) => m a -> t m a lift forall b c a. (b -> c) -> (a -> b) -> a -> c . forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a throwE noteEnv :: Text -> Maybe a -> M a noteEnv :: forall a. Text -> Maybe a -> M a noteEnv Text err = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. Error -> M a throwM (Text -> Error EnvError Text err)) forall (f :: * -> *) a. Applicative f => a -> f a pure noteGhci :: Text -> Maybe a -> M a noteGhci :: forall a. Text -> Maybe a -> M a noteGhci Text err = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. Error -> M a throwM (Text -> Error GhciError Text err)) forall (f :: * -> *) a. Applicative f => a -> f a pure noteNew :: Text -> Maybe a -> M a noteNew :: forall a. Text -> Maybe a -> M a noteNew Text err = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. Error -> M a throwM (Text -> Error NewError Text err)) forall (f :: * -> *) a. Applicative f => a -> f a pure noteBootstrap :: Text -> Maybe a -> M a noteBootstrap :: forall a. Text -> Maybe a -> M a noteBootstrap Text err = forall b a. b -> (a -> b) -> Maybe a -> b maybe (forall a. Error -> M a throwM (Text -> Error BootstrapError Text err)) forall (f :: * -> *) a. Applicative f => a -> f a pure runM :: Path Abs Dir -> M a -> IO (Either Error a) runM :: forall a. Path Abs Dir -> M a -> IO (Either Error a) runM Path Abs Dir root M a ma = forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a) runExceptT (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a runReaderT M a ma (Path Abs Dir -> Env Env Path Abs Dir root)) tryIOM :: IO a -> M a tryIOM :: forall a. IO a -> M a tryIOM IO a ma = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. (MonadTrans t, Monad m) => m a -> t m a lift (forall a. IO a -> ExceptT Error IO a tryIO IO a ma)