{-# OPTIONS_HADDOCK not-home #-}
module Servant.API.Routes.Internal.Body
( Body (..)
, bodyToList
, listToBody
, AllTypeable (..)
)
where
import Data.Kind (Type)
import Data.List (nub, sort)
import Data.Typeable
import "this" Servant.API.Routes.Utils
data Body
= NoBody
| OneType TypeRep
|
ManyTypes [TypeRep]
deriving (Int -> Body -> ShowS
[Body] -> ShowS
Body -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Body] -> ShowS
$cshowList :: [Body] -> ShowS
show :: Body -> String
$cshow :: Body -> String
showsPrec :: Int -> Body -> ShowS
$cshowsPrec :: Int -> Body -> ShowS
Show)
bodyToList :: Body -> [TypeRep]
bodyToList :: Body -> [TypeRep]
bodyToList = \case
Body
NoBody -> []
OneType TypeRep
tRep -> [TypeRep
tRep]
ManyTypes [TypeRep]
tReps -> [TypeRep]
tReps
listToBody :: [TypeRep] -> Body
listToBody :: [TypeRep] -> Body
listToBody = \case
[] -> Body
NoBody
[TypeRep
tRep] -> TypeRep -> Body
OneType TypeRep
tRep
[TypeRep]
tReps -> [TypeRep] -> Body
ManyTypes [TypeRep]
tReps
instance Eq Body where
Body
NoBody == :: Body -> Body -> Bool
== Body
NoBody = Bool
True
OneType TypeRep
t1 == OneType TypeRep
t2 = TypeRep
t1 forall a. Eq a => a -> a -> Bool
== TypeRep
t2
ManyTypes [TypeRep]
ts1 == ManyTypes [TypeRep]
ts2 = forall a. Ord a => [a] -> [a]
sort (forall a. Eq a => [a] -> [a]
nub [TypeRep]
ts1) forall a. Eq a => a -> a -> Bool
== forall a. Ord a => [a] -> [a]
sort (forall a. Eq a => [a] -> [a]
nub [TypeRep]
ts2)
Body
_ == Body
_ = Bool
False
instance Semigroup Body where
Body
NoBody <> :: Body -> Body -> Body
<> Body
x = Body
x
Body
x <> Body
NoBody = Body
x
OneType TypeRep
t1 <> OneType TypeRep
t2 = [TypeRep] -> Body
ManyTypes [TypeRep
t1, TypeRep
t2]
OneType TypeRep
t <> ManyTypes [TypeRep]
ts = [TypeRep] -> Body
ManyTypes (TypeRep
t forall a. a -> [a] -> [a]
: [TypeRep]
ts)
ManyTypes [TypeRep]
ts <> OneType TypeRep
t = [TypeRep] -> Body
ManyTypes (TypeRep
t forall a. a -> [a] -> [a]
: [TypeRep]
ts)
ManyTypes [TypeRep]
ts1 <> ManyTypes [TypeRep]
ts2 = [TypeRep] -> Body
ManyTypes ([TypeRep]
ts1 forall a. Semigroup a => a -> a -> a
<> [TypeRep]
ts2)
instance Monoid Body where
mempty :: Body
mempty = Body
NoBody
class AllTypeable (as :: [Type]) where
typeReps :: [TypeRep]
instance (Typeable a, Typeable b) => AllTypeable '[a, b] where
typeReps :: [TypeRep]
typeReps = [forall a. Typeable a => TypeRep
typeRepOf @a, forall a. Typeable a => TypeRep
typeRepOf @b]
instance (Typeable a, AllTypeable (b ': c ': as)) => AllTypeable (a ': b ': c ': as) where
typeReps :: [TypeRep]
typeReps = forall a. Typeable a => TypeRep
typeRepOf @a forall a. a -> [a] -> [a]
: forall (as :: [*]). AllTypeable as => [TypeRep]
typeReps @(b ': c ': as)