-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParseConfig.hs
54 lines (46 loc) · 1.48 KB
/
ParseConfig.hs
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
{-# LANGUAGE OverloadedStrings #-}
module ParseConfig
( Config(..)
, parseConfig
) where
import Data.Maybe (fromMaybe)
import qualified Data.Yaml as Y
import Data.Yaml (FromJSON(parseJSON), (.:?))
data RawConfig = RawConfig
{ __included_dirs :: Maybe [FilePath]
, __excluded_dirs :: Maybe [FilePath]
, __included_files :: Maybe [FilePath]
, __excluded_files :: Maybe [FilePath]
} deriving Show
data Config = Config
{ included_dirs :: [FilePath]
, excluded_dirs :: [FilePath]
, included_files :: [FilePath]
, excluded_files :: [FilePath]
} deriving Show
instance FromJSON RawConfig where
parseJSON (Y.Object v) =
RawConfig <$>
v .:? "included-dirs" <*>
v .:? "excluded-dirs" <*>
v .:? "included-files" <*>
v .:? "excluded-files"
parseJSON _ = fail "Expected Object for Config value"
-- -- UNUSED
-- parseRawConfig :: FilePath -> IO (Either Y.ParseException RawConfig)
-- parseRawConfig = Y.decodeFileEither
parseConfig :: FilePath -> IO Config
parseConfig fp = do
result <- Y.decodeFileEither fp
case result of
Left _ -> return defaultConfig
Right cfg -> return $ defaults cfg
where
defaults (RawConfig _incDirs _excDirs _incFiles _excFiles) =
let incDirs = fromMaybe [] _incDirs
excDirs = fromMaybe [] _excDirs
incFiles = fromMaybe [] _incFiles
excFiles = fromMaybe [] _excFiles
in Config incDirs excDirs incFiles excFiles
defaultConfig :: Config
defaultConfig = Config [] [] [] []