module Route ( Route (..), parseURI, routeToMisoString, ) where import Data.Attoparsec.Text qualified as P import Data.Default import Data.Text qualified as T import Miso import Miso.String (MisoString, toMisoString) data Route = Home | ListCollection MisoString | EditValue MisoString MisoString | NewCollection deriving (Show, Eq) instance Default Route where def = Home parseURI :: URI -> Route parseURI uri = either (const def) id $ P.parseOnly ( P.choice [ EditValue <$> (toMisoString <$> (P.string "#collection/" *> P.manyTill P.anyChar (P.string "/"))) <*> (toMisoString <$> (P.many1 P.anyChar)), pure NewCollection <* (toMisoString <$> (P.string "#collection/new")), ListCollection <$> (toMisoString <$> (P.string "#collection/" *> P.many1 P.anyChar)), pure Home ] <* P.endOfInput ) (T.pack uri.uriFragment) routeToMisoString :: Route -> MisoString routeToMisoString Home = "#" routeToMisoString (ListCollection collection) = "#collection/" <> collection routeToMisoString (EditValue collection fileName) = "#collection/" <> collection <> "/" <> fileName routeToMisoString NewCollection = "#collection/new"