diff --git a/src/core/route.ml b/src/core/route.ml index f2e52f08..f989dc86 100644 --- a/src/core/route.ml +++ b/src/core/route.ml @@ -91,3 +91,22 @@ module Private_ = struct end let pp out x = Format.pp_print_string out (to_string x) + +let rec to_url_rec : type b. Buffer.t -> (b, string) t -> b = + fun buf route -> + match route with + | Fire -> Buffer.contents buf + | Rest {url_encoded=_} -> + (fun str -> Buffer.add_string buf str; Buffer.contents buf) + | Compose (comp, rest) -> + (match comp with + | Exact s -> Buffer.add_string buf s; Buffer.add_char buf '/'; to_url_rec buf rest + | Int -> (fun i -> Printf.bprintf buf "%d/" i; to_url_rec buf rest) + | String -> + (fun s -> Printf.bprintf buf "%s/" s; to_url_rec buf rest) + | String_urlencoded -> + (fun s -> Printf.bprintf buf "%s/" (Util.percent_encode s); to_url_rec buf rest)) + +let to_url (h: ('a, string) t) : 'a = + let buf = Buffer.create 16 in + to_url_rec buf h diff --git a/src/core/route.mli b/src/core/route.mli index 80e893a9..9d70c47e 100644 --- a/src/core/route.mli +++ b/src/core/route.mli @@ -53,6 +53,8 @@ val to_string : _ t -> string (** Print the route. @since 0.7 *) +val to_url : ('a, string) t -> 'a + module Private_ : sig val eval : string list -> ('a, 'b) t -> 'a -> 'b option end