Files
acms/frontend/app/Route.hs

44 lines
1.1 KiB
Haskell
Raw Normal View History

2024-06-04 14:36:26 +02:00
module Route
( Route (..),
parseURI,
2024-06-06 15:31:16 +02:00
routeToString,
2024-06-04 14:36:26 +02:00
)
where
import Data.Attoparsec.Text qualified as P
import Data.Default
import Data.Text qualified as T
import Miso
data Route
= Home
| ListCollection String
2024-06-05 10:41:02 +02:00
| EditValue String String
2024-06-06 23:05:41 +02:00
| NewCollection
2024-06-04 14:36:26 +02:00
deriving (Show, Eq)
instance Default Route where
def = Home
parseURI :: URI -> Route
parseURI uri =
either (const def) id $
P.parseOnly
( P.choice
2024-06-05 10:41:02 +02:00
[ EditValue
<$> (P.string "#collection/" *> P.manyTill P.anyChar (P.string "/"))
<*> (P.many1 P.anyChar),
2024-06-06 23:05:41 +02:00
pure NewCollection <* (P.string "#collection/new"),
2024-06-05 10:41:02 +02:00
ListCollection <$> (P.string "#collection/" *> P.many1 P.anyChar),
2024-06-04 14:36:26 +02:00
pure Home
]
<* P.endOfInput
)
(T.pack uri.uriFragment)
2024-06-06 15:31:16 +02:00
routeToString :: Route -> String
routeToString Home = "#"
routeToString (ListCollection collection) = "#collection/" <> collection
routeToString (EditValue collection fileName) = "#collection/" <> collection <> "/" <> fileName
2024-06-06 23:05:41 +02:00
routeToString NewCollection = "#collection/new"