forked from MedeaMelana/Zwaluw
/
EventExample.hs
57 lines (37 loc) · 1.41 KB
/
EventExample.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- Chris Done's example at http://hackage.haskell.org/package/url-generic-0.1
-- translated to Zwaluw
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoMonoPatBinds #-}
import Web.Zwaluw
import Web.Zwaluw.TH
import Prelude hiding (id, (.), (/))
import Control.Category
import Data.Maybe
-- Datatypes copied verbatim from example
data Event = Event { eventId :: Maybe Integer -- ^ The event id.
, eventScope :: Bool -- ^ Show the scope?
, eventLayout :: Layout -- ^ Layout for the page.
}
deriving Show
data Layout =
Wide | Thin | Collapsed
deriving (Show, Enum)
-- Let Zwaluw automatically derive pure routers
event :: Router (Maybe Integer :- Bool :- Layout :- r) (Event :- r)
event = $(deriveRouterTuple ''Event)
wide, thin, collapsed :: Router r (Layout :- r)
(wide, thin, collapsed) = $(deriveRouterTuple ''Layout)
-- Custom routers, tying a URL format to the datatypes
rEvent :: Router r (Event :- r)
rEvent = event / "event" / "id" / rJust . integer / rFalse . "layout" / rLayout
rLayout :: Router r (Layout :- r)
rLayout = wide . "wide"
<> thin . "thin"
<> collapsed . "collapsed"
-- Auxiliary functions
parseURLPath :: String -> Maybe Event
parseURLPath = parse1 rEvent
formatURLPath :: Event -> Maybe String
formatURLPath = unparse1 rEvent