-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.elm
80 lines (60 loc) · 1.71 KB
/
Route.elm
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Route exposing (MazeShape(..), Route(..), parse, toAbsolutePath)
import Url exposing (Url)
import Url.Builder as Builder
import Url.Parser as Parser exposing ((</>), Parser, int, map, oneOf, s, top)
import Url.Parser.Query as Query
type Route
= New
| Maze { shape : MazeShape, seed : Int, width : Int, height : Int }
| NotFound
type MazeShape
= Squares
| Hexes
parse : Url -> Route
parse =
Parser.parse parser >> Maybe.withDefault NotFound
parser : Parser (Route -> b) b
parser =
oneOf
[ map
(\shape width height seed ->
Maze { shape = shape, seed = seed, width = width, height = height }
)
(top </> s "maze" </> mazeShapeParser </> int </> int </> int)
, map New top
]
mazeShapeParser : Parser (MazeShape -> b) b
mazeShapeParser =
Parser.custom "SHAPE"
(\segment ->
case segment of
"squares" ->
Just Squares
"hexes" ->
Just Hexes
_ ->
Nothing
)
toAbsolutePath : Route -> String
toAbsolutePath route =
case route of
New ->
Builder.absolute [] []
Maze { shape, seed, width, height } ->
Builder.absolute
[ "maze"
, shapeToSegment shape
, String.fromInt width
, String.fromInt height
, String.fromInt seed
]
[]
NotFound ->
Builder.absolute [ "404" ] []
shapeToSegment : MazeShape -> String
shapeToSegment shape =
case shape of
Squares ->
"squares"
Hexes ->
"hexes"