123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 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. *)(* *)(*****************************************************************************)moduleLwt_syntax=structincludeLwtincludeLwt.SyntaxendmoduleResult_syntax=structlet(return[@ocaml.inline"always"])=funx->Okxlet(fail[@ocaml.inline"always"])=funx->Errorxletreturn_unit=Ok()letreturn_none=OkNoneletreturn_somex=Ok(Somex)letreturn_nil=Ok[]letreturn_true=Oktrueletreturn_false=Okfalselet(let*)=Result.bindlet(let+)vf=Result.mapfvletrecjoin_errorserrors=function|Ok_::ts->join_errorserrorsts|Errorerror::ts->join_errors(error::errors)ts|[]->Errorerrorsletrecjoin=function|[]->return_unit|Ok()::ts->joints|Errorerror::ts->join_errors[error]tsletallts=letrecauxacc=function|[]->Ok(Stdlib.List.revacc)|Okv::ts->aux(v::acc)ts|Errorerror::ts->join_errors[error]tsinaux[]tsletbothab=match(a,b)with|(Oka,Okb)->Ok(a,b)|(Errorerr,Ok_)|(Ok_,Errorerr)->Error[err]|(Errorerra,Errorerrb)->Error[erra;errb]endmoduleLwt_result_syntax=structlet(return[@ocaml.iniline"always"])=funx->Lwt.return(Okx)let(fail[@ocaml.iniline"always"])=funx->Lwt.return(Errorx)letreturn_unit=Lwt.return(Ok())letreturn_none=Lwt.return(OkNone)letreturn_somex=Lwt.return(Ok(Somex))letreturn_true=Lwt.return(Oktrue)letreturn_false=Lwt.return(Okfalse)letreturn_nil=Lwt.return(Ok[])let(let*)=Lwt_result.bindlet(let+)vf=Lwt_result.mapfvletlwt_map_error=Lwt_result.map_errlet(let*!)=Lwt.bindlet(let*?)rf=matchrwithOkv->fv|Error_aserr->Lwt.returnerrletjoints=letopenLwt_syntaxinlet+ps=alltsinResult_syntax.joinpsletallts=letopenLwt_syntaxinlet+ps=alltsinResult_syntax.allpsletbothab=letopenLwt_syntaxinlet+(a,b)=bothabinResult_syntax.bothabend(**/**)(* For internal use only, not advertised *)(* Like Lwt.apply but specialised for two-parameters functions *)let(lwt_apply2[@ocaml.inline"always"])=funfxy->tryfxywithexn->Lwt.failexnlet(lwt_apply3[@ocaml.inline"always"])=funfaxy->tryfaxywithexn->Lwt.failexn