{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cachix.Types.ContentTypes
  ( XNixNar,
    XNixNarInfo,
    XNixCacheInfo,
  )
where

import qualified Cachix.Types.ByteStringStreaming as ByteStringStreaming
import qualified Cachix.Types.NarInfo as NarInfo
import qualified Cachix.Types.NixCacheInfo as NixCacheInfo
import qualified Data.ByteString.Lazy as BSL
import Data.Coerce (coerce)
import qualified Network.HTTP.Media as M
import Protolude
import Servant.API

data XNixNar deriving (Typeable)

data XNixNarInfo deriving (Typeable)

data XNixCacheInfo deriving (Typeable)

instance Accept XNixCacheInfo where
  contentType :: Proxy XNixCacheInfo -> MediaType
contentType Proxy XNixCacheInfo
_ = ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"octet-stream"

instance Accept XNixNarInfo where
  contentType :: Proxy XNixNarInfo -> MediaType
contentType Proxy XNixNarInfo
_ = ByteString
"text" ByteString -> ByteString -> MediaType
M.// ByteString
"x-nix-narinfo"

instance Accept XNixNar where
  contentType :: Proxy XNixNar -> MediaType
contentType Proxy XNixNar
_ = ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"octet-stream"

instance MimeUnrender XNixCacheInfo NixCacheInfo.NixCacheInfo where
  mimeUnrender :: Proxy XNixCacheInfo -> ByteString -> Either String NixCacheInfo
mimeUnrender Proxy XNixCacheInfo
_ ByteString
_ = forall a b. a -> Either a b
Left String
"TODO"

instance MimeUnrender XNixNarInfo NarInfo.CachixNarInfo where
  mimeUnrender :: Proxy XNixNarInfo -> ByteString -> Either String CachixNarInfo
mimeUnrender Proxy XNixNarInfo
_ ByteString
_ = forall a b. a -> Either a b
Left String
"TODO"

instance MimeRender XNixNar ByteStringStreaming.ByteStringStreaming where
  mimeRender :: Proxy XNixNar -> ByteStringStreaming -> ByteString
mimeRender Proxy XNixNar
_ = ByteString -> ByteString
BSL.fromStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce

instance MimeUnrender XNixNar ByteStringStreaming.ByteStringStreaming where
  mimeUnrender :: Proxy XNixNar -> ByteString -> Either String ByteStringStreaming
mimeUnrender Proxy XNixNar
_ = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.toStrict

instance MimeUnrender XNixNar ByteStringStreaming.LazyByteStringStreaming where
  mimeUnrender :: Proxy XNixNar
-> ByteString -> Either String LazyByteStringStreaming
mimeUnrender Proxy XNixNar
_ = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce

instance MimeRender XNixNar ByteStringStreaming.LazyByteStringStreaming where
  mimeRender :: Proxy XNixNar -> LazyByteStringStreaming -> ByteString
mimeRender Proxy XNixNar
_ = coerce :: forall a b. Coercible a b => a -> b
coerce

instance MimeRender PlainText ByteStringStreaming.ByteStringStreaming where
  mimeRender :: Proxy PlainText -> ByteStringStreaming -> ByteString
mimeRender Proxy PlainText
_ = ByteString -> ByteString
BSL.fromStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce

instance MimeUnrender PlainText ByteStringStreaming.ByteStringStreaming where
  mimeUnrender :: Proxy PlainText -> ByteString -> Either String ByteStringStreaming
mimeUnrender Proxy PlainText
_ = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.toStrict