feat: add Route.rest to match the rest of the path

This commit is contained in:
Simon Cruanes 2020-11-17 09:16:28 -05:00
parent aa66e172e8
commit 2ed9131bdc
2 changed files with 12 additions and 1 deletions

View file

@ -688,9 +688,11 @@ module Route = struct
type (_, _) t =
| Fire : ('b, 'b) t
| Rest : (string -> 'b, 'b) t
| Compose: ('a, 'b) comp * ('b, 'c) t -> ('a, 'c) t
let return = Fire
let rest = Rest
let (@/) a b = Compose (a,b)
let string = String
let string_urlencoded = String_urlencoded
@ -703,6 +705,9 @@ module Route = struct
begin match path, route with
| [], Fire -> Some f
| _, Fire -> None
| _, Rest ->
let whole_path = String.concat "/" path in
Some (f whole_path)
| (c1 :: path'), Compose (comp, route') ->
begin match comp with
| Int ->
@ -730,6 +735,7 @@ module Route = struct
: type a b. Buffer.t -> (a,b) t -> unit
= fun out -> function
| Fire -> bpf out "/"
| Rest -> bpf out "<rest>"
| Compose (Exact s, tl) -> bpf out "%s/%a" s pp_ tl
| Compose (Int, tl) -> bpf out "<int>/%a" pp_ tl
| Compose (String, tl) -> bpf out "<str>/%a" pp_ tl

View file

@ -386,7 +386,7 @@ module Route : sig
(** Matches an integer. *)
val string : (string -> 'a, 'a) comp
(** Matches a string and binds it as is. *)
(** Matches a string not containing ['/'] and binds it as is. *)
val string_urlencoded : (string -> 'a, 'a) comp
(** Matches a URL-encoded string, and decodes it. *)
@ -397,6 +397,11 @@ module Route : sig
val return : ('a, 'a) t
(** Matches the empty path. *)
val rest : (string -> 'a, 'a) t
(** Matches a string, even containing ['/']. This will match
the entirety of the remaining route.
@since NEXT_RELEASE *)
val (@/) : ('a, 'b) comp -> ('b, 'c) t -> ('a, 'c) t
(** [comp / route] matches ["foo/bar/…"] iff [comp] matches ["foo"],
and [route] matches ["bar/…"]. *)