{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

-- | This modules provides toml configuration related to events.
module Navi.Event.Toml
  ( RepeatEventToml (..),
    _NoRepeatsToml,
    _AllowRepeatsToml,
    repeatEventOptDecoder,
    repeatEventTomlToVal,
    mRepeatEventTomlToVal,
    ErrorNoteToml (..),
    _NoErrNoteToml,
    _ErrNoteAllowRepeatsToml,
    _ErrNoteNoRepeatsToml,
    errorNoteOptDecoder,
    errorNoteTomlToVal,
    mErrorNoteTomlToVal,
  )
where

import Navi.Event.Types (ErrorNote (..), RepeatEvent (..))
import Navi.Prelude

-- | TOML for 'RepeatEvent'.
data RepeatEventToml
  = NoRepeatsToml
  | AllowRepeatsToml
  deriving stock (RepeatEventToml -> RepeatEventToml -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RepeatEventToml -> RepeatEventToml -> Bool
$c/= :: RepeatEventToml -> RepeatEventToml -> Bool
== :: RepeatEventToml -> RepeatEventToml -> Bool
$c== :: RepeatEventToml -> RepeatEventToml -> Bool
Eq, Int -> RepeatEventToml -> ShowS
[RepeatEventToml] -> ShowS
RepeatEventToml -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RepeatEventToml] -> ShowS
$cshowList :: [RepeatEventToml] -> ShowS
show :: RepeatEventToml -> String
$cshow :: RepeatEventToml -> String
showsPrec :: Int -> RepeatEventToml -> ShowS
$cshowsPrec :: Int -> RepeatEventToml -> ShowS
Show)

makePrisms ''RepeatEventToml

-- | @since 0.1
instance DecodeTOML RepeatEventToml where
  tomlDecoder :: Decoder RepeatEventToml
tomlDecoder =
    forall a. DecodeTOML a => Decoder a
tomlDecoder forall (f :: Type -> Type) a b. Functor f => f a -> (a -> b) -> f b
<&> \Bool
b ->
      if Bool
b
        then RepeatEventToml
AllowRepeatsToml
        else RepeatEventToml
NoRepeatsToml

-- | TOML decoder for optional 'RepeatEventToml' with field name
-- "repeat-events".
--
-- @since 0.1
repeatEventOptDecoder :: Decoder (Maybe RepeatEventToml)
repeatEventOptDecoder :: Decoder (Maybe RepeatEventToml)
repeatEventOptDecoder = forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"repeat-events"

-- | Constructs a mutable 'RepeatEvent' from 'RepeatEventToml'.
repeatEventTomlToVal :: MonadIORef m => RepeatEventToml -> m (RepeatEvent a)
repeatEventTomlToVal :: forall (m :: Type -> Type) a.
MonadIORef m =>
RepeatEventToml -> m (RepeatEvent a)
repeatEventTomlToVal RepeatEventToml
AllowRepeatsToml = forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a. RepeatEvent a
AllowRepeats
repeatEventTomlToVal RepeatEventToml
NoRepeatsToml = forall a. IORef (Maybe a) -> RepeatEvent a
NoRepeats forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: Type -> Type) a.
(MonadIORef m, HasCallStack) =>
a -> m (IORef a)
newIORef forall a. Maybe a
Nothing
{-# INLINEABLE repeatEventTomlToVal #-}

-- | Constructs a mutable 'RepeatEvent' from 'RepeatEventToml'. If none is
-- provided, defaults to 'NoRepeatsToml', i.e., no repeats.
mRepeatEventTomlToVal :: MonadIORef m => Maybe RepeatEventToml -> m (RepeatEvent a)
mRepeatEventTomlToVal :: forall (m :: Type -> Type) a.
MonadIORef m =>
Maybe RepeatEventToml -> m (RepeatEvent a)
mRepeatEventTomlToVal Maybe RepeatEventToml
Nothing = forall (m :: Type -> Type) a.
MonadIORef m =>
RepeatEventToml -> m (RepeatEvent a)
repeatEventTomlToVal RepeatEventToml
NoRepeatsToml
mRepeatEventTomlToVal (Just RepeatEventToml
t) = forall (m :: Type -> Type) a.
MonadIORef m =>
RepeatEventToml -> m (RepeatEvent a)
repeatEventTomlToVal RepeatEventToml
t
{-# INLINEABLE mRepeatEventTomlToVal #-}

-- | TOML for 'ErrorNote'.
data ErrorNoteToml
  = NoErrNoteToml
  | ErrNoteAllowRepeatsToml
  | ErrNoteNoRepeatsToml
  deriving stock (ErrorNoteToml -> ErrorNoteToml -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ErrorNoteToml -> ErrorNoteToml -> Bool
$c/= :: ErrorNoteToml -> ErrorNoteToml -> Bool
== :: ErrorNoteToml -> ErrorNoteToml -> Bool
$c== :: ErrorNoteToml -> ErrorNoteToml -> Bool
Eq, Int -> ErrorNoteToml -> ShowS
[ErrorNoteToml] -> ShowS
ErrorNoteToml -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ErrorNoteToml] -> ShowS
$cshowList :: [ErrorNoteToml] -> ShowS
show :: ErrorNoteToml -> String
$cshow :: ErrorNoteToml -> String
showsPrec :: Int -> ErrorNoteToml -> ShowS
$cshowsPrec :: Int -> ErrorNoteToml -> ShowS
Show)

makePrisms ''ErrorNoteToml

-- | @since 0.1
instance DecodeTOML ErrorNoteToml where
  tomlDecoder :: Decoder ErrorNoteToml
tomlDecoder =
    forall a. DecodeTOML a => Decoder a
tomlDecoder
      forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Text
"none" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ErrorNoteToml
NoErrNoteToml
        Text
"repeats" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ErrorNoteToml
ErrNoteAllowRepeatsToml
        Text
"no-repeats" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ErrorNoteToml
ErrNoteNoRepeatsToml
        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] -> Text
