-- | Provides the 'FilePathDefault' type.
module Shrun.Configuration.Data.FileLogging.FilePathDefault
  ( FilePathDefault (..),
    parseFilePathDefault,
  )
where

import Data.Text qualified as T
import Effects.FileSystem.Utils qualified as FsUtils
import Shrun.Prelude

-- | OsPath option that includes a default possibility.
data FilePathDefault
  = FPDefault
  | FPManual OsPath
  deriving stock (FilePathDefault -> FilePathDefault -> Bool
(FilePathDefault -> FilePathDefault -> Bool)
-> (FilePathDefault -> FilePathDefault -> Bool)
-> Eq FilePathDefault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FilePathDefault -> FilePathDefault -> Bool
== :: FilePathDefault -> FilePathDefault -> Bool
$c/= :: FilePathDefault -> FilePathDefault -> Bool
/= :: FilePathDefault -> FilePathDefault -> Bool
Eq, Int -> FilePathDefault -> ShowS
[FilePathDefault] -> ShowS
FilePathDefault -> String
(Int -> FilePathDefault -> ShowS)
-> (FilePathDefault -> String)
-> ([FilePathDefault] -> ShowS)
-> Show FilePathDefault
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FilePathDefault -> ShowS
showsPrec :: Int -> FilePathDefault -> ShowS
$cshow :: FilePathDefault -> String
show :: FilePathDefault -> String
$cshowList :: [FilePathDefault] -> ShowS
showList :: [FilePathDefault] -> ShowS
Show)

instance DecodeTOML FilePathDefault where
  tomlDecoder :: Decoder FilePathDefault
tomlDecoder = Decoder Text -> Decoder FilePathDefault
forall (m :: Type -> Type).
MonadFail m =>
m Text -> m FilePathDefault
parseFilePathDefault Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder

parseFilePathDefault :: (MonadFail m) => m Text -> m FilePathDefault
parseFilePathDefault :: forall (m :: Type -> Type).
MonadFail m =>
m Text -> m FilePathDefault
parseFilePathDefault m Text
getTxt =
  m Text
getTxt m Text -> (Text -> m FilePathDefault) -> m FilePathDefault
forall a b. m a -> (a -> m b) -> m b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Text
"default" -> FilePathDefault -> m FilePathDefault
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure FilePathDefault
FPDefault
    Text
"" -> String -> m FilePathDefault
forall a. String -> m a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail String
"Empty path given for --file-log"
    Text
other -> OsPath -> FilePathDefault
FPManual (OsPath -> FilePathDefault) -> m OsPath -> m FilePathDefault
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> m OsPath
forall (m :: Type -> Type). MonadFail m => String -> m OsPath
FsUtils.encodeFpToOsFail (Text -> String
T.unpack Text
other)