module Shrun.Configuration.Data.FileLogging.FileMode
  ( FileMode (..),
    parseFileMode,
  )
where

import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude

-- | File mode.
data FileMode
  = FileModeAppend
  | FileModeWrite
  deriving stock (FileMode -> FileMode -> Bool
(FileMode -> FileMode -> Bool)
-> (FileMode -> FileMode -> Bool) -> Eq FileMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FileMode -> FileMode -> Bool
== :: FileMode -> FileMode -> Bool
$c/= :: FileMode -> FileMode -> Bool
/= :: FileMode -> FileMode -> Bool
Eq, Int -> FileMode -> ShowS
[FileMode] -> ShowS
FileMode -> String
(Int -> FileMode -> ShowS)
-> (FileMode -> String) -> ([FileMode] -> ShowS) -> Show FileMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FileMode -> ShowS
showsPrec :: Int -> FileMode -> ShowS
$cshow :: FileMode -> String
show :: FileMode -> String
$cshowList :: [FileMode] -> ShowS
showList :: [FileMode] -> ShowS
Show)

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

parseFileMode :: (MonadFail m) => m Text -> m FileMode
parseFileMode :: forall (m :: Type -> Type). MonadFail m => m Text -> m FileMode
parseFileMode m Text
getTxt =
  m Text
getTxt m Text -> (Text -> m FileMode) -> m FileMode
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
"append" -> FileMode -> m FileMode
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure FileMode
FileModeAppend
    Text
"write" -> FileMode -> m FileMode
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure FileMode
FileModeWrite
    Text
bad -> String -> m FileMode
forall a. String -> m a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail (String -> m FileMode) -> String -> m FileMode
forall a b. (a -> b) -> a -> b
$ String
"Unrecognized file-mode: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
unpack Text
bad

instance Default FileMode where
  def :: FileMode
def = FileMode
FileModeWrite