concat
                [ Text
"Unexpected error-events string: ",
                  Text
bad,
                  Text
". Expected one of <none | repeats | no-repeats>."
                ]

-- | TOML decoder for optional 'ErrorNoteToml' with field name
-- "error-events".
--
-- @since 0.1
errorNoteOptDecoder :: Decoder (Maybe ErrorNoteToml)
errorNoteOptDecoder :: Decoder (Maybe ErrorNoteToml)
errorNoteOptDecoder = forall a. Decoder a -> Text -> Decoder (Maybe a)
getFieldOptWith forall a. DecodeTOML a => Decoder a
tomlDecoder Text
"error-events"

-- | Constructs a mutable 'ErrorNote' from 'ErrorNoteToml'.
errorNoteTomlToVal :: MonadIORef m => ErrorNoteToml -> m ErrorNote
errorNoteTomlToVal :: forall (m :: Type -> Type).
MonadIORef m =>
ErrorNoteToml -> m ErrorNote
errorNoteTomlToVal ErrorNoteToml
NoErrNoteToml = forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ErrorNote
NoErrNote
errorNoteTomlToVal ErrorNoteToml
ErrNoteAllowRepeatsToml = forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ RepeatEvent () -> ErrorNote
AllowErrNote forall a. RepeatEvent a
AllowRepeats
errorNoteTomlToVal ErrorNoteToml
ErrNoteNoRepeatsToml = RepeatEvent () -> ErrorNote
AllowErrNote forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IORef (Maybe a) -> RepeatEvent a
NoRepeats forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: Type -> Type) a.
(MonadIORef m, HasCallStack) =>
a -> m (IORef a)
newIORef forall a. Maybe a
Nothing
{-# INLINEABLE errorNoteTomlToVal #-}

-- | Constructs a mutable 'ErrorNote' from 'ErrorNoteToml'. If none is
-- provided, defaults to 'ErrNoteNoRepeatsToml', i.e., we /do/ send
-- notifications for errors, but we do not send repeats.
mErrorNoteTomlToVal :: MonadIORef m => Maybe ErrorNoteToml -> m ErrorNote
mErrorNoteTomlToVal :: forall (m :: Type -> Type).
MonadIORef m =>
Maybe ErrorNoteToml -> m ErrorNote
mErrorNoteTomlToVal Maybe ErrorNoteToml
Nothing = forall (m :: Type -> Type).
MonadIORef m =>
ErrorNoteToml -> m ErrorNote
errorNoteTomlToVal ErrorNoteToml
ErrNoteNoRepeatsToml
mErrorNoteTomlToVal (Just ErrorNoteToml
t) = forall (m :: Type -> Type).
MonadIORef m =>
ErrorNoteToml -> m ErrorNote
errorNoteTomlToVal ErrorNoteToml
t
{-# INLINEABLE mErrorNoteTomlToVal #-}