{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Servant.OpenApi.Internal.TypeLevel.API where
import GHC.Exts (Constraint)
import Servant.API
#if MIN_VERSION_servant(0,19,0)
import Servant.API.Generic (ToServantApi)
#endif
type family EndpointsList api where
EndpointsList (a :<|> b) = AppendList (EndpointsList a) (EndpointsList b)
EndpointsList (e :> a) = MapSub e (EndpointsList a)
#if MIN_VERSION_servant(0,19,0)
EndpointsList (NamedRoutes api) = EndpointsList (ToServantApi api)
#endif
EndpointsList a = '[a]
type family IsSubAPI sub api :: Constraint where
IsSubAPI sub api = AllIsElem (EndpointsList sub) api
type family AllIsElem xs api :: Constraint where
AllIsElem '[] api = ()
AllIsElem (x ': xs) api = (IsIn x api, AllIsElem xs api)
type family MapSub e xs where
MapSub e '[] = '[]
MapSub e (x ': xs) = (e :> x) ': MapSub e xs
type family AppendList xs ys where
AppendList '[] ys = ys
AppendList (x ': xs) ys = x ': AppendList xs ys
type family Or (a :: Constraint) (b :: Constraint) :: Constraint where
Or () b = ()
Or a () = ()
type family IsIn sub api :: Constraint where
IsIn e (a :<|> b) = Or (IsIn e a) (IsIn e b)
IsIn (e :> a) (e :> b) = IsIn a b
#if MIN_VERSION_servant(0,19,0)
IsIn e (NamedRoutes api) = IsIn e (ToServantApi api)
#endif
IsIn e e = ()
type family Elem x xs where
Elem x '[] = 'False
Elem x (x ': xs) = 'True
Elem x (y ': xs) = Elem x xs
type family Nub xs where
Nub '[] = '[]
Nub (x ': xs) = x ': Nub (Remove x xs)
type family Remove x xs where
Remove x '[] = '[]
Remove x (x ': ys) = Remove x ys
Remove x (y ': ys) = y ': Remove x ys
type BodyTypes c api = Nub (BodyTypes' c api)
type AddBodyType c cs a as = If (Elem c cs) (a ': as) as
type family BodyTypes' c api :: [*] where
BodyTypes' c (Verb verb b cs (Headers hdrs a)) = AddBodyType c cs a '[]
BodyTypes' c (Verb verb b cs NoContent) = '[]
BodyTypes' c (Verb verb b cs a) = AddBodyType c cs a '[]
BodyTypes' c (ReqBody' mods cs a :> api) = AddBodyType c cs a (BodyTypes' c api)
BodyTypes' c (e :> api) = BodyTypes' c api
BodyTypes' c (a :<|> b) = AppendList (BodyTypes' c a) (BodyTypes' c b)
#if MIN_VERSION_servant(0,19,0)
BodyTypes' c (NamedRoutes api) = BodyTypes' c (ToServantApi api)
#endif
BodyTypes' c api = '[]