Source file codec.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
type 'a t = {
  name : string;
  schema : Schema.t;
  encode : 'a -> string;
  decode : string -> ('a, Error.t) result;
}

let decode_error ~name value =
  Error
    (Error.make ~location:Error.Route ~expected:name ~got:value
       ("expected " ^ name))

let string =
  {
    name = "string";
    schema = Schema.string;
    encode = Fun.id;
    decode = (fun value -> Ok value);
  }

let int =
  {
    name = "integer";
    schema = Schema.integer;
    encode = string_of_int;
    decode =
      (fun value ->
        match int_of_string_opt value with
        | Some value -> Ok value
        | None -> decode_error ~name:"integer" value);
  }

let bool =
  {
    name = "boolean";
    schema = Schema.boolean;
    encode = string_of_bool;
    decode =
      (function
      | "true" -> Ok true
      | "false" -> Ok false
      | value -> decode_error ~name:"boolean" value);
  }

let float =
  {
    name = "number";
    schema = Schema.number;
    encode = string_of_float;
    decode =
      (fun value ->
        match float_of_string_opt value with
        | Some value -> Ok value
        | None -> decode_error ~name:"number" value);
  }