Permalink
Browse files

Updated example to use the overloaded strings and the new / operator.

  • Loading branch information...
1 parent 45b2a57 commit 27f7be85a5435343059551c51e49c5b9a687553f @MedeaMelana committed Nov 24, 2010
Showing with 27 additions and 21 deletions.
  1. +22 −17 Example.hs
  2. +5 −4 Web/Zwaluw.hs
View
@@ -1,36 +1,41 @@
{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE OverloadedStrings #-}
-import Web.Zwaluw
-import Prelude hiding (id, (.))
+import Prelude hiding (id, (.), (/))
import Control.Category
+import Web.Zwaluw
+-- A datatype modelling all pages in a website.
+
data Sitemap
= Home
+ | UserOverview
+ | UserDetail Int
| Range Int Int
- | CaseOverview
- | CaseDetail Int
deriving (Eq, Show)
+
+-- Constructor routers. Soon to be generated by Template Haskell.
+
home :: Router r (Sitemap :- r)
home = constr0 Home $ \a -> do Home <- a; return ()
-caseOverview :: Router r (Sitemap :- r)
-caseOverview = constr0 CaseOverview $ \a -> do CaseOverview <- a; return ()
+userOverview :: Router r (Sitemap :- r)
+userOverview = constr0 UserOverview $ \a -> do UserOverview <- a; return ()
-caseDetail :: Router (Int :- r) (Sitemap :- r)
-caseDetail = constr1 CaseDetail $ \a -> do CaseDetail i <- a; return i
+userDetail :: Router (Int :- r) (Sitemap :- r)
+userDetail = constr1 UserDetail $ \a -> do UserDetail i <- a; return i
range :: Router (Int :- Int :- r) (Sitemap :- r)
range = constr2 Range $ \a -> do Range l u <- a; return (l, u)
-url :: Router r (Sitemap :- r)
-url =
- slash .
- ( home . lit "home"
- <> lit "cases" .
- ( caseOverview
- <> caseDetail . slash . int
- )
- <> range . lit "range" . slash . int . slash . int
+
+-- The router. Specifies how to parse a URL into a Sitemap and back.
+
+sitemap :: Router r (Sitemap :- r)
+sitemap = id /
+ ( home
+ <> "users" . (userOverview <> userDetail / int)
+ <> range / int / int
)
View
@@ -16,7 +16,7 @@ module Web.Zwaluw (
-- datatypes to routers. Their first argument is the constructor; their
-- second argument is a (partial) destructor.
, constr0, constr1, constr2, constr3
- , int, string, char, part, digit, val, slash, lit
+ , int, string, char, part, digit, val, (/), lit
, opt, duck, satisfy, having, printAs
, manyr, somer, chainr1
, manyl, somel, chainl1
@@ -210,9 +210,10 @@ lit l = Router
(\b -> return ((l ++), b))
(\s -> let (s1, s2) = splitAt (length l) s in if s1 == l then return (id, s2) else mzero)
--- | Routes a slash.
-slash :: Router r r
-slash = lit "/"
+-- | @p / q@ is equivalent to @p . "/" . q@.
+infixr 9 /
+(/) :: Router b c -> Router a b -> Router a c
+f / g = f . lit "/" . g
-- | Routes any integer.
int :: Router r (Int :- r)

0 comments on commit 27f7be8

Please sign in to comment.