I'm a little mixed up about how to properly use boomerang to generate URLs. I have the following:
data State =
AK | AL | AR | AZ | CA ... WY
data Sitemap
= Home
| State State
| Place State String
deriving (Eq, Ord, Read, Show, Data, Typeable)
$(derivePrinterParsers ''Sitemap)
sitemap ∷ Router Sitemap
sitemap =
( rHome
<> rState . state
<> rPlace . (state </> anyString)
)
state :: PrinterParser StringsError [String] o (State :- o)
state = xmaph read (Just . show) anyString
This seems to work, but when I compare my implementation of state
with the one in the documentation for articleId
, they seem to be working in opposite fashions:
articleId :: Router ArticleId
articleId = xmaph ArticleId (Just . unArticleId) int
The types are totally different and look like they're going in opposite directions, but my sitemap
works and the app correctly handles URLs. I think it should look more like this:
maybeState :: String → Maybe State
maybeState stateString = case reads stateString of
[(state, "")] -> Just state
_ -> Nothing
stateR :: Router State
stateR = xpure show maybeState
This doesn't type-check, but even substituting undefined
for its definition, in sitemap
above, rState . stateR
would work, but rPlace . (stateR </> anyString)
doesn't.
Seems like this would come up often enough there's probably a library function to take care of this for me, but I didn't see one.
Edit: here are some of the type errors I get:
For state = xpure show maybeState
:
Main.hs:56:16:
Couldn't match expected type `State :- ()'
with actual type `[Char]'
Expected type: () -> State :- ()
Actual type: () -> String
In the first argument of `xpure', namely `show'
In the expression: xpure show maybeState
For state = undefined :: Router State
(this error is in the sitemap
definition):
Main.hs:45:18:
Couldn't match expected type `String :- ()' with actual type `()'
Expected type: PrinterParser
StringsError [String] () (State :- (String :- ()))
Actual type: Router State
In the first argument of `(</>)', namely `state'
In the second argument of `(.)', namely `(state </> anyString)'