123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195(*****************************************************************************)(* *)(* 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.Syntaxletreturn_ok_unit=Lwt.return(Ok())letreturn_ok_true=Lwt.return(Oktrue)letreturn_ok_false=Lwt.return(Okfalse)letreturn_ok_none=Lwt.return(OkNone)letreturn_ok_nil=Lwt.return(Ok[])endmoduleOption_syntax=structlet(return[@ocaml.inline"always"])=funx->Somexlet(fail[@ocaml.inline"always"])=Noneletreturn_unit=return()letreturn_nil=return[]letreturn_true=returntrueletreturn_false=returnfalselet(let*)=Stdlib.Option.bindlet(let+)of=Stdlib.Option.mapfoletbothab=match(a,b)withSomex,Somey->Some(x,y)|_->Nonelet(and*)=bothlet(and+)=bothendmoduleResult_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_option_syntax=structlet(return[@ocaml.inline"always"])=funx->Lwt.return(Somex)let(fail[@ocaml.inline"always"])=Lwt.returnNoneletreturn_unit=Lwt_syntax.return_some()letreturn_true=Lwt_syntax.return_sometrueletreturn_false=Lwt_syntax.return_somefalseletreturn_nil=Lwt_syntax.return_some[]letbothab=letopenLwt_syntaxinlet+a,b=bothabinOption_syntax.bothablet(let*)lof=Lwt.bindlo(functionNone->fail|Somex->fx)let(and*)=bothlet(let+)lof=Lwt.map(Stdlib.Option.mapf)lolet(and+)=bothlet(let*!)=Lwt.bindlet(let*?)of=matchowithSomex->fx|None->failendmoduleLwt_result_syntax=structlet(return[@ocaml.inline"always"])=funx->Lwt.return(Okx)let(fail[@ocaml.inline"always"])=funx->Lwt.return(Errorx)letreturn_unit=Lwt_syntax.return_ok_unitletreturn_none=Lwt_syntax.return_ok_noneletreturn_somex=Lwt.return(Ok(Somex))letreturn_true=Lwt_syntax.return_ok_trueletreturn_false=Lwt_syntax.return_ok_falseletreturn_nil=Lwt_syntax.return_ok_nillet(let*)=Lwt_result.bindlet(let+)vf=Lwt_result.mapfvletlwt_map_error=Lwt_result.map_errorlet(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