tourist/router
src/tourist/router.tur
route pattern compilation and request dispatch.
Since: v0.1.0
defn
router-compile
(router-compile [pattern : cstr] :)
compile a route pattern string into a match handle.
Parameters
| pattern | route pattern string, e.g. "/user/:id" or "/files/*" |
Returns
:int -- compiled pattern handle; free with router-free
Example
(let [p (router-compile "/hello/:name")]
(router-free p))
Since: v0.1.0
defn
router-free
(router-free [pat : int] :)
release a compiled pattern handle.
Parameters
| pat | :int handle from router-compile, or 0 (no-op) |
Returns
:void
Example
(router-free p)
Since: v0.1.0
defn
router-match
(router-match [pat : int path : cstr] :)
test a compiled pattern against a request path.
Parameters
| pat | :int compiled pattern from router-compile | |
| path | request path string, e.g. "/user/42" |
Returns
:int -- captures handle on match (free with captures-free), 0 on no-match
Example
(let [caps (router-match p "/user/42")]
(when (!= caps 0)
(captures-get caps "id")))
Since: v0.1.0
defn
captures-get
(captures-get [caps : int key : cstr] :)
look up a capture by name.
Parameters
| caps | :int captures handle from router-match | |
| key | capture name, e.g. "id" for :id, or "*" for wildcard |
Returns
:int -- result<:cstr>: ok(value) if found, err("missing") otherwise
Example
(let [r (captures-get caps "id")]
(when (ok? r) (ok-val r)))
Since: v0.1.0
defn
captures-count
(captures-count [caps : int] :)
number of captures in a captures handle.
Parameters
| caps | :int captures handle from router-match |
Returns
:int -- number of captured segments (0 for empty or null handle)
Example
(captures-count caps) ; => 1
Since: v0.1.0
defn
captures-free
(captures-free [caps : int] :)
release a captures handle.
Parameters
| caps | :int handle from router-match, or 0 (no-op) |
Returns
:void
Example
(captures-free caps)
Since: v0.1.0