-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Parsing.elm
56 lines (47 loc) · 1.76 KB
/
Parsing.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
module Parsing exposing (urlParser)
import Regex
import Types exposing (Route(..))
import Url exposing (Url)
import Url.Parser exposing (Parser, map, oneOf, parse, top)
import Views.AlertList.Parsing exposing (alertsParser)
import Views.Settings.Parsing exposing (settingsViewParser)
import Views.SilenceForm.Parsing exposing (silenceFormEditParser, silenceFormNewParser)
import Views.SilenceList.Parsing exposing (silenceListParser)
import Views.SilenceView.Parsing exposing (silenceViewParser)
import Views.Status.Parsing exposing (statusParser)
urlParser : Url -> Route
urlParser url =
let
-- Parse a query string occurring after the hash if it exists, and use
-- it for routing.
hashAndQuery =
url.fragment
|> Maybe.map
(Regex.splitAtMost 1 (Regex.fromString "\\?" |> Maybe.withDefault Regex.never))
|> Maybe.withDefault []
( path, query ) =
case hashAndQuery of
[] ->
( "/", Nothing )
h :: [] ->
( h, Nothing )
h :: rest ->
( h, Just (String.concat rest) )
in
case parse routeParser { url | query = query, fragment = Nothing, path = path } of
Just route ->
route
Nothing ->
NotFoundRoute
routeParser : Parser (Route -> a) a
routeParser =
oneOf
[ map SilenceListRoute silenceListParser
, map StatusRoute statusParser
, map SettingsRoute settingsViewParser
, map SilenceFormNewRoute silenceFormNewParser
, map SilenceViewRoute silenceViewParser
, map SilenceFormEditRoute silenceFormEditParser
, map AlertsRoute alertsParser
, map TopLevelRoute top
]