aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Route.hs
blob: d683b76deed7218b22fcc2c131e83c695bbf3dd2 (plain)
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
module Route
  ( Route (..),
    parseURI,
    routeToString,
  )
where

import Data.Attoparsec.Text qualified as P
import Data.Default
import Data.Text qualified as T
import Miso

data Route
  = Home
  | ListCollection String
  | EditValue String String
  | NewCollection
  deriving (Show, Eq)

instance Default Route where
  def = Home

parseURI :: URI -> Route
parseURI uri =
  either (const def) id $
    P.parseOnly
      ( P.choice
          [ EditValue
              <$> (P.string "#collection/" *> P.manyTill P.anyChar (P.string "/"))
              <*> (P.many1 P.anyChar),
            pure NewCollection <* (P.string "#collection/new"),
            ListCollection <$> (P.string "#collection/" *> P.many1 P.anyChar),
            pure Home
          ]
          <* P.endOfInput
      )
      (T.pack uri.uriFragment)

routeToString :: Route -> String
routeToString Home = "#"
routeToString (ListCollection collection) = "#collection/" <> collection
routeToString (EditValue collection fileName) = "#collection/" <> collection <> "/" <> fileName
routeToString NewCollection = "#collection/new"