module Taskell.IO.Config.General
    ( Config
    , defaultConfig
    , parser
    , filename
    , debug
    ) where

import ClassyPrelude

import Data.Ini.Config

import Taskell.IO.Config.Parser (noEmpty)

data Config = Config
    { Config -> FilePath
filename :: FilePath
    , Config -> Bool
debug    :: Bool
    }

defaultConfig :: Config
defaultConfig :: Config
defaultConfig = Config :: FilePath -> Bool -> Config
Config {filename :: FilePath
filename = FilePath
"taskell.md", debug :: Bool
debug = Bool
False}

filenameP :: SectionParser String
filenameP :: SectionParser FilePath
filenameP = FilePath -> (Text -> FilePath) -> Maybe Text -> FilePath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Config -> FilePath
filename Config
defaultConfig) Text -> FilePath
forall mono. MonoFoldable mono => mono -> [Element mono]
unpack (Maybe Text -> FilePath)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> FilePath
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> FilePath)
-> SectionParser (Maybe Text) -> SectionParser FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"filename"

debugP :: SectionParser Bool
debugP :: SectionParser Bool
debugP = Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"debug" Bool
False

parser :: IniParser Config
parser :: IniParser Config
parser = Config -> Maybe Config -> Config
forall a. a -> Maybe a -> a
fromMaybe Config
defaultConfig (Maybe Config -> Config)
-> IniParser (Maybe Config) -> IniParser Config
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser Config -> IniParser (Maybe Config)
forall a. Text -> SectionParser a -> IniParser (Maybe a)
sectionMb Text
"general" (FilePath -> Bool -> Config
Config (FilePath -> Bool -> Config)
-> SectionParser FilePath -> SectionParser (Bool -> Config)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SectionParser FilePath
filenameP SectionParser (Bool -> Config)
-> SectionParser Bool -> SectionParser Config
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SectionParser Bool
debugP)