module Hydra.Network.Ouroboros.VersionedProtocol where

import Hydra.Prelude

import Codec.CBOR.Term qualified as CBOR
import Data.Text qualified as T
import GHC.Natural (naturalFromInteger, naturalToInteger)
import GHC.Num (integerToInt)
import Hydra.Network (Host (..))
import Hydra.Network.Message (HydraVersionedProtocolNumber (..))
import Network.TypedProtocol.Pipelined ()
import Ouroboros.Network.CodecCBORTerm (CodecCBORTerm (..))
import Ouroboros.Network.Protocol.Handshake.Codec (VersionDataCodec, cborTermVersionDataCodec)
import Ouroboros.Network.Protocol.Handshake.Version (Accept (..), Acceptable, Queryable, acceptableVersion, queryVersion)

hydraVersionedProtocolCodec :: CodecCBORTerm (String, Maybe Int) HydraVersionedProtocolNumber
hydraVersionedProtocolCodec :: CodecCBORTerm ([Char], Maybe Int) HydraVersionedProtocolNumber
hydraVersionedProtocolCodec = CodecCBORTerm{HydraVersionedProtocolNumber -> Term
encodeTerm :: HydraVersionedProtocolNumber -> Term
encodeTerm :: HydraVersionedProtocolNumber -> Term
encodeTerm, Term -> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
decodeTerm :: Term -> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
decodeTerm :: Term -> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
decodeTerm}
 where
  encodeTerm :: HydraVersionedProtocolNumber -> CBOR.Term
  encodeTerm :: HydraVersionedProtocolNumber -> Term
encodeTerm HydraVersionedProtocolNumber
x = Int -> Term
CBOR.TInt (Int -> Term) -> Int -> Term
forall a b. (a -> b) -> a -> b
$ Integer -> Int
integerToInt (Integer -> Int) -> (Natural -> Integer) -> Natural -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Integer
naturalToInteger (Natural -> Int) -> Natural -> Int
forall a b. (a -> b) -> a -> b
$ HydraVersionedProtocolNumber -> Natural
hydraVersionedProtocolNumber HydraVersionedProtocolNumber
x

  decodeTerm :: CBOR.Term -> Either (String, Maybe Int) HydraVersionedProtocolNumber
  decodeTerm :: Term -> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
decodeTerm (CBOR.TInt Int
x) = HydraVersionedProtocolNumber
-> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
forall a b. b -> Either a b
Right (HydraVersionedProtocolNumber
 -> Either ([Char], Maybe Int) HydraVersionedProtocolNumber)
-> HydraVersionedProtocolNumber
-> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
forall a b. (a -> b) -> a -> b
$ Natural -> HydraVersionedProtocolNumber
MkHydraVersionedProtocolNumber (Integer -> Natural
naturalFromInteger (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
x))
  decodeTerm Term
_ = ([Char], Maybe Int)
-> Either ([Char], Maybe Int) HydraVersionedProtocolNumber
forall a b. a -> Either a b
Left ([Char]
"unknown tag", Maybe Int
forall a. Maybe a
Nothing)

type HydraVersionedProtocolData :: Type
data HydraVersionedProtocolData = MkHydraVersionedProtocolData
  deriving stock (HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
(HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> Bool)
-> Eq HydraVersionedProtocolData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
== :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
$c/= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
/= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
Eq, Int -> HydraVersionedProtocolData -> ShowS
[HydraVersionedProtocolData] -> ShowS
HydraVersionedProtocolData -> [Char]
(Int -> HydraVersionedProtocolData -> ShowS)
-> (HydraVersionedProtocolData -> [Char])
-> ([HydraVersionedProtocolData] -> ShowS)
-> Show HydraVersionedProtocolData
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HydraVersionedProtocolData -> ShowS
showsPrec :: Int -> HydraVersionedProtocolData -> ShowS
$cshow :: HydraVersionedProtocolData -> [Char]
show :: HydraVersionedProtocolData -> [Char]
$cshowList :: [HydraVersionedProtocolData] -> ShowS
showList :: [HydraVersionedProtocolData] -> ShowS
Show, (forall x.
 HydraVersionedProtocolData -> Rep HydraVersionedProtocolData x)
-> (forall x.
    Rep HydraVersionedProtocolData x -> HydraVersionedProtocolData)
-> Generic HydraVersionedProtocolData
forall x.
Rep HydraVersionedProtocolData x -> HydraVersionedProtocolData
forall x.
HydraVersionedProtocolData -> Rep HydraVersionedProtocolData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
HydraVersionedProtocolData -> Rep HydraVersionedProtocolData x
from :: forall x.
HydraVersionedProtocolData -> Rep HydraVersionedProtocolData x
$cto :: forall x.
Rep HydraVersionedProtocolData x -> HydraVersionedProtocolData
to :: forall x.
Rep HydraVersionedProtocolData x -> HydraVersionedProtocolData
Generic, Eq HydraVersionedProtocolData
Eq HydraVersionedProtocolData =>
(HydraVersionedProtocolData
 -> HydraVersionedProtocolData -> Ordering)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> Bool)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> Bool)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> Bool)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> Bool)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> HydraVersionedProtocolData)
-> (HydraVersionedProtocolData
    -> HydraVersionedProtocolData -> HydraVersionedProtocolData)
-> Ord HydraVersionedProtocolData
HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
HydraVersionedProtocolData
-> HydraVersionedProtocolData -> Ordering
HydraVersionedProtocolData
-> HydraVersionedProtocolData -> HydraVersionedProtocolData
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 :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> Ordering
compare :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> Ordering
$c< :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
< :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
$c<= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
<= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
$c> :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
> :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
$c>= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
>= :: HydraVersionedProtocolData -> HydraVersionedProtocolData -> Bool
$cmax :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> HydraVersionedProtocolData
max :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> HydraVersionedProtocolData
$cmin :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> HydraVersionedProtocolData
min :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> HydraVersionedProtocolData
Ord)

