{-# LANGUAGE UndecidableInstances #-}

module Shrun.Configuration.Toml
  ( Toml (..),
  )
where

import Shrun.Configuration.Data.Core
  ( CoreConfigP
      ( MkCoreConfigP,
        commandLogging,
        commonLogging,
        consoleLogging,
        fileLogging,
        init,
        notify,
        timeout
      ),
    CoreConfigToml,
  )
import Shrun.Configuration.Data.Core.Timeout (Timeout)
import Shrun.Configuration.Default (Default (def))
import Shrun.Configuration.Toml.Legend (KeyVal)
import Shrun.Prelude

-- | Holds toml config.
data Toml = MkToml
  { -- | Core config.
    Toml -> CoreConfigToml
coreConfig :: CoreConfigToml,
    -- | Legend.
    Toml -> Maybe (List KeyVal)
legend :: Maybe (List KeyVal)
  }
  deriving stock (Toml -> Toml -> Bool
(Toml -> Toml -> Bool) -> (Toml -> Toml -> Bool) -> Eq Toml
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Toml -> Toml -> Bool
== :: Toml -> Toml -> Bool
$c/= :: Toml -> Toml -> Bool
/= :: Toml -> Toml -> Bool
Eq, Int -> Toml -> ShowS
[Toml] -> ShowS
Toml -> String
(Int -> Toml -> ShowS)
-> (Toml -> String) -> ([Toml] -> ShowS) -> Show Toml
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Toml -> ShowS
showsPrec :: Int -> Toml -> ShowS
$cshow :: Toml -> String
show :: Toml -> String
$cshowList :: [Toml] -> ShowS
showList :: [Toml] -> ShowS
Show)

instance
  (k ~ A_Lens, a ~ CoreConfigToml, b ~ CoreConfigToml) =>
  LabelOptic "coreConfig" k Toml Toml a b
  where
  labelOptic :: Optic k NoIx Toml Toml a b
labelOptic = LensVL Toml Toml a b -> Lens Toml Toml a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL Toml Toml a b -> Lens Toml Toml a b)
-> LensVL Toml Toml a b -> Lens Toml Toml a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkToml CoreConfigToml
_coreConfig Maybe (List KeyVal)
_legend) ->
    (CoreConfigToml -> Toml) -> f CoreConfigToml -> f Toml
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (CoreConfigToml -> Maybe (List KeyVal) -> Toml
`MkToml` Maybe (List KeyVal)
_legend) (a -> f b
f a
CoreConfigToml
_coreConfig)
  {-# INLINE labelOptic #-}

instance
  (k ~ A_Lens, a ~ Maybe (List KeyVal), b ~ Maybe (List KeyVal)) =>
  LabelOptic "legend" k Toml Toml a b
  where
  labelOptic :: Optic k NoIx Toml Toml a b
labelOptic = LensVL Toml Toml a b -> Lens Toml Toml a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL Toml Toml a b -> Lens Toml Toml a b)
-> LensVL Toml Toml a b -> Lens Toml Toml a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkToml CoreConfigToml
_coreConfig Maybe (List KeyVal)
_legend) ->
    (Maybe (List KeyVal) -> Toml) -> f (Maybe (List KeyVal)) -> f Toml
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (CoreConfigToml -> Maybe (List KeyVal) -> Toml
MkToml CoreConfigToml
_coreConfig) (a -> f b
f a
Maybe (List KeyVal)
_legend)
  {-# INLINE labelOptic #-}

instance Default Toml where
  def :: Toml
def =
    MkToml
      { coreConfig :: CoreConfigToml
coreConfig = CoreConfigToml
forall a. Default a => a
def,
        legend :: Maybe (List KeyVal)
legend = Maybe (List KeyVal)
forall a. Default a => a
def
      }

instance DecodeTOML Toml where
  tomlDecoder :: Decoder Toml
tomlDecoder = do
    Maybe Timeout
timeout <- Decoder (Maybe Timeout)
decodeTimeout
    Maybe Text
init <- Decoder (Maybe Text)
decodeInit
    Maybe (CommonLoggingP 'ConfigPhaseToml)
commonLogging <- Decoder (CommonLoggingP 'ConfigPhaseToml)
-> Text -> Decoder (Maybe (CommonLoggingP 'ConfigPhaseToml))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (CommonLoggingP 'ConfigPhaseToml)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"common-log"
    Maybe (CommandLoggingP 'ConfigPhaseToml)
commandLogging <- Decoder (CommandLoggingP 'ConfigPhaseToml)
-> Text -> Decoder (Maybe (CommandLoggingP 'ConfigPhaseToml))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (CommandLoggingP 'ConfigPhaseToml)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"command-log"
    Maybe (ConsoleLoggingP 'ConfigPhaseToml)
consoleLogging <- Decoder (ConsoleLoggingP 'ConfigPhaseToml)
-> Text -> Decoder (Maybe (ConsoleLoggingP 'ConfigPhaseToml))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (ConsoleLoggingP 'ConfigPhaseToml)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"console-log"
    Maybe (FileLoggingP 'ConfigPhaseToml)
fileLogging <- Decoder (FileLoggingP 'ConfigPhaseToml)
-> Text -> Decoder (Maybe (FileLoggingP 'ConfigPhaseToml))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (FileLoggingP 'ConfigPhaseToml)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"file-log"
    Maybe (NotifyP 'ConfigPhaseToml)
notify <- Decoder (NotifyP 'ConfigPhaseToml)
-> Text -> Decoder (Maybe (NotifyP 'ConfigPhaseToml))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (NotifyP 'ConfigPhaseToml)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"notify"

    Maybe (List KeyVal)
legend <- Decoder (Maybe (List KeyVal))
decodeLegend
    pure
      $ MkToml
        { coreConfig :: CoreConfigToml
coreConfig =
            MkCoreConfigP
              { Maybe Timeout
ConfigPhaseMaybeF 'ConfigPhaseToml Timeout
timeout :: ConfigPhaseMaybeF 'ConfigPhaseToml Timeout
timeout :: Maybe Timeout
timeout,
                Maybe Text
ConfigPhaseMaybeF 'ConfigPhaseToml Text
init :: ConfigPhaseMaybeF 'ConfigPhaseToml Text
init :: Maybe Text
init,
                Maybe (CommonLoggingP 'ConfigPhaseToml)
TomlOptF 'ConfigPhaseToml (CommonLoggingP 'ConfigPhaseToml)
commonLogging :: TomlOptF 'ConfigPhaseToml (CommonLoggingP 'ConfigPhaseToml)
commonLogging :: Maybe (CommonLoggingP 'ConfigPhaseToml)
commonLogging,
                Maybe (CommandLoggingP 'ConfigPhaseToml)
TomlOptF 'ConfigPhaseToml (CommandLoggingP 'ConfigPhaseToml)
commandLogging :: TomlOptF 'ConfigPhaseToml (CommandLoggingP 'ConfigPhaseToml)
commandLogging :: Maybe (CommandLoggingP 'ConfigPhaseToml)
commandLogging,
                Maybe (ConsoleLoggingP 'ConfigPhaseToml)
TomlOptF 'ConfigPhaseToml (ConsoleLoggingP 'ConfigPhaseToml)
consoleLogging :: TomlOptF 'ConfigPhaseToml (ConsoleLoggingP 'ConfigPhaseToml)
consoleLogging :: Maybe (ConsoleLoggingP 'ConfigPhaseToml)
consoleLogging,
                Maybe (FileLoggingP 'ConfigPhaseToml)
ArgsOnlyDetF 'ConfigPhaseToml (FileLoggingP 'ConfigPhaseToml)
fileLogging :: ArgsOnlyDetF 'ConfigPhaseToml (FileLoggingP 'ConfigPhaseToml)
fileLogging :: Maybe (FileLoggingP 'ConfigPhaseToml)
fileLogging,
                Maybe (NotifyP 'ConfigPhaseToml)
ArgsOnlyDetF 'ConfigPhaseToml (NotifyP 'ConfigPhaseToml)
notify :: ArgsOnlyDetF 'ConfigPhaseToml (NotifyP 'ConfigPhaseToml)
notify :: Maybe (NotifyP 'ConfigPhaseToml)
notify
              },
          Maybe (List KeyVal)
legend :: Maybe (List KeyVal)
legend :: Maybe (List KeyVal)
legend
        }

decodeTimeout :: Decoder (Maybe Timeout)
decodeTimeout :: Decoder (Maybe Timeout)
decodeTimeout = Decoder Timeout -> Text -> Decoder (Maybe Timeout)
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder Timeout
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"timeout"

decodeInit :: Decoder (Maybe Text)
decodeInit :: Decoder (Maybe Text)
decodeInit = Decoder Text -> Text -> Decoder (Maybe Text)
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"init"

decodeLegend :: Decoder (Maybe (List KeyVal))
decodeLegend :: Decoder (Maybe (List KeyVal))
decodeLegend = Decoder (List KeyVal) -> Text -> Decoder (Maybe (List KeyVal))
forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder (List KeyVal)
forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"legend"