aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Config.hs
blob: d7fccb6e923addbd05f27b524ca356f3b79b5cf0 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{-|
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(..)
    , defaultConfig
    , Probability(..)
    , probBlock
    , probNonBlock
    , probAssign
    , probAlways
    , propSize
    , propSeed
    , configProbability
    , configProperty
    , parseConfigFile
    , parseConfig
    )
where

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

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

makeLenses ''Probability

data Property = Property { _propSize :: {-# UNPACK #-} !Int
                         , _propSeed :: !(Maybe Int)
                         }
              deriving (Eq, Show)

makeLenses ''Property

data Config = Config { _configProbability :: {-# UNPACK #-} !Probability
                     , _configProperty    :: {-# UNPACK #-} !Property
                     }
            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

defaultConfig :: Config
defaultConfig = Config (Probability 1 1 1 1) (Property 100 Nothing)

probCodec :: TomlCodec Probability
probCodec =
    Probability
        <$> defaultValue (defProb probAssign) (Toml.int "assign")
        .=  _probAssign
        <*> defaultValue (defProb probAlways) (Toml.int "always")
        .=  _probAlways
        <*> defaultValue (defProb probBlock) (Toml.int "blocking")
        .=  _probAlways
        <*> defaultValue (defProb probNonBlock) (Toml.int "nonblocking")
        .=  _probAlways
    where defProb i = defaultConfig ^. configProbability . i

propCodec :: TomlCodec Property
propCodec =
    Property
        <$> defaultValue (defProp propSize) (Toml.int "size")
        .=  _propSize
        <*> Toml.dioptional (Toml.int "seed")
        .=  _propSeed
    where defProp i = defaultConfig ^. configProperty . i

configCodec :: TomlCodec Config
configCodec =
    Config
        <$> defaultValue (defaultConfig ^. configProbability)
                         (Toml.table probCodec "probability")
        .=  _configProbability
        <*> defaultValue (defaultConfig ^. configProperty)
                         (Toml.table propCodec "property")
        .=  _configProperty

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

parseConfig :: Text -> Config
parseConfig t = case Toml.decode configCodec t of
    Right c                      -> c
    Left Toml.TrivialError -> error "Trivial error while parsing Toml config"
    Left  (Toml.KeyNotFound   k) -> error $ "Key " ++ show k ++ " not found"
    Left  (Toml.TableNotFound k) -> error $ "Table " ++ show k ++ " not found"
    Left (Toml.TypeMismatch k _ _) ->
        error $ "Type mismatch with key " ++ show k
    Left (Toml.ParseError _) -> error "Config file parse error"