123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020-2021 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(** A replacement for {!Stdlib.Result} which
- is exception-safe,
- includes Lwt-, result-, and Lwt-result-aware traversors.
See {!Lwtreslib} and {!Seq} for general description of traversors and the
meaning of [_s], [_e], and [_es] suffixes. *)moduletypeS=sigtype('a,'e)t=('a,'e)result=Okof'a|Errorof'e(***)(* We do not provide all of the [_e] and [_es] functions that you might expect
based on other modules such as [Option]. This is because the double
layering of result is rarely necessary and generally discouraged. It is
possible to achieve manually. *)valok:'a->('a,'e)resultvalok_s:'a->('a,'e)resultLwt.tvalerror:'e->('a,'e)resultvalerror_s:'e->('a,'e)resultLwt.tvalvalue:('a,'e)result->default:'a->'avalvalue_f:('a,'e)result->default:(unit->'a)->'avalbind:('a,'e)result->('a->('b,'e)result)->('b,'e)resultvalbind_error:('a,'e)result->('e->('a,'f)result)->('a,'f)resultvalmap:('a->'b)->('a,'e)result->('b,'e)resultvalmap_error:('e->'f)->('a,'e)result->('a,'f)resultvalbind_s:('a,'e)result->('a->('b,'e)resultLwt.t)->('b,'e)resultLwt.tvalbind_error_s:('a,'e)result->('e->('a,'f)resultLwt.t)->('a,'f)resultLwt.tvaljoin:(('a,'e)result,'e)result->('a,'e)result(* NOTE: [map_e] is [bind] *)valmap_e:('a->('b,'e)result)->('a,'e)result->('b,'e)resultvalmap_s:('a->'bLwt.t)->('a,'e)result->('b,'e)resultLwt.t(* NOTE: [map_es] is [bind_s] *)valmap_es:('a->('b,'e)resultLwt.t)->('a,'e)result->('b,'e)resultLwt.t(* NOTE: [map_error_e] is [bind_error] *)valmap_error_e:('e->('a,'f)result)->('a,'e)result->('a,'f)resultvalmap_error_s:('e->'fLwt.t)->('a,'e)result->('a,'f)resultLwt.t(* NOTE: [map_error_es] is [bind_error_s] *)valmap_error_es:('e->('a,'f)resultLwt.t)->('a,'e)result->('a,'f)resultLwt.tvalfold:ok:('a->'c)->error:('e->'c)->('a,'e)result->'cvaliter:('a->unit)->('a,'e)result->unitvaliter_s:('a->unitLwt.t)->('a,'e)result->unitLwt.tvaliter_error:('e->unit)->('a,'e)result->unitvaliter_error_s:('e->unitLwt.t)->('a,'e)result->unitLwt.tvalis_ok:('a,'e)result->boolvalis_error:('a,'e)result->boolvalequal:ok:('a->'a->bool)->error:('e->'e->bool)->('a,'e)result->('a,'e)result->boolvalcompare:ok:('a->'a->int)->error:('e->'e->int)->('a,'e)result->('a,'e)result->intvalto_option:('a,'e)result->'aoptionvalof_option:error:'e->'aoption->('a,'e)resultvalto_list:('a,'e)result->'alistvalto_seq:('a,'e)result->'aStdlib.Seq.t(** [catch f] is [try Ok (f ()) with e -> Error e]: it is [Ok x] if [f ()]
evaluates to [x], and it is [Error e] if [f ()] raises [e].
See {!WithExceptions.S.Result.to_exn} for a converse function.
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)valcatch:?catch_only:(exn->bool)->(unit->'a)->('a,exn)result(** [catch_f f handler] is equivalent to [map_error (catch f) handler].
In other words, it catches exceptions in [f ()] and either returns the
value in an [Ok] or passes the exception to [handler] for the [Error].
No attempt is made to catch the exceptions raised by [handler].
[catch_only] has the same use as with [catch]. The same restriction on
catching non-deterministic runtime exceptions applies. *)valcatch_f:?catch_only:(exn->bool)->(unit->'a)->(exn->'e)->('a,'e)result(** [catch_ef f handler] is equivalent to [join @@ map_error (catch f) handler].
In other words, it catches exceptions in [f ()] and either returns the
value as is or passes the exception to [handler] for the [Error]. The
handler must return an error of the same type as that carried by [f ()].
No attempt is made to catch the exceptions raised by [handler].
[catch_only] has the same use as with [catch]. The same restriction on
catching non-deterministic runtime exceptions applies. *)valcatch_ef:?catch_only:(exn->bool)->(unit->('a,'error)result)->(exn->'error)->('a,'error)result(** [catch_s] is [catch] but for Lwt promises. Specifically, [catch_s f]
returns a promise that resolves to [Ok x] if and when [f ()] resolves to
[x], or to [Error exc] if and when [f ()] is rejected with [exc].
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)valcatch_s:?catch_only:(exn->bool)->(unit->'aLwt.t)->('a,exn)resultLwt.t(** We do not provide [catch_s_f] because (a) the suffix becomes confusing,
(b) it's not used, (c) it is not obvious whether we want the handler to be
within Lwt (gives more flexibility) or not (gives more guarantee about the
timeliness of learning about rejections). We will revisit this if a needs
for it arises. *)(**/**)(* for backwards compatibility *)valreturn:'a->('a,'e)resultvalreturn_unit:(unit,'e)resultvalreturn_none:('aoption,'e)resultvalreturn_some:'a->('aoption,'e)resultvalreturn_nil:('alist,'e)resultvalreturn_true:(bool,'e)resultvalreturn_false:(bool,'e)resultend