Announcing elm-route

Today marks the first release of elm-route, a type safe route parsing DSL built on top of elm-combine. Its main additions to the world of type safe route parsing are:

  • A generic DSL for expressing arbitrarily-nested dynamic routes (at the cost of uglier route constructors as the depth increases).
  • An automatic way to do reverse routing that when coupled with a small amount of boilerplate should provide the safest approach to reverse routing that the Elm language can currently support.

You can see a working demo here (source). Note that direct linking to routes in the demo does not work due to a limitation of Gihub Pages.

Related work

elm-route-parser is another type safe route parsing library. In contrast to elm-route, its more rigid matchers make it possible to have cleaner route constructors (for example, HomeR instead of HomeR ()). It does not yet provide automatic reverse routing support.

Example

Here's a short taste of what the DSL looks like:

type Sitemap
  = HomeR ()
  | UsersR ()
  | UserR Int
  | UserPostR (Int, String)

homeR = HomeR := static ""
usersR = UsersR := static "users"
userR = UserR := "users" <//> int
userPostR = UserPostR := "users" <//> int </> string
sitemap = router [homeR, usersR, userR, userPostR]

match : String -> Maybe Sitemap
match = Route.match sitemap

route : Sitemap -> String
route r =
  case r of
    HomeR () -> reverse homeR []
    UsersR () -> reverse usersR []
    UserR id -> reverse userR [toString id]
    UserPostR (uid, pid) -> reverse userPostR [toString uid, pid]

For more check out the README and the examples folder.