{-# LANGUAGE CPP #-}

module Shrun.Configuration.Default
  ( Default (..),
    fromMaybe,
    (<.>),
  )
where

import Shrun.Prelude hiding (fromMaybe)

-- | For types with a default value. In general, instances should be "simple"
-- i.e. no instances for aggregate TTG types (e.g. FileLogging) except for
-- CLI args, as complexity jumps quickly.
--
-- For the most part, types should only have instances for one of
-- Default xor (Semigroup/Monoid or Alternative) i.e. if types /do/ support
-- some sort of algebra, the latter is preferred. Default exists when
-- no algebra is sensible.
class Default a where
  def :: a

fromMaybe :: (Default a) => Maybe a -> a
fromMaybe :: forall a. Default a => Maybe a -> a
fromMaybe (Just a
x) = a
x
fromMaybe Maybe a
Nothing = a
forall a. Default a => a
def

-- | Like '(<>?)' except we extract a result via 'fromDefault'.
(<.>) :: (Default a) => Maybe a -> Maybe a -> a
Maybe a
x <.> :: forall a. Default a => Maybe a -> Maybe a -> a
<.> Maybe a
y = Maybe a -> a
forall a. Default a => Maybe a -> a
fromMaybe (Maybe a
x Maybe a -> Maybe a -> Maybe a
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: Type -> Type) a. Alternative f => f a -> f a -> f a
<|> Maybe a
y)

infixr 6 <.>

#if OSX
instance Default NotifySystem where
  def = NotifySystemAppleScript
#else
instance Default NotifySystem where
  def :: NotifySystem
def = NotifySystem
NotifySystemDBus
#endif

instance Default NotifyTimeout where
  def :: NotifyTimeout
def = Int -> NotifyTimeout
NotifyTimeoutMillis Int
10_000