summaryrefslogtreecommitdiff
path: root/lib/SlackBuilder/Trans.hs
blob: ee9b5396994cba6200649e31f0747ec95a3b562e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module SlackBuilder.Trans
    ( SlackBuilderException(..)
    , SlackBuilderT(..)
    ) where

import Control.Monad.Trans.Reader (ReaderT(..))
import Data.Text (Text)
import SlackBuilder.Config
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Catch (MonadCatch(..), MonadThrow(..))
import Control.Exception (Exception(..))

newtype SlackBuilderException = UpdaterNotFound Text
    deriving Show

instance Exception SlackBuilderException

newtype SlackBuilderT a = SlackBuilderT
    { runSlackBuilderT :: ReaderT Settings IO a
    }

instance Functor SlackBuilderT
  where
    fmap f (SlackBuilderT slackBuilderT) = SlackBuilderT $ f <$> slackBuilderT

instance Applicative SlackBuilderT
  where
    pure = SlackBuilderT . pure
    (SlackBuilderT f) <*> (SlackBuilderT x) = SlackBuilderT $ f <*> x

instance Monad SlackBuilderT
  where
    return = pure
    (SlackBuilderT x) >>= f = SlackBuilderT $ x >>= runSlackBuilderT . f

instance MonadIO SlackBuilderT
  where
    liftIO = SlackBuilderT . liftIO

instance MonadThrow SlackBuilderT
  where
    throwM = SlackBuilderT . throwM

instance MonadCatch SlackBuilderT
  where
    catch (SlackBuilderT action) handler =
        SlackBuilderT $ catch action $ runSlackBuilderT . handler