{-# LANGUAGE OverloadedStrings #-}

module Rzk.Project.Config where

import           Data.Yaml (FromJSON (..), (.!=), (.:), (.:?))
import qualified Data.Yaml as Y

data ProjectConfig = ProjectConfig
  { ProjectConfig -> [FilePath]
include :: [FilePath]
  , ProjectConfig -> [FilePath]
exclude :: [FilePath]
  } deriving (ProjectConfig -> ProjectConfig -> Bool
(ProjectConfig -> ProjectConfig -> Bool)
-> (ProjectConfig -> ProjectConfig -> Bool) -> Eq ProjectConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProjectConfig -> ProjectConfig -> Bool
== :: ProjectConfig -> ProjectConfig -> Bool
$c/= :: ProjectConfig -> ProjectConfig -> Bool
/= :: ProjectConfig -> ProjectConfig -> Bool
Eq, Int -> ProjectConfig -> ShowS
[ProjectConfig] -> ShowS
ProjectConfig -> FilePath
(Int -> ProjectConfig -> ShowS)
-> (ProjectConfig -> FilePath)
-> ([ProjectConfig] -> ShowS)
-> Show ProjectConfig
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectConfig -> ShowS
showsPrec :: Int -> ProjectConfig -> ShowS
$cshow :: ProjectConfig -> FilePath
show :: ProjectConfig -> FilePath
$cshowList :: [ProjectConfig] -> ShowS
showList :: [ProjectConfig] -> ShowS
Show)

instance FromJSON ProjectConfig where
  parseJSON :: Value -> Parser ProjectConfig
parseJSON (Y.Object Object
v) =
    [FilePath] -> [FilePath] -> ProjectConfig
ProjectConfig   ([FilePath] -> [FilePath] -> ProjectConfig)
-> Parser [FilePath] -> Parser ([FilePath] -> ProjectConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    Object
v Object -> Key -> Parser [FilePath]
forall a. FromJSON a => Object -> Key -> Parser a
.:  Key
"include" Parser ([FilePath] -> ProjectConfig)
-> Parser [FilePath] -> Parser ProjectConfig
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
    Object
v Object -> Key -> Parser (Maybe [FilePath])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"exclude" Parser (Maybe [FilePath]) -> [FilePath] -> Parser [FilePath]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  parseJSON Value
_ = FilePath -> Parser ProjectConfig
forall a. FilePath -> Parser a
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"Expected config value to be an object"