{-# LANGUAGE QuasiQuotes #-}

-- | Provides TH for CLI args.
module Shrun.Configuration.Args.TH
  ( getDefaultConfigTH,
  )
where

import Data.Text qualified as T
import Language.Haskell.TH (Code, Q)
import Language.Haskell.TH.Syntax (Lift (liftTyped), bindCode, runIO)
import Shrun.Prelude

-- TODO: Once we have multiline strings, it may be feasible to remove the TH
-- here.

-- | Reads examples/default.toml at compile time.
getDefaultConfigTH :: Code Q (List Text)
getDefaultConfigTH :: Code Q [Text]
getDefaultConfigTH = Q [Text] -> ([Text] -> Code Q [Text]) -> Code Q [Text]
forall (m :: Type -> Type) a b.
Monad m =>
m a -> (a -> Code m b) -> Code m b
bindCode (IO [Text] -> Q [Text]
forall a. IO a -> Q a
runIO IO [Text]
getDefault) [Text] -> Code Q [Text]
forall t (m :: Type -> Type). (Lift t, Quote m) => t -> Code m t
forall (m :: Type -> Type). Quote m => [Text] -> Code m [Text]
liftTyped
  where
    getDefault :: IO [Text]
getDefault = Text -> [Text]
T.lines (Text -> [Text]) -> IO Text -> IO [Text]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> OsPath -> IO Text
forall (m :: Type -> Type).
(HasCallStack, MonadFileReader m) =>
OsPath -> m Text
readFileUtf8Lenient [osp|examples/default.toml|]