X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FResource%2FDispatcher.hs;fp=Network%2FHTTP%2FLucu%2FResource%2FDispatcher.hs;h=b3c6d0758be3f18d379d620dbbc00034146d8362;hb=24d6b6e25e79495eaa00eb6eacdb707d181d0770;hp=0000000000000000000000000000000000000000;hpb=b87f64c979c79592e6824ee531478eacdaa384bb;p=Lucu.git diff --git a/Network/HTTP/Lucu/Resource/Dispatcher.hs b/Network/HTTP/Lucu/Resource/Dispatcher.hs new file mode 100644 index 0000000..b3c6d07 --- /dev/null +++ b/Network/HTTP/Lucu/Resource/Dispatcher.hs @@ -0,0 +1,100 @@ +{-# LANGUAGE + ExistentialQuantification + , FlexibleInstances + , UnicodeSyntax + #-} +-- |FIXME: doc +module Network.HTTP.Lucu.Resource.Dispatcher + ( Dispatchable(..) + , Dispatcher + ) + where +import Control.Applicative +import Data.ByteString (ByteString) +import Data.CaseInsensitive (CI) +import Data.Monoid +import Data.Text (Text) +import Network.HTTP.Lucu.Resource.Internal +import Prelude.Unicode + +-- |FIXME: docs +class Dispatchable α where + findResource ∷ α + → CI Text + → [ByteString] + → IO (Maybe ([ByteString], ResourceDef)) + + dispatcher ∷ α → Dispatcher + {-# INLINE dispatcher #-} + dispatcher = Dispatcher + +-- |FIXME: doc +data Dispatcher = ∀α. Dispatchable α ⇒ Dispatcher α + +instance Dispatchable Dispatcher where + findResource (Dispatcher α) = findResource α + dispatcher = id + +-- |FIXME: doc +instance Monoid Dispatcher where + {-# INLINE mempty #-} + mempty = dispatcher f + where + f ∷ CI Text → [ByteString] → IO (Maybe ([ByteString], ResourceDef)) + f _ _ = return Nothing + + {-# INLINEABLE mappend #-} + mappend (Dispatcher α) (Dispatcher β) + = dispatcher + $ \host path → do r ← findResource α host path + case r of + Just _ → return r + Nothing → findResource β host path + +instance Dispatchable (CI Text + → [ByteString] + → IO (Maybe ([ByteString], ResourceDef))) where + findResource = id + +instance Dispatchable (CI Text + → [ByteString] + → Maybe ([ByteString], ResourceDef)) where + findResource = ((return ∘) ∘) + +instance Dispatchable (CI Text + → [ByteString] + → IO (Maybe ResourceDef)) where + findResource = ((((<$>) ∘ (<$>)) ((,) []) ∘) ∘) + +instance Dispatchable (CI Text + → [ByteString] + → Maybe ResourceDef) where + findResource = (((return ∘ ((,) [] <$>)) ∘) ∘) + +instance Dispatchable ([ByteString] + → IO (Maybe ([ByteString], ResourceDef))) where + findResource = const + +instance Dispatchable ([ByteString] + → Maybe ([ByteString], ResourceDef)) where + findResource = const ∘ (return ∘) + +instance Dispatchable ([ByteString] + → IO (Maybe ResourceDef)) where + findResource = const ∘ (((<$>) ∘ (<$>)) ((,) []) ∘) + +instance Dispatchable ([ByteString] + → Maybe ResourceDef) where + findResource = const ∘ ((return ∘ ((,) [] <$>)) ∘) + +instance Dispatchable (IO (Maybe ([ByteString], ResourceDef))) where + findResource = const ∘ const + +instance Dispatchable ([ByteString], ResourceDef) where + findResource = const ∘ const ∘ return ∘ Just + +instance Dispatchable (IO (Maybe ResourceDef)) where + findResource = const ∘ const ∘ ((<$>) ∘ (<$>)) ((,) []) + +instance Dispatchable ResourceDef where + findResource = const ∘ const ∘ return ∘ Just ∘ (,) []