aboutsummaryrefslogtreecommitdiff
path: root/lib/helpers/toml.ml
blob: 1b7fb15e4028e27480829af95d5304e80df9d920 (plain)
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
module Decode = struct
  module S = struct
    type value = Otoml.t

    let pp : Format.formatter -> value -> unit =
     fun format t -> Format.pp_print_string format (Otoml.Printer.to_string t)

    let of_string : string -> (value, string) result =
      Otoml.Parser.from_string_result

    let of_file : string -> (value, string) result =
      Otoml.Parser.from_file_result

    let get_string : value -> string option = Otoml.get_opt Otoml.get_string
    let get_int : value -> int option = Otoml.get_opt Otoml.get_integer
    let get_float : value -> float option = Otoml.get_opt Otoml.get_float
    let get_bool : value -> bool option = Otoml.get_opt Otoml.get_boolean
    let get_null : value -> unit option = fun _ -> None

    let get_list : value -> value list option =
      Otoml.get_opt @@ Otoml.get_array Fun.id

    let get_key_value_pairs : value -> (value * value) list option =
      Otoml.get_opt (fun key ->
          Otoml.get_table key |> List.map (fun (k, v) -> (Otoml.string k, v)))

    let to_list : value list -> value = Otoml.array
  end

  include Decoders.Decode.Make (S)
end