module Shrun.Configuration.Data.LegendKeysCache
  ( LegendKeysCache (..),
    parseLegendKeysCache,
    lksMeta,
  )
where

import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude
import Shrun.Utils qualified as Utils

-- | Determines how to handle the legend key cache.
data LegendKeysCache
  = -- | New legend keys are added to the cache.
    LegendKeysAdd
  | -- | The cache is deleted, if it exists.
    LegendKeysClear
  | -- | Do nothing.
    LegendKeysOff
  | -- | New legend keys are written to the cache. Previous values are
    -- dropped.
    LegendKeysWrite
  deriving stock (LegendKeysCache
LegendKeysCache -> LegendKeysCache -> Bounded LegendKeysCache
forall a. a -> a -> Bounded a
$cminBound :: LegendKeysCache
minBound :: LegendKeysCache
$cmaxBound :: LegendKeysCache
maxBound :: LegendKeysCache
Bounded, Int -> LegendKeysCache
LegendKeysCache -> Int
LegendKeysCache -> [LegendKeysCache]
LegendKeysCache -> LegendKeysCache
LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
LegendKeysCache
-> LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
(LegendKeysCache -> LegendKeysCache)
-> (LegendKeysCache -> LegendKeysCache)
-> (Int -> LegendKeysCache)
-> (LegendKeysCache -> Int)
-> (LegendKeysCache -> [LegendKeysCache])
-> (LegendKeysCache -> LegendKeysCache -> [LegendKeysCache])
-> (LegendKeysCache -> LegendKeysCache -> [LegendKeysCache])
-> (LegendKeysCache
    -> LegendKeysCache -> LegendKeysCache -> [LegendKeysCache])
-> Enum LegendKeysCache
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: LegendKeysCache -> LegendKeysCache
succ :: LegendKeysCache -> LegendKeysCache
$cpred :: LegendKeysCache -> LegendKeysCache
pred :: LegendKeysCache -> LegendKeysCache
$ctoEnum :: Int -> LegendKeysCache
toEnum :: Int -> LegendKeysCache
$cfromEnum :: LegendKeysCache -> Int
fromEnum :: LegendKeysCache -> Int
$cenumFrom :: LegendKeysCache -> [LegendKeysCache]
enumFrom :: LegendKeysCache -> [LegendKeysCache]
$cenumFromThen :: LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
enumFromThen :: LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
$cenumFromTo :: LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
enumFromTo :: LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
$cenumFromThenTo :: LegendKeysCache
-> LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
enumFromThenTo :: LegendKeysCache
-> LegendKeysCache -> LegendKeysCache -> [LegendKeysCache]
Enum, LegendKeysCache -> LegendKeysCache -> Bool
(LegendKeysCache -> LegendKeysCache -> Bool)
-> (LegendKeysCache -> LegendKeysCache -> Bool)
-> Eq LegendKeysCache
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LegendKeysCache -> LegendKeysCache -> Bool
== :: LegendKeysCache -> LegendKeysCache -> Bool
$c/= :: LegendKeysCache -> LegendKeysCache -> Bool
/= :: LegendKeysCache -> LegendKeysCache -> Bool
Eq, Int -> LegendKeysCache -> ShowS
[LegendKeysCache] -> ShowS
LegendKeysCache -> String
(Int -> LegendKeysCache -> ShowS)
-> (LegendKeysCache -> String)
-> ([LegendKeysCache] -> ShowS)
-> Show LegendKeysCache
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LegendKeysCache -> ShowS
showsPrec :: Int -> LegendKeysCache -> ShowS
$cshow :: LegendKeysCache -> String
show :: LegendKeysCache -> String
$cshowList :: [LegendKeysCache] -> ShowS
showList :: [LegendKeysCache] -> ShowS
Show)

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

instance Default LegendKeysCache where
  def :: LegendKeysCache
def = LegendKeysCache
LegendKeysAdd

instance Pretty LegendKeysCache where
  pretty :: forall ann. LegendKeysCache -> Doc ann
pretty = \case
    LegendKeysCache
LegendKeysAdd -> Doc ann
"add"
    LegendKeysCache
LegendKeysClear -> Doc ann
"clear"
    LegendKeysCache
LegendKeysOff -> Doc ann
"off"
    LegendKeysCache
LegendKeysWrite -> Doc ann
"write"

parseLegendKeysCache :: (MonadFail m) => m Text -> m LegendKeysCache
parseLegendKeysCache :: forall (m :: Type -> Type).
MonadFail m =>
m Text -> m LegendKeysCache
parseLegendKeysCache = (m Text -> (Text -> m LegendKeysCache) -> m LegendKeysCache
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
>>= Text -> (Bool, [Text]) -> Text -> m LegendKeysCache
forall a (m :: Type -> Type).
(Bounded a, Enum a, MonadFail m, Pretty a) =>
Text -> (Bool, [Text]) -> Text -> m a
Utils.inversePrettyFail Text
"legend-key-cache" (Bool, [Text])
forall a. IsString a => (Bool, [a])
lksMeta)
{-# INLINEABLE parseLegendKeysCache #-}

-- | Available 'LegendKeysCache' strings.
lksMeta :: (IsString a) => Tuple2 Bool (List a)
lksMeta :: forall a. IsString a => (Bool, [a])
lksMeta = (Bool
True, [a
"add", a
"clear", a
"write"])