DoAndIfThenElse
, ExistentialQuantification
, FlexibleInstances
+ , GeneralizedNewtypeDeriving
, OverlappingInstances
, MultiParamTypeClasses
, RecordWildCards
, ScopedTypeVariables
+ , TemplateHaskell
, UndecidableInstances
, UnicodeSyntax
#-}
, ResourceMapper(..)
, ResourceMap
, ResourceTree
- , ResourceNode
, dispatch
)
import Control.Monad.Trans.Maybe
import Control.Monad.Unicode
import Data.Collections
-import qualified Data.Map as M
+import qualified Data.Collections.Newtype.TH as C
import Data.Monoid
import Data.Monoid.Unicode
+import Network.HTTP.Lucu.Dispatcher.Node
import Network.HTTP.Lucu.Resource.Internal
import Network.HTTP.Lucu.Utils
import Network.URI hiding (path)
-import Prelude hiding (filter, lookup, null)
+import Prelude hiding (lookup)
import Prelude.Unicode
-- |FIXME: docs
-- FIXME: doc
newtype ResourceTree = Root ResourceNode
-
--- FIXME: docs
-data ResourceNode
- = Greedy !Resource
- | NonGreedy !Resource !SubTree
- | Branch !SubTree
-
-type SubTree
- = M.Map PathSegment ResourceNode
+ deriving (Monoid, Show)
-- Instances of SchemeMapper --------------------------------------------------
instance SchemeMapper SchemeMap where
findResource = (maybe (fail (⊥)) return ∘) ∘ flip id
-- Instances of ResourceTree --------------------------------------------------
-instance Unfoldable ResourceTree (PathSegments, ResourceNode) where
- {-# INLINE insert #-}
- insert e (Root root) = Root $ insert e root
- {-# INLINE empty #-}
- empty = (∅)
-
-instance Monoid ResourceTree where
- {-# INLINE mempty #-}
- mempty = Root (∅)
- {-# INLINE mappend #-}
- mappend (Root a) (Root b)
- = Root (a ⊕ b)
-
--- Instances of ResourceNode --------------------------------------------------
-instance Unfoldable ResourceNode (PathSegments, ResourceNode) where
- {-# INLINEABLE insert #-}
- insert (p, n) = insertNodeAt (canonPath p) n
- {-# INLINE empty #-}
- empty = Branch (∅)
-
-canonPath ∷ (Collection c f, Foldable f e) ⇒ c → c
-{-# INLINEABLE canonPath #-}
-canonPath = filter ((¬) ∘ null)
-
-insertNodeAt ∷ PathSegments → ResourceNode → ResourceNode → ResourceNode
-{-# INLINEABLE insertNodeAt #-}
-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 #-}
- mappend _ (Greedy r ) = Greedy r
- mappend (Greedy _ ) (NonGreedy r n) = NonGreedy r n
- mappend (NonGreedy _ m) (NonGreedy r n) = NonGreedy r (m ⊕ n)
- mappend (Branch m) (NonGreedy r n) = NonGreedy r (m ⊕ n)
- mappend (Greedy r ) (Branch _) = Greedy r
- mappend (NonGreedy r m) (Branch n) = NonGreedy r (m ⊕ n)
- mappend (Branch m) (Branch n) = Branch (m ⊕ n)
+C.derive [d| instance Foldable ResourceTree (PathSegments, ResourceNode)
+ |]
-- dispatch -------------------------------------------------------------------
dispatch ∷ SchemeMapper α ⇒ URI → α → MaybeT IO (PathSegments, Resource)