Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
tatchi committed Jun 16, 2024
1 parent bd84fff commit 037ffeb
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/routes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,22 @@ type 'a match_result =

let parse_route path handler params =
let rec match_target
: type a b. (a, b) path -> a -> string list -> string list -> b list
: type a b. (a, b) path -> a -> string list -> string list -> b match_result
=
fun t f seen s ->
match t with
| End ->
(match s with
| [] | [ "" ] -> [ f ]
| _ -> [ f ])
| Wildcard -> [ f { Parts.prefix = List.rev seen; matched = s } ]
| End -> FullMatch [ f ]
| Wildcard -> FullMatch [ f { Parts.prefix = List.rev seen; matched = s } ]
| Match (x, fmt) ->
(match s with
| x' :: xs when x = x' -> match_target fmt f (x' :: seen) xs
| _ -> [])
| _ -> NoMatch)
| Conv ({ from_; _ }, fmt) ->
(match s with
| [] -> []
| [] -> NoMatch
| x :: xs ->
(match from_ x with
| None -> []
| None -> NoMatch
| Some x' -> match_target fmt (f x') (x :: seen) xs))
in
match_target path handler [] params
Expand Down Expand Up @@ -243,22 +240,19 @@ let map f (Route (r, h, g)) = Route (r, h, fun x -> f (g x))

let rec match_routes target routes acc =
match routes with
| [] -> acc
| [] -> FullMatch acc
| Route (r, h, f) :: rs ->
(match parse_route r h target with
| [] -> match_routes target rs acc
| r ->
| NoMatch -> match_routes target rs acc
| FullMatch r ->
let r = List.map f r in
match_routes target rs (r @ acc))
;;

let match' router ~target =
let target = Util.split_path target in
let routes = PatternTrie.feed_params router target in
let res = match_routes target routes [] in
match res with
| [] -> NoMatch
| l -> FullMatch l
match_routes target routes []
;;

let ( /~ ) m path = m path

0 comments on commit 037ffeb

Please sign in to comment.