{-# 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
      ( NetInterfaceAppIp,
        NetInterfaceAppNmCli
      ),
  )

-- | 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
(NetInterfacesToml -> NetInterfacesToml -> Bool)
-> (NetInterfacesToml -> NetInterfacesToml -> Bool)
-> Eq NetInterfacesToml
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NetInterfacesToml -> NetInterfacesToml -> Bool
== :: NetInterfacesToml -> NetInterfacesToml -> Bool
$c/= :: NetInterfacesToml -> NetInterfacesToml -> Bool
/= :: NetInterfacesToml -> NetInterfacesToml -> Bool
Eq, Int -> NetInterfacesToml -> ShowS
[NetInterfacesToml] -> ShowS
NetInterfacesToml -> String
(Int -> NetInterfacesToml -> ShowS)
-> (NetInterfacesToml -> String)
-> ([NetInterfacesToml] -> ShowS)
-> Show NetInterfacesToml
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NetInterfacesToml -> ShowS
showsPrec :: Int -> NetInterfacesToml -> ShowS
$cshow :: NetInterfacesToml -> String
show :: NetInterfacesToml -> String
$cshowList :: [NetInterfacesToml] -> ShowS
showList :: [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
      (NetInterfaceApp
 -> Text
 -> Maybe PollInterval
 -> Maybe RepeatEventToml
 -> Maybe ErrorNoteToml
 -> Maybe Timeout
 -> NetInterfacesToml)
-> Decoder NetInterfaceApp
-> Decoder
     (Text
      -> Maybe PollInterval
      -> Maybe RepeatEventToml
      -> Maybe ErrorNoteToml
      -> Maybe Timeout
      -> NetInterfacesToml)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder NetInterfaceApp -> Text -> Decoder NetInterfaceApp
forall a. Decoder a -> Text -> Decoder a
getFieldWith Decoder NetInterfaceApp
decodeNetInterfaceApp Text
"app"
      Decoder
  (Text
   -> Maybe PollInterval
   -> Maybe RepeatEventToml
   -> Maybe ErrorNoteToml
   -> Maybe Timeout
   -> NetInterfacesToml)
-> Decoder Text
-> Decoder
     (Maybe PollInterval
      -> Maybe RepeatEventToml
      -> Maybe ErrorNoteToml
      -> Maybe Timeout
      -> NetInterfacesToml)
forall a b. Decoder (a -> b) -> Decoder a -> Decoder b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Text -> Decoder Text
forall a. DecodeTOML a => Text -> Decoder a
getField Text
"device"
      Decoder
  (Maybe PollInterval
   -> Maybe RepeatEventToml
   -> Maybe ErrorNoteToml
   -> Maybe Timeout
   -> NetInterfacesToml)
-> Decoder (Maybe PollInterval)
-> Decoder
     (Maybe RepeatEventToml
      -> Maybe ErrorNoteToml -> Maybe Timeout -> NetInterfacesToml)
forall a b. Decoder (a -> b) -> Decoder a -> Decoder b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe PollInterval)
pollIntervalOptDecoder
      Decoder
  (Maybe RepeatEventToml
   -> Maybe ErrorNoteToml -> Maybe Timeout -> NetInterfacesToml)
-> Decoder (Maybe RepeatEventToml)
-> Decoder
     (Maybe ErrorNoteToml -> Maybe Timeout -> NetInterfacesToml)
forall a b. Decoder (a -> b) -> Decoder a -> Decoder b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe RepeatEventToml)
repeatEventOptDecoder
      Decoder (Maybe ErrorNoteToml -> Maybe Timeout -> NetInterfacesToml)
-> Decoder (Maybe ErrorNoteToml)
-> Decoder (Maybe Timeout -> NetInterfacesToml)
forall a b. Decoder (a -> b) -> Decoder a -> Decoder b
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe ErrorNoteToml)
errorNoteOptDecoder
      Decoder (Maybe Timeout -> NetInterfacesToml)
-> Decoder (Maybe Timeout) -> Decoder NetInterfacesToml
forall a b. Decoder (a -> b) -> Decoder a -> Decoder b
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 =
  Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder Decoder Text
-> (Text -> Decoder NetInterfaceApp) -> Decoder NetInterfaceApp
forall a b. Decoder a -> (a -> Decoder b) -> Decoder b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Text
"nmcli" -> NetInterfaceApp -> Decoder NetInterfaceApp
forall a. a -> Decoder a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure NetInterfaceApp
NetInterfaceAppNmCli
    Text
"ip" -> NetInterfaceApp -> Decoder NetInterfaceApp
forall a. a -> Decoder a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure NetInterfaceApp
NetInterfaceAppIp
    Text
bad ->
      String -> Decoder NetInterfaceApp
forall a. String -> Decoder a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail
        (String -> Decoder NetInterfaceApp)
-> String -> Decoder NetInterfaceApp
forall a b. (a -> b) -> a -> b
$ Text -> String
unpackText
        (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
          [ Text
"Unexpected net-interface app: ",
            Text
bad,
            Text
". Expected one of <nmcli | ip>."
          ]