From dc20568c7c323ea2193326aab48f236fa3817191 Mon Sep 17 00:00:00 2001 From: PHO <pho@cielonegro.org> Date: Wed, 23 Nov 2011 22:32:36 +0900 Subject: [PATCH] Still working on Dispatcher... Ditz-issue: e0312227f40a0fa92d4c5d69a64dad473f54389a --- Network/HTTP/Lucu/Dispatcher/Internal.hs | 46 +++++++++++------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/Network/HTTP/Lucu/Dispatcher/Internal.hs b/Network/HTTP/Lucu/Dispatcher/Internal.hs index f8f3b12..71e5ffd 100644 --- a/Network/HTTP/Lucu/Dispatcher/Internal.hs +++ b/Network/HTTP/Lucu/Dispatcher/Internal.hs @@ -77,8 +77,11 @@ newtype ResourceTree = Root ResourceNode -- FIXME: docs data ResourceNode = Greedy !Resource - | NonGreedy !Resource !ResourceTree - | Branch !ResourceTree + | NonGreedy !Resource !SubTree + | Branch !SubTree + +type SubTree + = M.Map PathSegment ResourceNode -- Instances of SchemeMapper -------------------------------------------------- instance SchemeMapper SchemeMap where @@ -116,7 +119,7 @@ instance SchemeMapper α â Unfoldable SchemeMap α where {-# INLINE singleton #-} singleton = schemeMap --- |@a `'mappend'` b@ first tries @a@, and if it fails, tries @b@ next. +-- |@'mappend' a b@ first tries @a@, and if it fails, tries @b@ next. instance Monoid SchemeMap where {-# INLINE mempty #-} mempty = schemeMap e @@ -168,7 +171,7 @@ instance HostMapper α â Unfoldable HostMap α where {-# INLINE singleton #-} singleton = hostMap --- |@a `'mappend'` b@ first tries @a@, and if it fails, tries @b@ next. +-- |@'mappend' a b@ first tries @a@, and if it fails, tries @b@ next. instance Monoid HostMap where {-# INLINE mempty #-} mempty = hostMap e @@ -213,7 +216,7 @@ instance ResourceMapper α â Unfoldable ResourceMap α where {-# INLINE singleton #-} singleton = resourceMap --- |@a `'mappend'` b@ first tries @a@, and if it fails, tries @b@ next. +-- |@'mappend' a b@ first tries @a@, and if it fails, tries @b@ next. instance Monoid ResourceMap where {-# INLINE mempty #-} mempty = resourceMap e @@ -239,28 +242,23 @@ instance ResourceMapper (PathSegments â Maybe (PathSegments, Resource)) where findResource = (maybe (fail (â¥)) return â) â flip id -- Instances of ResourceTree -------------------------------------------------- -{- -instance (Functor m, MonadIO m) - â Unfoldable (ResourceTree m) ([PathSegment], ResourceNode m) where +instance Unfoldable ResourceTree (PathSegments, ResourceNode) where {-# INLINE insert #-} insert e (Root root) = Root $ insert e root {-# INLINE empty #-} empty = (â ) -instance (Functor m, MonadIO m) â Monoid (ResourceTree m) where +instance Monoid ResourceTree where {-# INLINE mempty #-} mempty = Root (â ) {-# INLINE mappend #-} mappend (Root a) (Root b) = Root (a â b) --} -- Instances of ResourceNode -------------------------------------------------- -{- -instance (Functor m, MonadIO m) - â Unfoldable (ResourceNode m) ([PathSegment], ResourceNode m) where +instance Unfoldable ResourceNode (PathSegments, ResourceNode) where {-# INLINEABLE insert #-} - insert (p, a) b = insertNodeAt (canonPath p) a b + insert (p, n) = insertNodeAt (canonPath p) n {-# INLINE empty #-} empty = Branch (â ) @@ -268,17 +266,16 @@ canonPath â· (Collection c f, Foldable f e) â c â c {-# INLINEABLE canonPath #-} canonPath = filter ((¬) â null) -insertNodeAt â· (Functor m, MonadIO m) - â [PathSegment] - â ResourceNode m - â ResourceNode m - â ResourceNode m +insertNodeAt â· PathSegments â ResourceNode â ResourceNode â ResourceNode {-# INLINEABLE insertNodeAt #-} -insertNodeAt [] a b = a â b -insertNodeAt (x:[]) a b = Branch (singleton (x, a)) â b -insertNodeAt (x:xs) a b = insertNodeAt xs a (â ) â b - -instance (Functor m, MonadIO m) â Monoid (ResourceNode m) where +insertNodeAt p a b + = case front p of + Nothing â a â b + Just (x, xs) + | null xs â Branch (singleton (x, a)) â b + | otherwise â insertNodeAt xs a (â ) â b + +instance Monoid ResourceNode where {-# INLINE mempty #-} mempty = Branch (â ) {-# INLINEABLE mappend #-} @@ -289,7 +286,6 @@ instance (Functor m, MonadIO m) â Monoid (ResourceNode m) where mappend (Greedy r ) (Branch _) = Greedy r mappend (NonGreedy r m) (Branch n) = NonGreedy r (m â n) mappend (Branch m) (Branch n) = Branch (m â n) --} -- dispatch ------------------------------------------------------------------- dispatch â· SchemeMapper α â URI â α â MaybeT IO (PathSegments, Resource) -- 2.40.0