instance Acceptable HydraVersionedProtocolData where
  acceptableVersion :: HydraVersionedProtocolData
-> HydraVersionedProtocolData -> Accept HydraVersionedProtocolData
acceptableVersion
    HydraVersionedProtocolData
MkHydraVersionedProtocolData
    HydraVersionedProtocolData
MkHydraVersionedProtocolData = HydraVersionedProtocolData -> Accept HydraVersionedProtocolData
forall vData. vData -> Accept vData
Accept HydraVersionedProtocolData
MkHydraVersionedProtocolData

instance Queryable HydraVersionedProtocolData where
  queryVersion :: HydraVersionedProtocolData -> Bool
queryVersion HydraVersionedProtocolData
MkHydraVersionedProtocolData = Bool
False

hydraVersionedProtocolDataCodec ::
  VersionDataCodec
    CBOR.Term
    HydraVersionedProtocolNumber
    HydraVersionedProtocolData
hydraVersionedProtocolDataCodec :: VersionDataCodec
  Term HydraVersionedProtocolNumber HydraVersionedProtocolData
hydraVersionedProtocolDataCodec =
  (HydraVersionedProtocolNumber
 -> CodecCBORTerm Text HydraVersionedProtocolData)
-> VersionDataCodec
     Term HydraVersionedProtocolNumber HydraVersionedProtocolData
forall vNumber vData.
(vNumber -> CodecCBORTerm Text vData)
-> VersionDataCodec Term vNumber vData
cborTermVersionDataCodec
    (CodecCBORTerm Text HydraVersionedProtocolData
-> HydraVersionedProtocolNumber
-> CodecCBORTerm Text HydraVersionedProtocolData
forall a b. a -> b -> a
const CodecCBORTerm{HydraVersionedProtocolData -> Term
encodeTerm :: HydraVersionedProtocolData -> Term
encodeTerm :: HydraVersionedProtocolData -> Term
encodeTerm, Term -> Either Text HydraVersionedProtocolData
decodeTerm :: Term -> Either Text HydraVersionedProtocolData
decodeTerm :: Term -> Either Text HydraVersionedProtocolData
decodeTerm})
 where
  encodeTerm :: HydraVersionedProtocolData -> CBOR.Term
  encodeTerm :: HydraVersionedProtocolData -> Term
encodeTerm HydraVersionedProtocolData
MkHydraVersionedProtocolData = Term
CBOR.TNull

  decodeTerm :: CBOR.Term -> Either Text HydraVersionedProtocolData
  decodeTerm :: Term -> Either Text HydraVersionedProtocolData
decodeTerm Term
CBOR.TNull = HydraVersionedProtocolData
-> Either Text HydraVersionedProtocolData
forall a b. b -> Either a b
Right HydraVersionedProtocolData
MkHydraVersionedProtocolData
  decodeTerm Term
t = Text -> Either Text HydraVersionedProtocolData
forall a b. a -> Either a b
Left (Text -> Either Text HydraVersionedProtocolData)
-> Text -> Either Text HydraVersionedProtocolData
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ [Char]
"unexpected term: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Term -> [Char]
forall b a. (Show a, IsString b) => a -> b
show Term
t

type HydraNetworkConfig :: Type
data HydraNetworkConfig = HydraNetworkConfig
  { HydraNetworkConfig -> HydraVersionedProtocolNumber
protocolVersion :: HydraVersionedProtocolNumber
  , HydraNetworkConfig -> Host
localHost :: Host
  , HydraNetworkConfig -> [Host]
remoteHosts :: [Host]
  }
  deriving stock (HydraNetworkConfig -> HydraNetworkConfig -> Bool
(HydraNetworkConfig -> HydraNetworkConfig -> Bool)
-> (HydraNetworkConfig -> HydraNetworkConfig -> Bool)
-> Eq HydraNetworkConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HydraNetworkConfig -> HydraNetworkConfig -> Bool
== :: HydraNetworkConfig -> HydraNetworkConfig -> Bool
$c/= :: HydraNetworkConfig -> HydraNetworkConfig -> Bool
/= :: HydraNetworkConfig -> HydraNetworkConfig -> Bool
Eq, Int -> HydraNetworkConfig -> ShowS
[HydraNetworkConfig] -> ShowS
HydraNetworkConfig -> [Char]
(Int -> HydraNetworkConfig -> ShowS)
-> (HydraNetworkConfig -> [Char])
-> ([HydraNetworkConfig] -> ShowS)
-> Show HydraNetworkConfig
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HydraNetworkConfig -> ShowS
showsPrec :: Int -> HydraNetworkConfig -> ShowS
$cshow :: HydraNetworkConfig -> [Char]
show :: HydraNetworkConfig -> [Char]
$cshowList :: [HydraNetworkConfig] -> ShowS
showList :: [HydraNetworkConfig] -> ShowS
Show, (forall x. HydraNetworkConfig -> Rep HydraNetworkConfig x)
-> (forall x. Rep HydraNetworkConfig x -> HydraNetworkConfig)
-> Generic HydraNetworkConfig
forall x. Rep HydraNetworkConfig x -> HydraNetworkConfig
forall x. HydraNetworkConfig -> Rep HydraNetworkConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HydraNetworkConfig -> Rep HydraNetworkConfig x
from :: forall x. HydraNetworkConfig -> Rep HydraNetworkConfig x
$cto :: forall x. Rep HydraNetworkConfig x -> HydraNetworkConfig
to :: forall x. Rep HydraNetworkConfig x -> HydraNetworkConfig
Generic)