Source file dispatch.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(*----------------------------------------------------------------------------
    Copyright (c) 2015 Inhabited Type LLC.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. Neither the name of the author nor the names of his contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
  ----------------------------------------------------------------------------*)

type tag =
  [ `Lit | `Var ]

type typ =
  [ `Prefix | `Exact ]

type assoc = (string * string) list
type 'a route = (tag * string) list * typ * (assoc -> string option -> 'a)

type 'a t = 'a route list

let create t = t

let path_split path =
  (* NOTE(seliopou): This was implemented manually to minimize dependencies for
   * js_of_ocaml. Ain't nobody got time for another regular expression library
   * in their browser. *)
  let rec loop i acc =
    try
      let j = String.index_from path i '/' in
      loop (j + 1) (String.(sub path i (j - i))::acc)
    with Not_found ->
      let len = String.length path in
      let result =
        if i >= len
          then acc
          else (String.sub path i (len - i))::acc
      in
      List.rev result
  in
  match path with
  | "" -> []
  | _ ->
    if String.get path 0 = '/'
      then loop 1 []
      else loop 0 []

let to_dsl (ms, typ) =
  let start =
    String.concat "/" (List.map (function
      | (`Lit, x) -> x
      | (`Var, x) -> ":" ^ x)
    ms)
  in
  match typ with
  | `Exact  -> start
  | `Prefix -> start ^ "/*"

let of_dsl str =
  let star, rev_parts =
    match List.rev (path_split str) with
    | "*"::ps' -> `Prefix, ps'
    | ps'      -> `Exact , ps'
  in
  let parts =
    List.fold_left (fun acc p ->
      let len = String.length p in
      if len > 0 && String.get p 0 = ':' then
        (`Var, String.sub p 1 (len - 1))::acc
      else
        (`Lit, p)::acc)
    [] rev_parts
  in
  parts, star

let path_match ps0 ms0 =
  let rec loop ps ms acc =
    match ps, ms with
    | []    , []  -> `Exact (List.rev acc)
    | _     , []  -> `Partial (List.rev acc, ps)
    | []    , _   -> `Failure (Printf.sprintf
      "unmatched pattern suffix: %s" (to_dsl (ms, `Exact)))
    | p::ps', (`Lit, l)::ms' ->
      if p = l then loop ps' ms' acc else `Failure (Printf.sprintf
        "pattern mismatch: expected '%s' but got '%s'" l p)
    | p::ps', (`Var, m)::ms' ->
      loop ps' ms' ((m, p) :: acc)
  in
  loop ps0 ms0 []

let dispatch routes path =
  let ps0 = path_split path in
  let rec loop = function
    | []          -> None
    | (ms, exact, f)::xs ->
      begin match exact, path_match ps0 ms with
      | #typ   , `Exact assoc        -> Some(f assoc None)
      | `Prefix, `Partial(assoc, ps) -> Some(f assoc (Some (String.concat "/" ps)))
      | `Exact , `Partial _          -> loop xs
      | _      , `Failure _          -> loop xs
      end
  in
  loop routes

let dispatch_exn routes path =
  match dispatch routes path with
  | Some x -> x
  | None   -> failwith (Printf.sprintf "no matching route found: %s" path)

module DSL = struct
  type 'a route = string * (assoc -> string option -> 'a)

  let create routes =
    List.map (fun (m, x) ->
      let ts, t = of_dsl m in
      ts, t, x)
    routes
end