-- | Provides utilities.
--
-- @since 0.1
module Navi.Utils
  ( -- * TOML

    -- ** Decoding utils
    getFieldOptArrayOf,

    -- ** Specific decoders
    commandDecoder,
    urgencyLevelOptDecoder,
  )
where

import DBus.Notify (UrgencyLevel (..))
import Navi.Prelude
import Pythia.Data.Command (Command (..))

-- | Decodes an optional list. This is morally
--
-- @
-- getFieldOptWith (getArrayOf tomlDecoder) :: Text -> Decoder (Maybe [a])
-- @
--
-- except we return an empty list when the key is missing rather than
-- 'Nothing'.
--
-- @since 0.1
getFieldOptArrayOf :: DecodeTOML a => Text -> Decoder [a]
getFieldOptArrayOf :: forall a. DecodeTOML a => Text -> Decoder [a]
getFieldOptArrayOf =
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe [])
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith (forall a. Decoder a -> Decoder [a]
getArrayOf forall a. DecodeTOML a => Decoder a
tomlDecoder)

-- | TOML decoder for optional 'UrgencyLevel' with field name "urgency".
--
-- @since 0.1
urgencyLevelOptDecoder :: Decoder (Maybe UrgencyLevel)
urgencyLevelOptDecoder :: Decoder (Maybe UrgencyLevel)
urgencyLevelOptDecoder = forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith Decoder UrgencyLevel
urgencyLevelDecoder Text
"urgency"

urgencyLevelDecoder :: Decoder UrgencyLevel
urgencyLevelDecoder :: Decoder UrgencyLevel
urgencyLevelDecoder = do
  Text
t <- forall a. DecodeTOML a => Decoder a
tomlDecoder
  case Text
t of
    Text
"low" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure UrgencyLevel
Low
    Text
"normal" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure UrgencyLevel
Normal
    Text
"critical" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure UrgencyLevel
Critical
    Text
bad -> forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ Text -> String
unpack forall a b. (a -> b) -> a -> b
$ Text
"Invalid value: " forall a. Semigroup a => a -> a -> a
<> Text
bad
{-# INLINEABLE urgencyLevelDecoder #-}

-- | TOML decoder for 'Command' with field name "command".
--
-- @since 0.1
commandDecoder :: Decoder Command
commandDecoder :: Decoder Command
commandDecoder = Text -> Command
MkCommand forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. DecodeTOML a => Text -> Decoder a
getField Text
"command"