summaryrefslogtreecommitdiff
path: root/fig-monitor-irc/src/Fig/Monitor/IRC/Utils.hs
blob: b1176627705ca0542d234fe02e7092f4a0ba499c (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
{-# Language ApplicativeDo #-}

module Fig.Monitor.IRC.Utils
  ( FigMonitorIRCException(..)
  , Config(..)
  , loadConfig
  ) where

import Fig.Prelude

import qualified Toml

newtype FigMonitorIRCException = FigMonitorIRCException Text
  deriving (Show, Eq, Ord)
instance Exception FigMonitorIRCException

data Config = Config
  { host :: Text
  , port :: Int
  , nick :: Text
  , channels :: [Text]
  } deriving (Show, Eq, Ord)

configCodec :: Toml.TomlCodec Config
configCodec = do
  host <- Toml.text "host" Toml..= (\a -> a.host)
  port <- Toml.int "port" Toml..= (\a -> a.port)
  nick <- Toml.text "nick" Toml..= (\a -> a.nick)
  channels <- Toml.arrayOf Toml._Text "channels" Toml..= (\a -> a.channels)
  pure $ Config{..}

loadConfig :: FilePath -> IO Config
loadConfig path = Toml.decodeFileEither configCodec path >>= \case
  Left err -> throwM . FigMonitorIRCException $ tshow err
  Right config -> pure config