123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(* Copyright (C) 2019--2021 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)openPrintfletidentx=xlet(%)gfx=g(fx)let(%>)fgx=g(fx)let(%>?)fgx=matchfxwithOky->gy|Error_asr->rlet(|>?)rf=matchrwithOkx->fx|Error_asr->rletrecncomposenfacc=ifn=0thenaccelsencompose(n-1)f(facc)moduleOption=structtype'at='aoptionletmapf=functionNone->None|Somex->Some(fx)letfoldf=functionNone->ident|Somex->fxletfor_allf=functionNone->true|Somex->fxendmoduleResult=structletmapf=functionOkx->Ok(fx)|Errore->ErroreendmoduleList=structincludeListletrecfoldf=function|[]->funacc->acc|x::xs->fx%>foldfxsletrecfold_rf=function|[]->funacc->Okacc|x::xs->fx%>?fold_rfxsletreciter_rf=function|[]->Ok()|x::xs->(matchfxwithOk()->iter_rfxs|Error_asr->r)letiteri_rfxs=letrecloopi=function|[]->Ok()|x::xs->(matchfixwithOk()->loop(i+1)xs|Error_asr->r)inloop0xsletrecequalfxsys=(matchxs,yswith|[],[]->true|x::xs',y::ys'->fxy&&equalfxs'ys'|[],_::_|_::_,[]->false)endletfinallycleanupthunk=letr=trythunk()withxc->cleanup();raisexcincleanup();rletdatetuple_of_iso8601s=ifString.lengths=10&&s.[4]='-'&&s.[7]='-'thentry(int_of_string(String.subs04),int_of_string(String.subs52),int_of_string(String.subs82))withFailure_->failwith"Caqti_internal.datetuple_of_iso8601"elsefailwith"Caqti_internal.datetuple_of_iso8601"letiso8601_of_datetuple(y,m,d)=sprintf"%04d-%02d-%02d"ymdletstring_of_rfc3339_error~inputerr=letbuf=Buffer.create64inletppf=Format.formatter_of_bufferbufinPtime.pp_rfc3339_errorppferr;Format.fprintfppf" in value %S."input;Format.pp_print_flushppf();Buffer.contentsbufletptime_of_rfc3339_utcs=letn=String.lengthsinlets'=ifn<13thenselseifs.[n-1]='Z'thenselseifs.[n-3]='+'||s.[n-3]='-'thens^":00"elseifs.[n-6]='+'||s.[n-6]='-'thenselses^"Z"in(matchPtime.of_rfc3339s'with|Ok(t,_,_)->Okt|Error(`RFC3339(_,err))->Error(string_of_rfc3339_error~input:s'err))letpdate_of_iso8601s=(matchPtime.of_date(datetuple_of_iso8601s)with|exceptionFailure_->Error(sprintf"Cannot parse date %S."s)|None->Error(sprintf"Date %s is out of range."s)|Somepdate->Okpdate)letiso8601_of_pdatex=iso8601_of_datetuple(Ptime.to_datex)letdefault_log_src=Logs.Src.create"caqti"letrequest_log_src=Logs.Src.create"caqti.request"moduleAlog=(valLogs.src_logdefault_log_src)