{-# LANGUAGE UndecidableInstances #-}

-- | Provides the 'PollInterval' type.
module Shrun.Configuration.Data.CommandLogging.PollInterval
  ( PollInterval (..),
    parsePollInterval,
  )
where

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

-- | Represents how often to poll for command logs, in microseconds.
-- Zero is interpreted as infinite i.e. limited only by the CPU.
newtype PollInterval = MkPollInterval {PollInterval -> Natural
unPollInterval :: Natural}
  deriving stock (PollInterval -> PollInterval -> Bool
(PollInterval -> PollInterval -> Bool)
-> (PollInterval -> PollInterval -> Bool) -> Eq PollInterval
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollInterval -> PollInterval -> Bool
== :: PollInterval -> PollInterval -> Bool
$c/= :: PollInterval -> PollInterval -> Bool
/= :: PollInterval -> PollInterval -> Bool
Eq, Eq PollInterval
Eq PollInterval =>
(PollInterval -> PollInterval -> Ordering)
-> (PollInterval -> PollInterval -> Bool)
-> (PollInterval -> PollInterval -> Bool)
-> (PollInterval -> PollInterval -> Bool)
-> (PollInterval -> PollInterval -> Bool)
-> (PollInterval -> PollInterval -> PollInterval)
-> (PollInterval -> PollInterval -> PollInterval)
-> Ord PollInterval
PollInterval -> PollInterval -> Bool
PollInterval -> PollInterval -> Ordering
PollInterval -> PollInterval -> PollInterval
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PollInterval -> PollInterval -> Ordering
compare :: PollInterval -> PollInterval -> Ordering
$c< :: PollInterval -> PollInterval -> Bool
< :: PollInterval -> PollInterval -> Bool
$c<= :: PollInterval -> PollInterval -> Bool
<= :: PollInterval -> PollInterval -> Bool
$c> :: PollInterval -> PollInterval -> Bool
> :: PollInterval -> PollInterval -> Bool
$c>= :: PollInterval -> PollInterval -> Bool
>= :: PollInterval -> PollInterval -> Bool
$cmax :: PollInterval -> PollInterval -> PollInterval
max :: PollInterval -> PollInterval -> PollInterval
$cmin :: PollInterval -> PollInterval -> PollInterval
min :: PollInterval -> PollInterval -> PollInterval
Ord, Int -> PollInterval -> ShowS
[PollInterval] -> ShowS
PollInterval -> String
(Int -> PollInterval -> ShowS)
-> (PollInterval -> String)
-> ([PollInterval] -> ShowS)
-> Show PollInterval
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollInterval -> ShowS
showsPrec :: Int -> PollInterval -> ShowS
$cshow :: PollInterval -> String
show :: PollInterval -> String
$cshowList :: [PollInterval] -> ShowS
showList :: [PollInterval] -> ShowS
Show)
  deriving (Integer -> PollInterval
PollInterval -> PollInterval
PollInterval -> PollInterval -> PollInterval
(PollInterval -> PollInterval -> PollInterval)
-> (PollInterval -> PollInterval -> PollInterval)
-> (PollInterval -> PollInterval -> PollInterval)
-> (PollInterval -> PollInterval)
-> (PollInterval -> PollInterval)
-> (PollInterval -> PollInterval)
-> (Integer -> PollInterval)
-> Num PollInterval
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: PollInterval -> PollInterval -> PollInterval
+ :: PollInterval -> PollInterval -> PollInterval
$c- :: PollInterval -> PollInterval -> PollInterval
- :: PollInterval -> PollInterval -> PollInterval
$c* :: PollInterval -> PollInterval -> PollInterval
* :: PollInterval -> PollInterval -> PollInterval
$cnegate :: PollInterval -> PollInterval
negate :: PollInterval -> PollInterval
$cabs :: PollInterval -> PollInterval
abs :: PollInterval -> PollInterval
$csignum :: PollInterval -> PollInterval
signum :: PollInterval -> PollInterval
$cfromInteger :: Integer -> PollInterval
fromInteger :: Integer -> PollInterval
Num) via Natural

instance
  (k ~ An_Iso, a ~ Natural, b ~ Natural) =>
  LabelOptic "unPollInterval" k PollInterval PollInterval a b
  where
  labelOptic :: Optic k NoIx PollInterval PollInterval a b
labelOptic = (PollInterval -> a)
-> (b -> PollInterval) -> Iso PollInterval PollInterval a b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(MkPollInterval Natural
x) -> a
Natural
x) b -> PollInterval
Natural -> PollInterval
MkPollInterval
  {-# INLINE labelOptic #-}

instance DecodeTOML PollInterval where
  tomlDecoder :: Decoder PollInterval
tomlDecoder = Natural -> PollInterval
MkPollInterval (Natural -> PollInterval)
-> Decoder Natural -> Decoder PollInterval
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder Natural
forall a. DecodeTOML a => Decoder a
tomlDecoder

parsePollInterval :: (Functor m) => m Natural -> m PollInterval
parsePollInterval :: forall (m :: Type -> Type).
Functor m =>
m Natural -> m PollInterval
parsePollInterval m Natural
getNat = Natural -> PollInterval
MkPollInterval (Natural -> PollInterval) -> m Natural -> m PollInterval
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> m Natural
getNat

instance Default PollInterval where
  def :: PollInterval
def = Natural -> PollInterval
MkPollInterval Natural
10_000