@@ -6,7 +6,6 @@
66{-# LANGUAGE ScopedTypeVariables #-}
77{-# LANGUAGE TypeFamilies #-}
88{-# LANGUAGE TypeOperators #-}
9−{-# LANGUAGE UndecidableInstances #-}
109{-# OPTIONS_HADDOCK not-home #-}
1110
1211-- | Type safe generation of internal links.
@@ -79,6 +78,8 @@
7978-- bad_link under api after trying the open (but empty) type family
8079-- `IsElem'` as a last resort.
8180module Servant.Utils.Links (
81+ module Servant.API.TypeLevel,
82+
8283 -- * Building and using safe links
8384 --
8485 -- | Note that 'URI' is Network.URI.URI from the network-uri package.
@@ -88,10 +89,6 @@ module Servant.Utils.Links (
8889 , HasLink(..)
8990 , linkURI
9091 , Link
91− , IsElem'
92− -- * Illustrative exports
93− , IsElem
94− , Or
9592) where
9693
9794import Data.List
@@ -99,7 +96,6 @@ import Data.Monoid.Compat ( (<>) )
9996import Data.Proxy ( Proxy(..) )
10097import qualified Data.Text as Text
10198import qualified Data.Text.Encoding as TE
102−import GHC.Exts (Constraint)
10399import GHC.TypeLits ( KnownSymbol, symbolVal )
104100import Network.URI ( URI(..), escapeURIString, isUnreserved )
105101import Prelude ()
@@ -115,7 +111,7 @@ import Servant.API.RemoteHost ( RemoteHost )
115111import Servant.API.Verbs ( Verb )
116112import Servant.API.Sub ( type (:>) )
117113import Servant.API.Raw ( Raw )
118−import Servant.API.Alternative ( type (:<|>) )
114+import Servant.API.TypeLevel
119115
120116-- | A safe link datatype.
121117-- The only way of constructing a 'Link' is using 'safeLink', which means any
@@ -131,58 +127,6 @@ instance ToHttpApiData Link where
131127 let uri = linkURI l
132128 in Text.pack $ uriPath uri ++ uriQuery uri
133129
134−-- | If either a or b produce an empty constraint, produce an empty constraint.
135−type family Or (a :: Constraint) (b :: Constraint) :: Constraint where
136− -- This works because of:
137− -- https://ghc.haskell.org/trac/ghc/wiki/NewAxioms/CoincidentOverlap
138− Or () b = ()
139− Or a () = ()
140−
141−-- | If both a or b produce an empty constraint, produce an empty constraint.
142−type family And (a :: Constraint) (b :: Constraint) :: Constraint where
143− And () () = ()
144−
145−-- | You may use this type family to tell the type checker that your custom
146−-- type may be skipped as part of a link. This is useful for things like
147−-- 'QueryParam' that are optional in a URI and do not affect them if they are
148−-- omitted.
149−--
150−-- >>> data CustomThing
151−-- >>> type instance IsElem' e (CustomThing :> s) = IsElem e s
152−--
153−-- Note that 'IsElem' is called, which will mutually recurse back to `IsElem'`
154−-- if it exhausts all other options again.
155−--
156−-- Once you have written a HasLink instance for CustomThing you are ready to
157−-- go.
158−type family IsElem' a s :: Constraint
159−
160−-- | Closed type family, check if endpoint is within api
161−type family IsElem endpoint api :: Constraint where
162− IsElem e (sa :<|> sb) = Or (IsElem e sa) (IsElem e sb)
163− IsElem (e :> sa) (e :> sb) = IsElem sa sb
164− IsElem sa (Header sym x :> sb) = IsElem sa sb
165− IsElem sa (ReqBody y x :> sb) = IsElem sa sb
166− IsElem (Capture z y :> sa) (Capture x y :> sb)
167− = IsElem sa sb
168− IsElem (CaptureAll z y :> sa) (CaptureAll x y :> sb)
169− = IsElem sa sb
170− IsElem sa (QueryParam x y :> sb) = IsElem sa sb
171− IsElem sa (QueryParams x y :> sb) = IsElem sa sb
172− IsElem sa (QueryFlag x :> sb) = IsElem sa sb
173− IsElem (Verb m s ct typ) (Verb m s ct' typ)
174− = IsSubList ct ct'
175− IsElem e e = ()
176− IsElem e a = IsElem' e a
177−
178−type family IsSubList a b :: Constraint where
179− IsSubList '[] b = ()
180− IsSubList (x ': xs) y = Elem x y `And` IsSubList xs y
181−
182−type family Elem e es :: Constraint where
183− Elem x (x ': xs) = ()
184− Elem y (x ': xs) = Elem y xs
185−
186130-- Phantom types for Param
187131data Query
188132