···11+open B0_kit.V000
22+open Result.Syntax
33+44+(* OCaml library names *)
55+66+let compiler_libs_toplevel = B0_ocaml.libname "compiler-libs.toplevel"
77+88+let rresult = B0_ocaml.libname "rresult"
99+let rresult_top = B0_ocaml.libname "rresult.top"
1010+1111+(* Libraries *)
1212+1313+let result_lib =
1414+ let srcs =
1515+ Fpath.[ `File (v "src/rresult.mli"); `File (v "src/rresult.ml"); ]
1616+ in
1717+ let requires = [] in
1818+ B0_ocaml.lib rresult ~doc:"The rresult library" ~srcs ~requires
1919+2020+let rresult_top =
2121+ let srcs = Fpath.[ `File (v "src/rresult_top.ml") ] in
2222+ let requires = [compiler_libs_toplevel] in
2323+ B0_ocaml.lib rresult_top ~doc:"The rresult.top library" ~srcs ~requires
2424+2525+(* Tests *)
2626+2727+let test =
2828+ let srcs = Fpath.[`File (v "test/test.ml")] in
2929+ let meta = B0_meta.(empty |> tag test) in
3030+ let requires = [ rresult ] in
3131+ B0_ocaml.exe "test" ~doc:"Test suite" ~srcs ~meta ~requires
3232+3333+(* Packs *)
3434+3535+let default =
3636+ let meta =
3737+ B0_meta.empty
3838+ |> B0_meta.(add authors) ["The rresult programmers"]
3939+ |> B0_meta.(add maintainers)
4040+ ["Daniel Bünzli <daniel.buenzl i@erratique.ch>"]
4141+ |> B0_meta.(add homepage) "https://erratique.ch/software/rresult"
4242+ |> B0_meta.(add online_doc)
4343+ "https://erratique.ch/software/rresult/doc/Rresult"
4444+ |> B0_meta.(add licenses) ["ISC"]
4545+ |> B0_meta.(add repo) "git+https://erratique.ch/repos/rresult.git"
4646+ |> B0_meta.(add issues) "https://github.com/dbuenzli/rresult/issues"
4747+ |> B0_meta.(add description_tags) ["result"; "error"; "org:erratique"]
4848+ |> B0_meta.tag B0_opam.tag
4949+ |> B0_meta.add B0_opam.build
5050+ {|[["ocaml" "pkg/pkg.ml" "build" "--dev-pkg" "%{dev}%"]]|}
5151+ |> B0_meta.add B0_opam.depends
5252+ [ "ocaml", {|>= "4.08.0"|};
5353+ "ocamlfind", {|build|};
5454+ "ocamlbuild", {|build|};
5555+ "topkg", {|build & >= "1.0.3"|} ]
5656+ in
5757+ B0_pack.make "default" ~doc:"rresult package" ~meta ~locked:true @@
5858+ B0_unit.list ()
vendor/opam/rresult/BRZO
This is a binary file and will not be displayed.
+53
vendor/opam/rresult/CHANGES.md
···11+v0.7.0 2021-10-04 Zagreb
22+------------------------
33+44+* Require OCaml >= 4.08. This drops the dependency on the `result`
55+ compatibility package.
66+* Users are encouraged to move the `Stdlib.Result` module available in
77+ OCaml 4.08.
88+99+v0.6.0 2018-10-07 Zagreb
1010+------------------------
1111+1212+* Add `R.failwith_error_msg`.
1313+1414+v0.5.0 2016-09-03 Zagreb
1515+------------------------
1616+1717+* `R.[k]ignore_error` use a thunk for the `~use` argument. This is an
1818+ incompatible change. Simply wrap occurences of `~use:e` with
1919+ `~use:(fun _ -> e)`.
2020+2121+v0.4.0 2016-05-23 La Forclaz (VS)
2222+---------------------------------
2323+2424+* Build depend on topkg.
2525+* Relicense from BSD3 to ISC.
2626+* `R.pp` remove the `pp_` prefix from the labels and do not print the
2727+ constructors names, use to the newly introduced `R.dump` for this.
2828+ This follows the conventions of `Fmt`.
2929+3030+v0.3.0 2015-11-30 Cambridge (UK)
3131+--------------------------------
3232+3333+* Use the type provided by the `Result` compatibility library. Opening
3434+ `Rresult` still gives you both unprefixed variant constructors and infix
3535+ operators.
3636+* Remove the `R.{int,nativeint,int32,int64,float,bool}_of_string` functions.
3737+ They do not belong here.
3838+* `R.map`, swap argument order. Thanks to Gabriel Radanne for suggesting.
3939+* Fix `R.bind` which had a more restrictive type than `>>=`. Thanks to
4040+ Hezekiah M. Carty for the patch.
4141+4242+4343+v0.2.0 2015-05-20 La Forclaz (VS)
4444+---------------------------------
4545+4646+* The `Rresult_infix` module no longer exists. Open directly `Rresult`
4747+ for using the library.
4848+4949+5050+v0.1.0 2015-03-19 La Forclaz (VS)
5151+---------------------------------
5252+5353+First release.
+13
vendor/opam/rresult/LICENSE.md
···11+Copyright (c) 2014 The rresult programmers
22+33+Permission to use, copy, modify, and/or distribute this software for any
44+purpose with or without fee is hereby granted, provided that the above
55+copyright notice and this permission notice appear in all copies.
66+77+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
88+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1010+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1111+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1212+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1313+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+44
vendor/opam/rresult/README.md
···11+Rresult — Result value combinators for OCaml
22+-------------------------------------------------------------------------------
33+%%VERSION%%
44+55+Rresult is an OCaml module for handling computation results and errors
66+in an explicit and declarative manner, without resorting to
77+exceptions. It defines combinators to operate on the `result` type
88+available from OCaml 4.03 in the standard library.
99+1010+OCaml 4.08 provides the `Stdlib.Result` module which you should prefer
1111+to Rresult.
1212+1313+Rresult is distributed under the ISC license.
1414+1515+Home page: http://erratique.ch/software/rresult
1616+Contact: Daniel Bünzli `<daniel.buenzl i@erratique.ch>`
1717+1818+## Installation
1919+2020+Rresult can be installed with `opam`:
2121+2222+ opam install rresult
2323+2424+If you don't use `opam` consult the [`opam`](opam) file for build
2525+instructions.
2626+2727+## Documentation
2828+2929+The documentation and API reference is automatically generated by
3030+`ocamldoc` from the interfaces. It can be consulted [online][doc]
3131+3232+[doc]: http://erratique.ch/software/rresult/doc/
3333+3434+3535+## Sample programs
3636+3737+If you installed Rresult with `opam` sample programs are located in
3838+the directory `opam config var rresult:doc`.
3939+4040+In the distribution sample programs and tests are located in the
4141+[`test`](test) directory of the distribution. They can be built and run
4242+with:
4343+4444+ topkg build --tests true && topkg test
+5
vendor/opam/rresult/_tags
···11+true : bin_annot, safe_string
22+<_b0> : -traverse
33+<src> : include
44+<src/rresult_top*> : package(compiler-libs.toplevel)
55+<test> : include
···11+opam-version: "2.0"
22+name: "rresult"
33+synopsis: """Result value combinators for OCaml"""
44+maintainer: ["Daniel Bünzli <daniel.buenzl i@erratique.ch>"]
55+authors: ["The rresult programmers"]
66+homepage: "https://erratique.ch/software/rresult"
77+doc: "https://erratique.ch/software/rresult/doc/Rresult"
88+dev-repo: "git+https://erratique.ch/repos/rresult.git"
99+bug-reports: "https://github.com/dbuenzli/rresult/issues"
1010+license: ["ISC"]
1111+tags: ["result" "error" "org:erratique"]
1212+depends: ["ocaml" {>= "4.08.0"}
1313+ "ocamlfind" {build}
1414+ "ocamlbuild" {build}
1515+ "topkg" {build & >= "1.0.3"}]
1616+build: [["ocaml" "pkg/pkg.ml" "build" "--dev-pkg" "%{dev}%"]]
1717+description: """
1818+Rresult is an OCaml module for handling computation results and errors
1919+in an explicit and declarative manner, without resorting to
2020+exceptions. It defines combinators to operate on the `result` type
2121+available from OCaml 4.03 in the standard library.
2222+2323+OCaml 4.08 provides the `Stdlib.Result` module which you should prefer
2424+to Rresult.
2525+2626+Rresult is distributed under the ISC license.
2727+2828+Home page: http://erratique.ch/software/rresult
2929+Contact: Daniel Bünzli `<daniel.buenzl i@erratique.ch>`"""
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2015 The rresult programmers. All rights reserved.
33+ Distributed under the ISC license, see terms at the end of the file.
44+ ---------------------------------------------------------------------------*)
55+66+type ('a, 'b) result = ('a, 'b) Stdlib.result = Ok of 'a | Error of 'b
77+88+module R = struct
99+1010+ let err_error = "result value is (Error _)"
1111+ let err_ok = "result value is (Ok _)"
1212+1313+ (* Results *)
1414+1515+ type ('a, 'b) t = ('a, 'b) result
1616+ let ok v = Ok v
1717+ let error e = Error e
1818+ let get_ok = function Ok v -> v | Error _ -> invalid_arg err_error
1919+ let get_error = function Error e -> e | Ok _ -> invalid_arg err_ok
2020+ let reword_error reword = function
2121+ | Ok _ as r -> r
2222+ | Error e -> Error (reword e)
2323+2424+ let return = ok
2525+ let fail = error
2626+2727+ (* Composing results *)
2828+2929+ let bind v f = match v with Ok v -> f v | Error _ as e -> e
3030+ let map f v = match v with Ok v -> Ok (f v) | Error _ as e -> e
3131+ let join r = match r with Ok v -> v | Error _ as e -> e
3232+ let ( >>= ) = bind
3333+ let ( >>| ) v f = match v with Ok v -> Ok (f v) | Error _ as e -> e
3434+3535+ module Infix = struct
3636+ let ( >>= ) = ( >>= )
3737+ let ( >>| ) = ( >>| )
3838+ end
3939+4040+ (* Error messages *)
4141+4242+ let pp_lines ppf s = (* hints new lines *)
4343+ let left = ref 0 and right = ref 0 and len = String.length s in
4444+ let flush () =
4545+ Format.pp_print_string ppf (String.sub s !left (!right - !left));
4646+ incr right; left := !right;
4747+ in
4848+ while (!right <> len) do
4949+ if s.[!right] = '\n' then (flush (); Format.pp_force_newline ppf ()) else
5050+ incr right;
5151+ done;
5252+ if !left <> len then flush ()
5353+5454+ type msg = [ `Msg of string ]
5555+ let msg s = `Msg s
5656+ let msgf fmt =
5757+ let kmsg _ = `Msg (Format.flush_str_formatter ()) in
5858+ Format.kfprintf kmsg Format.str_formatter fmt
5959+6060+ let pp_msg ppf (`Msg msg) = pp_lines ppf msg
6161+6262+ let error_msg s = Error (`Msg s)
6363+ let error_msgf fmt =
6464+ let kerr _ = Error (`Msg (Format.flush_str_formatter ())) in
6565+ Format.kfprintf kerr Format.str_formatter fmt
6666+6767+ let reword_error_msg ?(replace = false) reword = function
6868+ | Ok _ as r -> r
6969+ | Error (`Msg e) ->
7070+ let (`Msg e' as v) = reword e in
7171+ if replace then Error v else error_msgf "%s\n%s" e e'
7272+7373+ let error_to_msg ~pp_error = function
7474+ | Ok _ as r -> r
7575+ | Error e -> error_msgf "%a" pp_error e
7676+7777+ let error_msg_to_invalid_arg = function
7878+ | Ok v -> v
7979+ | Error (`Msg m) -> invalid_arg m
8080+8181+ let open_error_msg = function Ok _ as r -> r | Error (`Msg _) as r -> r
8282+ let failwith_error_msg = function Ok v -> v | Error (`Msg m) -> failwith m
8383+8484+ (* Trapping unexpected exceptions *)
8585+8686+ type exn_trap = [ `Exn_trap of exn * Printexc.raw_backtrace ]
8787+ let pp_exn_trap ppf (`Exn_trap (exn, bt)) =
8888+ Format.fprintf ppf "%s@\n" (Printexc.to_string exn);
8989+ pp_lines ppf (Printexc.raw_backtrace_to_string bt)
9090+9191+ let trap_exn f v = try Ok (f v) with
9292+ | e ->
9393+ let bt = Printexc.get_raw_backtrace () in
9494+ Error (`Exn_trap (e, bt))
9595+9696+ let error_exn_trap_to_msg = function
9797+ | Ok _ as r -> r
9898+ | Error trap ->
9999+ error_msgf "Unexpected exception:@\n%a" pp_exn_trap trap
100100+101101+ let open_error_exn_trap = function
102102+ | Ok _ as r -> r | Error (`Exn_trap _) as r -> r
103103+104104+ (* Pretty-printing *)
105105+106106+ let pp ~ok ~error ppf = function Ok v -> ok ppf v | Error e -> error ppf e
107107+ let dump ~ok ~error ppf = function
108108+ | Ok v -> Format.fprintf ppf "@[<2>Ok@ @[%a@]@]" ok v
109109+ | Error e -> Format.fprintf ppf "@[<2>Error@ @[%a@]@]" error e
110110+111111+ (* Predicates *)
112112+113113+ let is_ok = function Ok _ -> true | Error _ -> false
114114+ let is_error = function Ok _ -> false | Error _ -> true
115115+116116+ let equal ~ok ~error r r' = match r, r' with
117117+ | Ok v, Ok v' -> ok v v'
118118+ | Error e, Error e' -> error e e'
119119+ | _ -> false
120120+121121+ let compare ~ok ~error r r' = match r, r' with
122122+ | Ok v, Ok v' -> ok v v'
123123+ | Error v, Error v' -> error v v'
124124+ | Ok _, Error _ -> -1
125125+ | Error _, Ok _ -> 1
126126+127127+ (* Converting *)
128128+129129+ let to_option = function Ok v -> Some v | Error e -> None
130130+ let of_option ~none = function None -> none () | Some v -> Ok v
131131+ let to_presult = function Ok v -> `Ok v | Error e -> `Error e
132132+ let of_presult = function `Ok v -> Ok v | `Error e -> Error e
133133+134134+ (* Ignoring errors *)
135135+136136+ let ignore_error ~use = function Ok v -> v | Error e -> use e
137137+ let kignore_error ~use = function Ok _ as r -> r | Error e -> use e
138138+end
139139+140140+include R.Infix
141141+142142+(*---------------------------------------------------------------------------
143143+ Copyright (c) 2015 The rresult programmers
144144+145145+ Permission to use, copy, modify, and/or distribute this software for any
146146+ purpose with or without fee is hereby granted, provided that the above
147147+ copyright notice and this permission notice appear in all copies.
148148+149149+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
150150+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
151151+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
152152+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
153153+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
154154+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
155155+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
156156+ ---------------------------------------------------------------------------*)
+308
vendor/opam/rresult/src/rresult.mli
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2014 The rresult programmers. All rights reserved.
33+ Distributed under the ISC license, see terms at the end of the file.
44+ ---------------------------------------------------------------------------*)
55+66+(** Result value combinators.
77+88+ {b Note.} OCaml 4.08 provides the {!Stdlib.Result} module
99+ which you should prefer to [Rresult].
1010+1111+ [Rresult] is a module for handling computation results and errors
1212+ in an explicit and declarative manner without resorting to
1313+ exceptions. It defines a {!result} type equal to OCaml 4.03's
1414+ [result] type and {{!R}combinators} to operate on these values.
1515+1616+ Open the module to use it, this defines the {{!result}result type},
1717+ the {!R.Infix} operators {!R} in your scope.
1818+1919+ Consult {{!usage}usage guidelines} for the type. *)
2020+2121+(** {1 Results} *)
2222+2323+(** The type for results. *)
2424+type ('a, 'b) result = ('a, 'b) Stdlib.result = Ok of 'a | Error of 'b
2525+2626+val ( >>= ) : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
2727+(** [(>>=)] is {!R.(>>=)}. *)
2828+2929+val ( >>| ) : ('a, 'b) result -> ('a -> 'c) -> ('c, 'b) result
3030+(** [(>>|)] is {!R.(>>|)}. *)
3131+3232+(** Result value combinators. *)
3333+module R : sig
3434+3535+ (** {1 Results} *)
3636+3737+ type ('a, 'b) t = ('a, 'b) result
3838+ (** The type for results. *)
3939+4040+ val ok : 'a -> ('a, 'b) result
4141+ (** [ok v] is [Ok v]. *)
4242+4343+ val error : 'b -> ('a, 'b) result
4444+ (** [error e] is [Error e]. *)
4545+4646+ val reword_error : ('b -> 'c) -> ('a, 'b) result -> ('a, 'c) result
4747+ (** [reword_error reword r] is:
4848+ {ul
4949+ {- [r] if [r = Ok v]}
5050+ {- [Error (reword e)] if [r = Error e]}} *)
5151+5252+ val get_ok : ('a, 'b) result -> 'a
5353+ (** [get_ok r] is [v] if [r = Ok v] and raises [Invalid_argument]
5454+ otherwise. *)
5555+5656+ val get_error : ('a, 'b) result -> 'b
5757+ (** [get_error r] is [e] if [r = Error e] and raises [Invalid_argument]
5858+ otherwise. *)
5959+6060+ (**/**)
6161+ val return : 'a -> ('a, 'b) result
6262+ val fail : 'b -> ('a, 'b) result
6363+ (**/**)
6464+6565+ (** {1 Composing results} *)
6666+6767+ val bind : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
6868+ (** [bind r f] is [f v] if [r = Ok v] and [r] if [r = Error _]. *)
6969+7070+ val map : ('a -> 'c) -> ('a, 'b) result -> ('c, 'b) result
7171+ (** [map f r] is [bind (fun v -> ret (f v))] r. *)
7272+7373+ val join : (('a, 'b) result, 'b) result -> ('a, 'b) result
7474+ (** [join r] is [v] if [r = Ok v] and [r] otherwise. *)
7575+7676+ val ( >>= ) : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
7777+ (** [r >>= f] is {!bind}[ r f]. *)
7878+7979+ val ( >>| ) : ('a, 'b) result -> ('a -> 'c) -> ('c, 'b) result
8080+ (** [r >>| f] is {!map}[ r f]. *)
8181+8282+ (** Infix operators.
8383+8484+ Gathers {!R}'s infix operators. *)
8585+ module Infix : sig
8686+8787+ (** {1 Infix operators} *)
8888+8989+ val ( >>= ) : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
9090+ (** [(>>=)] is {!R.(>>=)}. *)
9191+9292+ val ( >>| ) : ('a, 'b) result -> ('a -> 'c) -> ('c, 'b) result
9393+ (** [(>>|)] is {!R.(>>|)}. *)
9494+ end
9595+9696+ (** {1:msgs Error messages} *)
9797+9898+ type msg = [ `Msg of string ]
9999+ (** The type for (error) messages. *)
100100+101101+ val msg : string -> [> msg]
102102+ (** [msg s] is [`Msg s]. *)
103103+104104+ val msgf : ('a, Format.formatter, unit, [> msg]) format4 -> 'a
105105+ (** [msgf fmt ...] formats a message according to [fmt]. *)
106106+107107+ val pp_msg : Format.formatter -> msg -> unit
108108+ (** [pp_msg ppf m] prints [m] on [ppf]. *)
109109+110110+ val error_msg : string -> ('a, [> msg]) result
111111+ (** [error_msg s] is [error (`Msg s)]. *)
112112+113113+ val error_msgf : ('a, Format.formatter, unit, ('b, [> msg]) result)
114114+ format4 -> 'a
115115+ (** [error_msgf fmt ...] is an error message formatted according to [fmt]. *)
116116+117117+ val reword_error_msg : ?replace:bool -> (string -> msg) ->
118118+ ('a, msg) result -> ('a, [> msg]) result
119119+ (** [reword_error_msg ~replace reword r] is like {!reword_error} except
120120+ if [replace] is [false] (default), the result of [reword old_msg] is
121121+ concatened, on a new line to the old message. *)
122122+123123+ val error_to_msg : pp_error:(Format.formatter -> 'b -> unit) ->
124124+ ('a, 'b) result -> ('a, [> msg]) result
125125+ (** [error_to_msg ~pp_error r] converts errors in [r] with [pp_error] to
126126+ an error message. *)
127127+128128+ val error_msg_to_invalid_arg : ('a, msg) result -> 'a
129129+ (** [err_msg_to_invalid_arg r] is [v] if [r = Ok v] and
130130+131131+ @raise Invalid_argument with the error message otherwise. *)
132132+133133+ val open_error_msg : ('a, msg) result -> ('a, [> msg]) result
134134+ (** [open_error_msg r] allows to combine a closed error message
135135+ variant with other variants. *)
136136+137137+ val failwith_error_msg : ('a, msg) result -> 'a
138138+ (** [failwith_error_msg r] raises [Failure m] if [r] is
139139+ [Error (`Msg m)]. *)
140140+141141+ (** {1:exn Trapping unexpected exceptions}
142142+143143+ {e Getting rid of [null] was not enough}. *)
144144+145145+ type exn_trap = [ `Exn_trap of exn * Printexc.raw_backtrace ]
146146+ (** The type for exception traps. *)
147147+148148+ val pp_exn_trap : Format.formatter -> exn_trap -> unit
149149+ (** [pp_exn_trap ppf bt] prints [bt] on [ppf]. *)
150150+151151+ val trap_exn : ('a -> 'b) -> 'a -> ('b, [> exn_trap]) result
152152+ (** [trap_exn f v] is [f v] and traps any exception that may occur as
153153+ an exception trap error. *)
154154+155155+ val error_exn_trap_to_msg : ('a, exn_trap) result -> ('a, [> msg]) result
156156+ (** [error_exn_trap_to_msg r] converts exception trap errors in
157157+ [r] to an error message. *)
158158+159159+ val open_error_exn_trap : ('a, exn_trap) result -> ('a, [> exn_trap]) result
160160+ (** [open_error_exn_trap r] allows to combine a closed exception trap error
161161+ variant with other variants. *)
162162+163163+ (** {1:print Pretty printing} *)
164164+165165+ val pp :
166166+ ok:(Format.formatter -> 'a -> unit) ->
167167+ error:(Format.formatter -> 'b -> unit) -> Format.formatter ->
168168+ ('a, 'b) result -> unit
169169+ (** [pp ~ok ~error ppf r] prints [r] on [ppf] using [ok] and [error]
170170+ according to [r]. *)
171171+172172+ val dump :
173173+ ok:(Format.formatter -> 'a -> unit) ->
174174+ error:(Format.formatter -> 'b -> unit) -> Format.formatter ->
175175+ ('a, 'b) result -> unit
176176+ (** [dump ~ok ~error] formats an OCaml result value using [ok] or [error]
177177+ according to case, no parentheses are added. *)
178178+179179+ (** {1:pred Predicates and comparison} *)
180180+181181+ val is_ok : ('a, 'b) result -> bool
182182+ (** [is_ok r] is [true] iff [r = Ok _]. *)
183183+184184+ val is_error : ('a, 'b) result -> bool
185185+ (** [is_error r] is [true] iff [r = Error _]. *)
186186+187187+ val equal : ok:('a -> 'a -> bool) -> error:('b -> 'b -> bool) ->
188188+ ('a, 'b) result -> ('a, 'b) result -> bool
189189+ (** [equal ~ok ~error r r'] tests [r] and [r'] for equality using [ok]
190190+ and [error]. *)
191191+192192+ val compare : ok:('a -> 'a -> int) -> error:('b -> 'b -> int) ->
193193+ ('a, 'b) result -> ('a, 'b) result -> int
194194+ (** [compare ~ok ~error r r'] totally orders [r] and [r'] using [ok]
195195+ and [error]. *)
196196+197197+ (** {1:convert Converting} *)
198198+199199+ val to_option : ('a, 'b) result -> 'a option
200200+ (** [to_option r] is [Some v] if [r = Ok v] and [None] otherwise. *)
201201+202202+ val of_option : none:(unit -> ('a, 'b) result) -> 'a option -> ('a, 'b) result
203203+ (** [of_option ~none r] is [Ok v] if [r = Some v] and [none ()] otherwise. *)
204204+205205+ val to_presult : ('a, 'b) result -> [> `Ok of 'a | `Error of 'b ]
206206+ (** [to_presult r] is [r] as a polymorphic variant result value. *)
207207+208208+ val of_presult : [< `Ok of 'a | `Error of 'b ] -> ('a, 'b) result
209209+ (** [of_presult pr] is [pr] as a result value. *)
210210+211211+ (** {1:ignore Ignoring errors}
212212+213213+ {b Warning.} Using these functions is, most of the time, a bad idea. *)
214214+215215+ val ignore_error : use:('b -> 'a) -> ('a, 'b) result -> 'a
216216+ (** [ignore_error ~use r] is [v] if [r = Ok v] and [use e] if
217217+ [r = Error e]. *)
218218+219219+ val kignore_error :
220220+ use:('b -> ('a, 'c) result) -> ('a, 'b) result -> ('a, 'c) result
221221+ (** [kignore_error ~use r] is [r] if [r = Ok v] and [use e] if
222222+ [r = Error e]. *)
223223+end
224224+225225+(** {1:usage Usage design guidelines}
226226+227227+ These are rough design guidelines, don't forget to think.
228228+229229+ {2 Error messages}
230230+231231+ Use {{!R.msgs}error messages} if:
232232+ {ol
233233+ {- Your error messages don't need to be localized, e.g. scripts,
234234+ command line programs.}
235235+ {- The errors don't need to be processed. They are just meant to
236236+ be logged at certain point in your program.}}
237237+238238+ If the above doesn't hold and your errors need to be processed for
239239+ localization or error recovery then use a custom error type in your
240240+ result values.
241241+242242+ {2 Custom error types}
243243+244244+ If your module has specific errors then define an error type, and
245245+ a result type that tags this error type with the library name (or
246246+ any other tag that may make sense, see for example {!R.exn}) along
247247+ with the following functions:
248248+249249+{[
250250+module Mod : sig
251251+ type error = ...
252252+ type 'a result = ('a, [`Mod of error]) Rresult.result
253253+ val pp_error : Format.formatter -> [`Mod of error] -> unit
254254+ val open_error : 'a result -> ('a, [> `Mod of error]) Rresult.result
255255+ val error_to_msg : 'a result -> ('a, Rresult.R.msg) Rresult.result
256256+257257+ val f : ... -> 'a result
258258+end
259259+]}
260260+261261+If your library has generic errors that may be useful in other context
262262+or shared among modules and to be composed together, then define your
263263+error type itself as being a variant and return these values
264264+without tagging them.
265265+{[
266266+module Mod : sig
267267+ type error = [`Generic of ... | ... ]
268268+ type 'a result = ('a, error) Rresult.result
269269+ val pp_error : Format.formatter -> error -> unit
270270+ val open_error : 'a result -> ('a, [> error]) Rresult.result
271271+ val error_to_msg : 'a result -> ('a, Rresult.R.msg) Rresult.result
272272+273273+ val f : ... -> 'a result
274274+end
275275+]}
276276+In the latter case it may still be useful to provide a function to
277277+tag these errors whenever they reach a certain point of the program.
278278+For this the following function could be added to [Mod]:
279279+{[
280280+val pack_error : 'a result -> ('a, [> `Mod of error]) Rresult.result
281281+]}
282282+You should then provide the following functions aswell, so that
283283+the packed error composes well in the system:
284284+{[
285285+val pp_pack_error : Format.formatter -> [ `Mod of error] -> unit
286286+val open_pack_error : ('a, [ `Mod of error]) Rresult.result ->
287287+ ('a, [> `Mod of error]) Rresult.result
288288+289289+val error_pack_to_msg : ('a, [ `Mod of error]) Rresult.result ->
290290+ ('a, Rresult.R.msg) Rresult.result
291291+]}
292292+*)
293293+294294+(*---------------------------------------------------------------------------
295295+ Copyright (c) 2014 The rresult programmers
296296+297297+ Permission to use, copy, modify, and/or distribute this software for any
298298+ purpose with or without fee is hereby granted, provided that the above
299299+ copyright notice and this permission notice appear in all copies.
300300+301301+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
302302+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
303303+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
304304+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
305305+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
306306+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
307307+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
308308+ ---------------------------------------------------------------------------*)
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2015 The rresult programmers. All rights reserved.
33+ Distributed under the ISC license, see terms at the end of the file.
44+ ---------------------------------------------------------------------------*)
55+66+let () = ignore (Toploop.use_file Format.err_formatter "rresult_top_init.ml")
77+88+(*---------------------------------------------------------------------------
99+ Copyright (c) 2015 The rresult programmers
1010+1111+ Permission to use, copy, modify, and/or distribute this software for any
1212+ purpose with or without fee is hereby granted, provided that the above
1313+ copyright notice and this permission notice appear in all copies.
1414+1515+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1616+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1717+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1818+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1919+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2020+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2121+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2222+ ---------------------------------------------------------------------------*)
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2015 The rresult programmers. All rights reserved.
33+ Distributed under the ISC license, see terms at the end of the file.
44+ ---------------------------------------------------------------------------*)
55+66+open Rresult;;
77+88+(*---------------------------------------------------------------------------
99+ Copyright (c) 2015 The rresult programmers
1010+1111+ Permission to use, copy, modify, and/or distribute this software for any
1212+ purpose with or without fee is hereby granted, provided that the above
1313+ copyright notice and this permission notice appear in all copies.
1414+1515+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1616+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1717+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1818+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1919+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2020+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2121+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2222+ ---------------------------------------------------------------------------*)
+151
vendor/opam/rresult/test/test.ml
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2015 The rresult programmers. All rights reserved.
33+ Distributed under the ISC license, see terms at the end of the file.
44+ ---------------------------------------------------------------------------*)
55+66+open Rresult
77+88+let log f = Format.printf (f ^^ "@.")
99+1010+let raises_invalid_arg f x =
1111+ try f x; raise Exit with
1212+ | Invalid_argument _ -> ()
1313+ | e -> assert false
1414+1515+let test_constructors () =
1616+ log "Test R.{ok,error}";
1717+ assert (R.ok 3 = Ok 3);
1818+ assert (R.error `An_error = Error `An_error);
1919+ ()
2020+2121+let test_reword_error () =
2222+ log "Test R.reword_error";
2323+ let reword `An_error = `Another_one in
2424+ assert (R.reword_error reword (Ok 3) = Ok 3);
2525+ assert (R.reword_error reword (Error `An_error) = (Error `Another_one));
2626+ ()
2727+2828+let test_gets () =
2929+ log "Test R.get_{ok,error}";
3030+ assert (R.get_ok (Ok 2) = 2);
3131+ assert (R.get_error (Error 3) = 3);
3232+ raises_invalid_arg R.get_ok (Error 3);
3333+ raises_invalid_arg R.get_error (Ok 2);
3434+ ()
3535+3636+let test_bind () =
3737+ log "Test R.bind";
3838+ assert (R.bind (Ok 3) (fun v -> Error (string_of_int v)) = Error "3");
3939+ assert (R.bind (Ok 3) (fun v -> Ok (string_of_int v)) = Ok "3");
4040+ assert (R.bind (Error 1) (fun v -> Ok (string_of_int v)) = Error 1);
4141+ ()
4242+4343+let test_map () =
4444+ log "Test R.map";
4545+ assert (R.map (fun v -> string_of_int v) (Ok 2) = Ok "2");
4646+ assert (R.map (fun v -> string_of_int v) (Error 2) = Error 2);
4747+ ()
4848+4949+let test_join () =
5050+ log "Test R.join";
5151+ assert (R.join (Ok (Ok 3)) = Ok 3);
5252+ assert (R.join (Ok (Error 2)) = Error 2);
5353+ assert (R.join (Error 3) = Error 3);
5454+ assert (R.join (Error 4) = Error 4);
5555+ ()
5656+5757+let test_msgs () =
5858+ log "Test error messages.";
5959+ assert (R.msg "bla" = `Msg "bla");
6060+ assert (R.msgf "bla%d" 3 = `Msg "bla3");
6161+ assert (R.error_msg "bla" = Error (`Msg "bla"));
6262+ assert (R.error_msgf "bla%d" 3 = Error (`Msg "bla3"));
6363+ let reword s = `Msg (s ^ "++") in
6464+ assert (R.reword_error_msg ~replace:true reword (Ok 2) = Ok 2);
6565+ assert (R.reword_error_msg ~replace:false reword (Ok 2) = Ok 2);
6666+ assert (R.reword_error_msg ~replace:true reword
6767+ (Error (`Msg "ha")) = (Error (`Msg "ha++")));
6868+ assert (R.reword_error_msg ~replace:false reword
6969+ (Error (`Msg "ha")) = (Error (`Msg "ha\nha++")));
7070+ let pp_error ppf = function `E -> Format.fprintf ppf "E" in
7171+ assert (R.error_to_msg ~pp_error (Ok 2) = (Ok 2));
7272+ assert (R.error_to_msg ~pp_error (Error `E) = (Error (`Msg "E")));
7373+ assert (R.error_msg_to_invalid_arg (Ok 2) = 2);
7474+ raises_invalid_arg R.error_msg_to_invalid_arg (Error (`Msg "E"));
7575+ ()
7676+7777+let test_exn_trap () =
7878+ log "Test trapping unexpected exceptions.";
7979+ let no_raise x = string_of_int x in
8080+ let do_raise x = raise Exit in
8181+ assert (R.trap_exn no_raise 3 = Ok "3");
8282+ begin match R.trap_exn do_raise 3 with
8383+ | Ok _ -> assert false
8484+ | Error (`Exn_trap (Exit, _)) -> ()
8585+ | Error _ -> assert false
8686+ end;
8787+ ()
8888+8989+let test_is () =
9090+ log "Test R.is_{ok,error}";
9191+ assert (R.is_ok (Ok 2));
9292+ assert (not @@ R.is_ok (Error 2));
9393+ assert (R.is_error (Error 2));
9494+ assert (not @@ R.is_error (Ok 2));
9595+ ()
9696+9797+let test_converting () =
9898+ log "Test R.{to,of}_{option,presult}";
9999+ assert (R.to_option (Ok 3) = Some 3);
100100+ assert (R.to_option (Error 3) = None);
101101+ assert (R.of_option ~none:(fun () -> Error "none") (Some 3) = Ok 3);
102102+ assert (R.of_option ~none:(fun () -> Error "none") (None) = Error "none");
103103+ assert (R.to_presult (Ok 3) = (`Ok 3));
104104+ assert (R.to_presult (Error 3) = (`Error 3));
105105+ assert (R.of_presult (`Ok 3) = (Ok 3));
106106+ assert (R.of_presult (`Error 3) = (Error 3));
107107+ ()
108108+109109+let test_ignoring () =
110110+ log "Test.[k]ignore_error";
111111+ assert (R.ignore_error ~use:(fun _ -> 3) (Ok 4) = 4);
112112+ assert (R.ignore_error ~use:(fun _ -> 3) (Error 4) = 3);
113113+ assert (R.kignore_error ~use:(fun _ -> Ok 3) (Ok 4) = (Ok 4));
114114+ assert (R.kignore_error ~use:(fun _ -> Ok 3) (Error 4) = (Ok 3));
115115+ assert (R.kignore_error ~use:(fun _ -> Error 3) (Ok 4) = (Ok 4));
116116+ assert (R.kignore_error ~use:(fun _ -> Error 3) (Error 4) = (Error 3));
117117+ ()
118118+119119+let tests () =
120120+ test_constructors ();
121121+ test_reword_error ();
122122+ test_gets ();
123123+ test_bind ();
124124+ test_map ();
125125+ test_join ();
126126+ test_msgs ();
127127+ test_exn_trap ();
128128+ test_is ();
129129+ test_converting ();
130130+ test_ignoring ();
131131+ ()
132132+133133+let () =
134134+ tests ();
135135+ log "All tests succeeded."
136136+137137+(*---------------------------------------------------------------------------
138138+ Copyright (c) 2015 The rresult programmers
139139+140140+ Permission to use, copy, modify, and/or distribute this software for any
141141+ purpose with or without fee is hereby granted, provided that the above
142142+ copyright notice and this permission notice appear in all copies.
143143+144144+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
145145+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
146146+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
147147+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
148148+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149149+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
150150+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
151151+ ---------------------------------------------------------------------------*)