{-# LANGUAGE DeriveGeneric #-}

module Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..)) where

import Data.Aeson
import Data.Bool (bool)
import GHC.Generics (Generic)

-- | Should this peer be advertised to other peers asking for known peers?
-- For certain peers specified by configuration it would be an appropriate
-- policy to keep them private.
--
data PeerAdvertise = DoNotAdvertisePeer
                   | DoAdvertisePeer
  deriving (PeerAdvertise -> PeerAdvertise -> Bool
(PeerAdvertise -> PeerAdvertise -> Bool)
-> (PeerAdvertise -> PeerAdvertise -> Bool) -> Eq PeerAdvertise
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PeerAdvertise -> PeerAdvertise -> Bool
== :: PeerAdvertise -> PeerAdvertise -> Bool
$c/= :: PeerAdvertise -> PeerAdvertise -> Bool
/= :: PeerAdvertise -> PeerAdvertise -> Bool
Eq, Int -> PeerAdvertise -> ShowS
[PeerAdvertise] -> ShowS
PeerAdvertise -> String
(Int -> PeerAdvertise -> ShowS)
-> (PeerAdvertise -> String)
-> ([PeerAdvertise] -> ShowS)
-> Show PeerAdvertise
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PeerAdvertise -> ShowS
showsPrec :: Int -> PeerAdvertise -> ShowS
$cshow :: PeerAdvertise -> String
show :: PeerAdvertise -> String
$cshowList :: [PeerAdvertise] -> ShowS
showList :: [PeerAdvertise] -> ShowS
Show, Eq PeerAdvertise
Eq PeerAdvertise =>
(PeerAdvertise -> PeerAdvertise -> Ordering)
-> (PeerAdvertise -> PeerAdvertise -> Bool)
-> (PeerAdvertise -> PeerAdvertise -> Bool)
-> (PeerAdvertise -> PeerAdvertise -> Bool)
-> (PeerAdvertise -> PeerAdvertise -> Bool)
-> (PeerAdvertise -> PeerAdvertise -> PeerAdvertise)
-> (PeerAdvertise -> PeerAdvertise -> PeerAdvertise)
-> Ord PeerAdvertise
PeerAdvertise -> PeerAdvertise -> Bool
PeerAdvertise -> PeerAdvertise -> Ordering
PeerAdvertise -> PeerAdvertise -> PeerAdvertise
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 :: PeerAdvertise -> PeerAdvertise -> Ordering
compare :: PeerAdvertise -> PeerAdvertise -> Ordering
$c< :: PeerAdvertise -> PeerAdvertise -> Bool
< :: PeerAdvertise -> PeerAdvertise -> Bool
$c<= :: PeerAdvertise -> PeerAdvertise -> Bool
<= :: PeerAdvertise -> PeerAdvertise -> Bool
$c> :: PeerAdvertise -> PeerAdvertise -> Bool
> :: PeerAdvertise -> PeerAdvertise -> Bool
$c>= :: PeerAdvertise -> PeerAdvertise -> Bool
>= :: PeerAdvertise -> PeerAdvertise -> Bool
$cmax :: PeerAdvertise -> PeerAdvertise -> PeerAdvertise
max :: PeerAdvertise -> PeerAdvertise -> PeerAdvertise
$cmin :: PeerAdvertise -> PeerAdvertise -> PeerAdvertise
min :: PeerAdvertise -> PeerAdvertise -> PeerAdvertise
Ord, (forall x. PeerAdvertise -> Rep PeerAdvertise x)
-> (forall x. Rep PeerAdvertise x -> PeerAdvertise)
-> Generic PeerAdvertise
forall x. Rep PeerAdvertise x -> PeerAdvertise
forall x. PeerAdvertise -> Rep PeerAdvertise x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PeerAdvertise -> Rep PeerAdvertise x
from :: forall x. PeerAdvertise -> Rep PeerAdvertise x
$cto :: forall x. Rep PeerAdvertise x -> PeerAdvertise
to :: forall x. Rep PeerAdvertise x -> PeerAdvertise
Generic)

instance FromJSON PeerAdvertise where
  parseJSON :: Value -> Parser PeerAdvertise
parseJSON = String
-> (Bool -> Parser PeerAdvertise) -> Value -> Parser PeerAdvertise
forall a. String -> (Bool -> Parser a) -> Value -> Parser a
withBool String
"PeerAdvertise" ((Bool -> Parser PeerAdvertise) -> Value -> Parser PeerAdvertise)
-> (Bool -> Parser PeerAdvertise) -> Value -> Parser PeerAdvertise
forall a b. (a -> b) -> a -> b
$
      PeerAdvertise -> Parser PeerAdvertise
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (PeerAdvertise -> Parser PeerAdvertise)
-> (Bool -> PeerAdvertise) -> Bool -> Parser PeerAdvertise
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeerAdvertise -> PeerAdvertise -> Bool -> PeerAdvertise
forall a. a -> a -> Bool -> a
bool PeerAdvertise
DoNotAdvertisePeer PeerAdvertise
DoAdvertisePeer

instance ToJSON PeerAdvertise where
  toJSON :: PeerAdvertise -> Value
toJSON PeerAdvertise
DoAdvertisePeer    = Bool -> Value
Bool Bool
True
  toJSON PeerAdvertise
DoNotAdvertisePeer = Bool -> Value
Bool Bool
False