package ocaml-protoc-plugin

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file result.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
(** This module provides result type and functions for compatibility
 * with OCaml 4.06 *)

type error =
  [ `Premature_end_of_input
  | `Unknown_field_type of int
  | `Wrong_field_type of string * Field.t
  | `Illegal_value of string * Field.t
  | `Unknown_enum_value of int
  | `Oneof_missing
  | `Required_field_missing ]

type 'a t = ('a, error) result

let ( >>| ) v f = match v with Ok x -> Ok (f x) | Error err -> Error err
let ( >>= ) v f = match v with Ok x -> f x | Error err -> Error err
let open_error = function
  | Ok _ as v -> v
  | Error #error as v -> v

(* Extra functions (from Base) *)

let return x = Ok x
let fail x = Error x
let get ~msg = function
  | Ok v -> v
  | Error _ -> failwith msg

let pp_error : Format.formatter -> [> error] -> unit = fun fmt -> function
  | `Premature_end_of_input ->
    Format.pp_print_string fmt
      "`Premature_end_of_input"
  | `Unknown_field_type x ->
    (Format.fprintf fmt
       "`Unknown_field_type (@[<hov>";
     (Format.fprintf fmt "%d") x;
     Format.fprintf fmt "@])")
  | `Wrong_field_type x ->
    (Format.fprintf fmt
       "`Wrong_field_type (@[<hov>";
     ((fun (a0, a1) ->
         Format.fprintf fmt "(@[";
         ((Format.fprintf fmt "%S") a0;
          Format.fprintf fmt ",@ ";
          (Field.pp fmt) a1);
         Format.fprintf fmt "@])")) x;
     Format.fprintf fmt "@])")
  | `Illegal_value x ->
    (Format.fprintf fmt
       "`Illegal_value (@[<hov>";
     ((fun (a0, a1) ->
         Format.fprintf fmt "(@[";
         ((Format.fprintf fmt "%S") a0;
          Format.fprintf fmt ",@ ";
          (Field.pp fmt) a1);
         Format.fprintf fmt "@])")) x;
     Format.fprintf fmt "@])")
  | `Unknown_enum_value x ->
    (Format.fprintf fmt
       "`Unknown_enum_value (@[<hov>";
     (Format.fprintf fmt "%d") x;
     Format.fprintf fmt "@])")
  | `Oneof_missing ->
    Format.pp_print_string fmt "`Oneof_missing"
  | `Required_field_missing ->
    Format.pp_print_string fmt
      "`Required_field_missing"
let show_error : error -> string = Format.asprintf "%a" pp_error

let pp pp fmt = function
  | Ok v -> Format.fprintf fmt "Ok %a" pp v
  | Error (#error as e) -> Format.fprintf fmt "Error %a" pp_error e
(* let show : 'a t -> string = Format.asprintf "%a" pp *)