{-# OPTIONS_GHC -Wno-orphans #-}

-- | Orphan 'ToCBOR' / 'FromCBOR' instances for third-party types that appear
-- on the CBOR-encoded API surface.
module Hydra.CBOR.Orphans () where

import Hydra.Prelude

import Codec.CBOR.JSON (decodeValue, encodeValue)
import Data.Aeson qualified as Aeson
import Network.Socket (PortNumber)

-- | 'Aeson.Value' is CBOR-encoded using the standard JSON-in-CBOR mapping
-- from cborg-json. This is used to serve the pre-rendered configuration
-- document at GET /config; 'FromCBOR' is its client-side decode counterpart.
--
-- NOTE: Non-integral JSON numbers round-trip through 'Double', which may lose
-- precision; integers are exact.
instance ToCBOR Aeson.Value where
  toCBOR :: Value -> Encoding
toCBOR = Value -> Encoding
encodeValue

instance FromCBOR Aeson.Value where
  fromCBOR :: forall s. Decoder s Value
fromCBOR = Bool -> Decoder s Value
forall s. Bool -> Decoder s Value
decodeValue Bool
False

instance ToCBOR PortNumber where
  toCBOR :: PortNumber -> Encoding
toCBOR = Word16 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word16 -> Encoding)
-> (PortNumber -> Word16) -> PortNumber -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PortNumber -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral :: PortNumber -> Word16)

instance FromCBOR PortNumber where
  fromCBOR :: forall s. Decoder s PortNumber
fromCBOR = (Word16 -> PortNumber
forall a b. (Integral a, Num b) => a -> b
fromIntegral :: Word16 -> PortNumber) (Word16 -> PortNumber) -> Decoder s Word16 -> Decoder s PortNumber
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word16
forall s. Decoder s Word16
forall a s. FromCBOR a => Decoder s a
fromCBOR