2 ExistentialQuantification
7 module Network.HTTP.Lucu.Resource.Dispatcher
12 import Control.Applicative
13 import Data.ByteString (ByteString)
14 import Data.CaseInsensitive (CI)
16 import Data.Text (Text)
17 import Network.HTTP.Lucu.Resource.Internal
18 import Prelude.Unicode
21 class Dispatchable α where
25 → IO (Maybe ([ByteString], Resource))
27 dispatcher ∷ α → Dispatcher
28 {-# INLINE dispatcher #-}
29 dispatcher = Dispatcher
31 -- |Container type for 'Dispatchable' type class.
32 data Dispatcher = ∀α. Dispatchable α ⇒ Dispatcher α
34 instance Dispatchable Dispatcher where
35 dispatch (Dispatcher α) = dispatch α
38 -- |@a `'mappend'` b@ first tries to find a resource with @a@, and if
39 -- it returns 'Nothing', tries @b@ next.
40 instance Monoid Dispatcher where
42 mempty = dispatcher ()
44 {-# INLINEABLE mappend #-}
45 mappend (Dispatcher α) (Dispatcher β)
47 $ \host path → do r ← dispatch α host path
50 Nothing → dispatch β host path
52 -- |An IO-based dispatcher returning resource paths as well as
54 instance Dispatchable (CI Text
56 → IO (Maybe ([ByteString], Resource))) where
59 -- |An IO-based dispatcher.
60 instance Dispatchable (CI Text → [ByteString] → IO (Maybe Resource)) where
61 dispatch = ((((<$>) ∘ (<$>)) ((,) []) ∘) ∘)
63 -- |A pure dispatcher.
64 instance Dispatchable (CI Text → [ByteString] → Maybe Resource) where
65 dispatch = (((return ∘ ((,) [] <$>)) ∘) ∘)
67 -- |An IO-based dispatcher ignoring host names.
68 instance Dispatchable ([ByteString] → IO (Maybe Resource)) where
69 dispatch = const ∘ (((<$>) ∘ (<$>)) ((,) []) ∘)
71 -- |A pure dispatcher ignoring host names.
72 instance Dispatchable ([ByteString] → Maybe Resource) where
73 dispatch = const ∘ ((return ∘ ((,) [] <$>)) ∘)
75 -- |The constant dispatcher returning always the same 'Resource'.
76 instance Dispatchable Resource where
77 dispatch = const ∘ const ∘ return ∘ Just ∘ (,) []
79 -- |The empty dispatcher returning always 'Nothing'.
80 instance Dispatchable () where
81 dispatch _ _ _ = return Nothing