{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
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 (..))
data NetInterfacesToml = MkNetInterfacesToml
{
NetInterfacesToml -> NetInterfaceApp
app :: NetInterfaceApp,
NetInterfacesToml -> Text
deviceName :: Text,
NetInterfacesToml -> Maybe PollInterval
pollInterval :: Maybe PollInterval,
NetInterfacesToml -> Maybe RepeatEventToml
repeatEvent :: Maybe RepeatEventToml,
NetInterfacesToml -> Maybe ErrorNoteToml
errorNote :: Maybe ErrorNoteToml,
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
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 #-}