evancz / url-parser

Parse URLs into nicely structured data
http://package.elm-lang.org/packages/evancz/url-parser/latest/
BSD 3-Clause "New" or "Revised" License
114 stars 29 forks source link

Reverse path #19

Closed AdrianRibao closed 7 years ago

AdrianRibao commented 7 years ago

Hi,

Is it possible to use a parse to get the path?

For example, with this code:

type Route
  = Search String
  | Blog Int
  | User String
  | Comment String Int

route : Parser (Route -> a) a
route =
  oneOf
    [ map Search  (s "search" </> string)
    , map Blog    (s "blog" </> int)
    , map User    (s "user" </> string)
    , map Comment (s "user" </> string </> "comments" </> int)
    ]

is there an easy way to create a funcion like this:

getRouteForPath : String -> Route

process-bot commented 7 years ago

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

sporto commented 7 years ago

Do you mean parsing a route from a string rather than location? Or reverse routing as in getting the path from a Route.

getRouteForPath : String -> Route seems to be just parsing a path and giving you the route. The lib already does this when using parsePath except that it expects a Location not a String.

Maybe you mean something like reverse: Route -> String?

AdrianRibao commented 7 years ago

oh! sorry, yes, I meant from Route to string:

getRouteForPath : Route -> String

Actually I'm having serious problems understanding parsers and their signatures, I can't event think about how to create a function that reverses the path.

I'm doing it by hand, but it seems to be a redundant solution

pathFor : Page -> String
pathFor page =
    case page of
        Home ->
            "#home"

        Login ->
            "#login"

        Post id ->
          "#post/" ++ toString id
evancz commented 7 years ago

It is not possible right now. Creating code that works like this is a research problem (Note: lens means something very particular in Nate's work. It is not the "popular" meaning, and the Haskell community uses the term "lens" to refer to something that is related only on the surface level.)