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

-- | This module provides toml configuration for the network
-- connectivity service.
module Navi.Services.Network.NetInterfaces.Toml
  ( NetInterfacesToml (..),
  )
where

import Navi.Data.NaviNote (Timeout, timeoutOptDecoder)
import Navi.Data.PollInterval (PollInterval (..), pollIntervalOptDecoder)
import Navi.Event.Toml
  ( ErrorNoteToml,
    RepeatEventToml,
    errorNoteOptDecoder,
    repeatEventOptDecoder,
  )
import Navi.Prelude
import Pythia.Services.NetInterface (NetInterfaceApp (..))

-- | TOML for the network connectivity service.
data NetInterfacesToml = MkNetInterfacesToml
  { -- | Determines how we should query the system for network information.
    NetInterfacesToml -> NetInterfaceApp
app :: NetInterfaceApp,
    -- | The name of the network device. For \"standard\" formats like
    -- ifconfig or NetworkManager, this might be something like
    -- wlp0s20f3 or enp0s31f6.
    NetInterfacesToml -> Text
deviceName :: Text,
    -- | The poll interval.
    NetInterfacesToml -> Maybe PollInterval
pollInterval :: Maybe PollInterval,
    -- | Determines how we treat repeat alerts.
    NetInterfacesToml -> Maybe RepeatEventToml
repeatEvent :: Maybe RepeatEventToml,
    -- | Determines how we handle errors.
    NetInterfacesToml -> Maybe ErrorNoteToml
errorNote :: Maybe ErrorNoteToml,
    -- | The timeout for this alert.
    NetInterfacesToml -> Maybe Timeout
mTimeout :: Maybe Timeout
  }
  deriving stock (NetInterfacesToml -> NetInterfacesToml -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NetInterfacesToml -> NetInterfacesToml -> Bool
$c/= :: NetInterfacesToml -> NetInterfacesToml -> Bool
== :: NetInterfacesToml -> NetInterfacesToml -> Bool
$c== :: NetInterfacesToml -> NetInterfacesToml -> Bool
Eq, Int -> NetInterfacesToml -> ShowS
[NetInterfacesToml] -> ShowS
NetInterfacesToml -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NetInterfacesToml] -> ShowS
$cshowList :: [NetInterfacesToml] -> ShowS
show :: NetInterfacesToml -> String
$cshow :: NetInterfacesToml -> String
showsPrec :: Int -> NetInterfacesToml -> ShowS
$cshowsPrec :: Int -> NetInterfacesToml -> ShowS
Show)

makeFieldLabelsNoPrefix ''NetInterfacesToml

-- | @since 0.1
instance DecodeTOML NetInterfacesToml where
  tomlDecoder :: Decoder NetInterfacesToml
tomlDecoder =
    NetInterfaceApp
-> Text
-> Maybe PollInterval
-> Maybe RepeatEventToml
-> Maybe ErrorNoteToml
-> Maybe Timeout
-> NetInterfacesToml
MkNetInterfacesToml
      forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Decoder a -> Text -> Decoder a
getFieldWith Decoder NetInterfaceApp
decodeNetInterfaceApp Text
"app"
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> forall a. DecodeTOML a => Text -> Decoder a
getField Text
"device"
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe PollInterval)
pollIntervalOptDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe RepeatEventToml)
repeatEventOptDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe ErrorNoteToml)
errorNoteOptDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe Timeout)
timeoutOptDecoder

decodeNetInterfaceApp :: Decoder NetInterfaceApp
decodeNetInterfaceApp :: Decoder NetInterfaceApp
decodeNetInterfaceApp =
  forall a. DecodeTOML a => Decoder a
tomlDecoder forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Text
"nmcli" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure NetInterfaceApp
NetInterfaceAppNmCli
    Text
"ip" -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure NetInterfaceApp
NetInterfaceAppIp
    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 net-interface app: ",
              Text
bad,
              Text
". Expected one of <nmcli | ip>."
            ]
{-# INLINEABLE decodeNetInterfaceApp #-}