From e34e1075598174c9f38111b504daa26845a8133f Mon Sep 17 00:00:00 2001 From: Corentin Leruth Date: Sun, 16 Jun 2024 09:01:45 +0200 Subject: [PATCH] remove MatchWithTrailingSlash --- src/routes.ml | 11 ++++------- src/routes.mli | 4 +--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/routes.ml b/src/routes.ml index f424215..6c1bf5a 100644 --- a/src/routes.ml +++ b/src/routes.ml @@ -182,7 +182,7 @@ let string_of_route r = Format.asprintf "%a" pp_route r let ksprintf' k path = let rec aux : type a b. (string list -> b) -> (a, b) path -> a = - fun k -> function + fun k -> function | End -> k [] | Wildcard -> fun { Parts.matched; _ } -> k (List.concat [ matched; [] ]) | Match (w, fmt) -> aux (fun s -> k @@ (w :: s)) fmt @@ -196,19 +196,17 @@ let sprintf t = ksprintf (fun x -> x) t type 'a match_result = | FullMatch of 'a - | MatchWithTrailingSlash of 'a | NoMatch let parse_route path handler params = let rec match_target : type a b. (a, b) path -> a -> string list -> string list -> b match_result = - fun t f seen s -> + fun t f seen s -> match t with | End -> (match s with - | [ "" ] -> MatchWithTrailingSlash f - | [] -> FullMatch f + | [] | [ "" ] -> FullMatch f | _ -> NoMatch) | Wildcard -> FullMatch (f { Parts.prefix = List.rev seen; matched = s }) | Match (x, fmt) -> @@ -251,8 +249,7 @@ let rec match_routes target = function | Route (r, h, f) :: rs -> (match parse_route r h target with | NoMatch -> match_routes target rs - | FullMatch r -> FullMatch (f r) - | MatchWithTrailingSlash r -> MatchWithTrailingSlash (f r)) + | FullMatch r -> FullMatch (f r)) ;; let match' router ~target = diff --git a/src/routes.mli b/src/routes.mli index 5c4a85f..9c8735d 100644 --- a/src/routes.mli +++ b/src/routes.mli @@ -8,8 +8,7 @@ type ('a, 'b) path (** [route] is a combination of a path sequence, with a function that will be called on a successful match. When a path sequence matches, the patterns that are extracted are forwarded to said function with the types that the user defined. Note that because of - {{:https://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#ss:valuerestriction} - value restriction}, the route definitions will be assigned a weak type by the + {{:https://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#ss:valuerestriction} value restriction}, the route definitions will be assigned a weak type by the compiler. This causes problems if one intends to re-use the same route definition in multiple contexts, like using a single definition for both matching a target url, and serializing to use in a client call. To avoid such problems one can use eta-expansion @@ -179,7 +178,6 @@ val map : ('a -> 'b) -> 'a route -> 'b route type 'a match_result = | FullMatch of 'a - | MatchWithTrailingSlash of 'a | NoMatch (** [match'] accepts a router and the target url to match. *)