2 ExistentialQuantification
4 , MultiParamTypeClasses
9 module Network.HTTP.Lucu.Resource.Dispatcher
16 import Data.CaseInsensitive (CI)
17 import Data.Collections
19 import Data.Text (Text)
20 import Network.HTTP.Lucu.Resource.Internal
21 import Network.HTTP.Lucu.Utils
23 import Prelude.Unicode
27 -- Minimal complete definition: 'dispatch'
28 class Dispatchable α where
29 dispatch ∷ α → URI → IO (Maybe Resource)
31 dispatcher ∷ α → Dispatcher
32 {-# INLINE dispatcher #-}
33 dispatcher = Dispatcher
35 -- |Container type for 'Dispatchable' type class.
36 data Dispatcher = ∀α. Dispatchable α ⇒ Dispatcher α
38 instance Dispatchable Dispatcher where
39 dispatch (Dispatcher α) = dispatch α
42 -- |@insert a b@ first tries @a@, and then tries @b@. @insertMany bs
43 -- a@ first tries @a@, and then tries each one in @bs@ from head to
45 instance Dispatchable α ⇒ Unfoldable Dispatcher α where
46 {-# INLINEABLE insert #-}
47 insert a (Dispatcher b)
49 $ \uri → do r ← dispatch a uri
52 Nothing → dispatch b uri
56 e ∷ URI → IO (Maybe Resource)
58 e = return ∘ const Nothing
59 {-# INLINE singleton #-}
60 singleton = dispatcher
62 -- |@a `'mappend'` b@ first tries @a@, and then tries @b@.
63 instance Monoid Dispatcher where
66 {-# INLINE mappend #-}
69 -- |An IO-based dispatcher.
70 instance Dispatchable (URI → IO (Maybe Resource)) where
73 -- |A pure dispatcher.
74 instance Dispatchable (URI → Maybe Resource) where
77 -- |The constant dispatcher returning always the same 'Resource'.
78 instance Dispatchable Resource where
79 dispatch = const ∘ return ∘ Just
82 uriHost ∷ URI → CI Text
83 uriHost = error "FIXME"