aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Config.hs
blob: a51743e2c1fa9620fd714c3b2c140e3bd07eb436 (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
48
49
50
51
52
53
54
55
56
57
{-|
Module      : VeriFuzz.Config
Description : Configuration file format and parser.
Copyright   : (c) 2019, Yann Herklotz Grave
License     : GPL-3
Maintainer  : ymherklotz [at] gmail [dot] com
Stability   : experimental
Portability : POSIX

Configuration file format and parser.
-}

{-# LANGUAGE TemplateHaskell #-}

module VeriFuzz.Config
    ( Config(..)
    , Probability(..)
    , probAssign
    , probAlways
    , configProbability
    , parseConfigFile
    )
where

import           Control.Applicative (Alternative)
import           Control.Lens        hiding ((.=))
import           Data.Maybe          (fromMaybe)
import           Toml                (TomlCodec, (.=))
import qualified Toml

data Probability = Probability { _probAssign :: {-# UNPACK #-} !Int
                               , _probAlways :: {-# UNPACK #-} !Int
                               }
                 deriving (Eq, Show)

makeLenses ''Probability

newtype Config = Config { _configProbability :: Probability }
               deriving (Eq, Show)

makeLenses ''Config

defaultValue
  :: (Alternative r, Applicative w) =>
     b -> Toml.Codec r w a b -> Toml.Codec r w a b
defaultValue x = Toml.dimap Just (fromMaybe x) . Toml.dioptional

probCodec :: TomlCodec Probability
probCodec = Probability
    <$> Toml.int "assign" .= _probAssign
    <*> defaultValue 1 (Toml.int "always") .= _probAlways

configCodec :: TomlCodec Config
configCodec = Toml.dimap _configProbability Config $ Toml.table probCodec "probability"

parseConfigFile :: FilePath -> IO Config
parseConfigFile = Toml.decodeFile configCodec