-- 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
{-# 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
{-# 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
{-# 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
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 (∅)
{-# 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 #-}
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)