]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Dispatcher/Internal.hs
docs
[Lucu.git] / Network / HTTP / Lucu / Dispatcher / Internal.hs
index 71e5ffd211659b6af3dc8019b6db3f92e3c17eea..88b1e16cd07056110e78a93c440d89debcc10226 100644 (file)
@@ -2,13 +2,16 @@
     DoAndIfThenElse
   , ExistentialQuantification
   , FlexibleInstances
+  , GeneralizedNewtypeDeriving
   , OverlappingInstances
   , MultiParamTypeClasses
   , RecordWildCards
   , ScopedTypeVariables
+  , TemplateHaskell
   , UndecidableInstances
   , UnicodeSyntax
   #-}
+{-# OPTIONS_GHC -fno-warn-missing-methods #-}
 module Network.HTTP.Lucu.Dispatcher.Internal
     ( SchemeMapper(..)
     , SchemeMap
@@ -71,10 +74,11 @@ class ResourceMapper α where
 -- |Container type for the 'ResourceMapper' type class.
 data ResourceMap = ∀α. ResourceMapper α ⇒ RMap α
 
--- FIXME: doc
+-- |FIXME: doc
 newtype ResourceTree = Root ResourceNode
+    deriving (Monoid, Show)
 
--- FIXME: docs
+-- |FIXME: docs
 data ResourceNode
     = Greedy    !Resource
     | NonGreedy !Resource !SubTree
@@ -130,6 +134,7 @@ instance Monoid SchemeMap where
     {-# INLINE mappend #-}
     mappend = insert
 
+-- |Any 'Map's from 'Scheme' to 'HostMap' are also 'SchemeMapper's.
 instance Map α Scheme HostMap ⇒ SchemeMapper α where
     {-# INLINE findHostMap #-}
     findHostMap = (maybe (fail (⊥)) return ∘) ∘ lookup
@@ -182,6 +187,7 @@ instance Monoid HostMap where
     {-# INLINE mappend #-}
     mappend = insert
 
+-- |Any 'Map's from 'Host' to 'ResourceMap' are also 'HostMapper's.
 instance Map α Host ResourceMap ⇒ HostMapper α where
     {-# INLINE findResourceMap #-}
     findResourceMap = (maybe (fail (⊥)) return ∘) ∘ lookup
@@ -227,6 +233,8 @@ instance Monoid ResourceMap where
     {-# INLINE mappend #-}
     mappend = insert
 
+-- |Any 'Map's from 'PathSegments' to @('PathSegments', 'Resource')@
+-- are also 'ResourceMapper's.
 instance Map α PathSegments (PathSegments, Resource) ⇒ ResourceMapper α where
     {-# INLINE findResource #-}
     findResource = (maybe (fail (⊥)) return ∘) ∘ lookup
@@ -243,24 +251,10 @@ instance ResourceMapper (PathSegments → Maybe (PathSegments, Resource)) where
 
 -- 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
+    insert (p, a) (Root b) = Root $ insertNodeAt (canonPath p) a b
     {-# INLINE empty #-}
-    empty = Branch (∅)
+    empty = Root $ Branch (∅)
 
 canonPath ∷ (Collection c f, Foldable f e) ⇒ c → c
 {-# INLINEABLE canonPath #-}
@@ -275,16 +269,36 @@ insertNodeAt p a b
             | null xs   → Branch (singleton (x, a)) ⊕ b
             | otherwise → insertNodeAt xs a (∅) ⊕ b
 
+instance Foldable ResourceTree (PathSegments, ResourceNode) where
+    foldMap f (Root n) = go (∅) n
+        where
+          go p (Greedy    r  ) = f (p, Greedy r)
+          go p (NonGreedy r m) = f (p, NonGreedy r (∅)) ⊕ go' p m
+          go p (Branch      m) = go' p m
+
+          go' p = foldMap $ \(s, n') → go (p `snoc` s) n'
+
+    null (Root (Greedy    _  )) = False
+    null (Root (NonGreedy _ _)) = False
+    null (Root (Branch      m)) = null m
+
+
+-- Instances of ResourceNode --------------------------------------------------
+instance Show ResourceNode where
+    show (Greedy    _  ) = "Greedy _"
+    show (NonGreedy _ m) = "NonGreedy _ " ⊕ show m
+    show (Branch      m) = "Branch " ⊕ show m
+
 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 (Greedy    r  ) _               = Greedy    r
+    mappend (NonGreedy r m) (Greedy    _  ) = NonGreedy r      m
+    mappend (NonGreedy r m) (NonGreedy _ n) = NonGreedy r (m ⊕ n)
     mappend (NonGreedy r m) (Branch      n) = NonGreedy r (m ⊕ n)
+    mappend (Branch      _) (Greedy    r  ) = Greedy    r
+    mappend (Branch      m) (NonGreedy r n) = NonGreedy r (m ⊕ n)
     mappend (Branch      m) (Branch      n) = Branch      (m ⊕ n)
 
 -- dispatch -------------------------------------------------------------------