123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382(* Copyright (C) 2017--2025 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.
*)openShimsexceptionRejectofstringmodulePrivate=structtype_record_serial=..type'aproduct_id={serial:'arecord_serial;is_serial:'b.'brecord_serial->('a,'b)Type.eqoption;}letmake_id(typea)():aproduct_id=letmoduleM=structtype_record_serial+=Serial:arecord_serialendinletis_serial:typeb.brecord_serial->(a,b)Type.eqoption=function|M.Serial->SomeEqual|_->Nonein{serial=M.Serial;is_serial}letunify_id{is_serial;_}{serial;_}=is_serialserialtype_t=|Field:'aField_type.t->'at|Option:'at->'aoptiont|Product:'aproduct_id*'i*('a,'i)product->'at|Annot:[`Redacted]*'at->'atand(_,_)product=|Proj_end:('a,'a)product|Proj:'bt*('a->'b)*('a,'i)product->('a,'b->'i)productendopenPrivatetype'at='aPrivate.ttype('a,'b)product=('a,'b)Private.producttypeany=Any:'at->anyletrecunify:typeab.at->bt->(a,b)Type.eqoption=funt1t2->(matcht1,t2with|Fieldft1,Fieldft2->Field_type.unifyft1ft2|Field_,_|_,Field_->None|Optiont1,Optiont2->(matchunifyt1t2withNone->None|SomeEqual->SomeEqual)|Option_,_|_,Option_->None|Product(id1,_,_),Product(id2,_,_)->unify_idid1id2|Product_,_|_,Product_->None|Annot(`Redacted,t1),Annot(`Redacted,t2)->unifyt1t2)letequal_optionfxy=(matchx,ywith|None,None->true|Somex,Somey->fxy|None,Some_|Some_,None->false)letrecequal_value:typea.at->a->a->bool=(function|Fieldft->Field_type.equal_valueft|Optiont->equal_option(equal_valuet)|Product(_,_,prod)->equal_value_prodprod|Annot(_,t)->equal_valuet)andequal_value_prod:typeai.(a,i)product->a->a->bool=(function|Proj_end->fun__->true|Proj(t,p,prod)->leteq_first=equal_valuetinleteq_rest=equal_value_prodprodinfunxy->eq_first(px)(py)&&eq_restxy)letreclength:typea.at->int=function|Field_->1|Optiont->lengtht|Product(_,_,prod)->letrecloop:typeai.(a,i)product->_->_=function|Proj_end->Fun.id|Proj(t,_,prod)->funn->loopprod(n+lengtht)inloopprod0|Annot(_,t)->lengthtletrecpp_at:typea.int->Format.formatter->at->unit=funprecppf->function|Fieldft->Format.pp_print_stringppf(Field_type.to_stringft)|Optiont->pp_at1ppft;Format.pp_print_stringppf" option"|Product(_,_,Proj_end)->Format.pp_print_stringppf"unit"|Product(_,_,Proj(t0,_,prod))->ifprec>0thenFormat.pp_print_charppf'(';letrecloop:typeai.(a,i)product->_=function|Proj_end->()|Proj(t,_,prod)->Format.pp_print_stringppf" × ";pp_at1ppft;loopprodinpp_at1ppft0;loopprod;ifprec>0thenFormat.pp_print_charppf')'|Annot(`Redacted,t)->pp_at1ppft;Format.pp_print_stringppf" redacted"letppppf=pp_at0ppfletpp_anyppf(Anyt)=pp_at0ppftletrecpp_value:typea._->at*a->unit=funppf->function|Fieldft,fv->Field_type.pp_valueppf(ft,fv)|Option_,None->Format.pp_print_stringppf"None"|Optiont,Somex->Format.pp_print_stringppf"Some ";pp_valueppf(t,x)|Product(_,_,prod),x->letrecloop:typei.int->(a,i)product->_=funi->function|Proj_end->()|Proj(t,p,prod)->ifi>0thenFormat.pp_print_stringppf", ";pp_valueppf(t,px);loop(i+1)prodinloop0prod|Annot(`Redacted,_),_->Format.pp_print_stringppf"#redacted#"letshowt=letbuf=Buffer.create64inletppf=Format.formatter_of_bufferbufinppppft;Format.pp_print_flushppf();Buffer.contentsbufletfieldft=FieldftmoduletypeSTD=sigvalbool:booltvalint:inttvalint16:inttvalint32:int32tvalint64:int64tvalfloat:floattvalstring:stringtvaloctets:stringtvalpdate:Ptime.ttvalptime:Ptime.ttvalptime_span:Ptime.spantvaloption:'at->'aoptiontvalredacted:'at->'atvalunit:unittvalt2:'a1t->'a2t->('a1*'a2)tvalt3:'a1t->'a2t->'a3t->('a1*'a2*'a3)tvalt4:'a1t->'a2t->'a3t->'a4t->('a1*'a2*'a3*'a4)tvalt5:'a1t->'a2t->'a3t->'a4t->'a5t->('a1*'a2*'a3*'a4*'a5)tvalt6:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->('a1*'a2*'a3*'a4*'a5*'a6)tvalt7:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->('a1*'a2*'a3*'a4*'a5*'a6*'a7)tvalt8:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->'a8t->('a1*'a2*'a3*'a4*'a5*'a6*'a7*'a8)tvalt9:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->'a8t->'a9t->('a1*'a2*'a3*'a4*'a5*'a6*'a7*'a8*'a9)tvalt10:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->'a8t->'a9t->'a10t->('a1*'a2*'a3*'a4*'a5*'a6*'a7*'a8*'a9*'a10)tvalt11:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->'a8t->'a9t->'a10t->'a11t->('a1*'a2*'a3*'a4*'a5*'a6*'a7*'a8*'a9*'a10*'a11)tvalt12:'a1t->'a2t->'a3t->'a4t->'a5t->'a6t->'a7t->'a8t->'a9t->'a10t->'a11t->'a12t->('a1*'a2*'a3*'a4*'a5*'a6*'a7*'a8*'a9*'a10*'a11*'a12)tendletoptiont=Optiontletproductintroprod=Product(make_id(),intro,prod)letprojtpprod=Proj(t,p,prod)letproj_end=Proj_endletenum~encode~decodename=letdecode'y=(matchdecodeywith|Okx->x|Errormsg->raise(Rejectmsg))inproductdecode'@@proj(Field(Enumname))encode@@proj_endletunit=product()proj_endlett2t1t2=letintrox1x2=(x1,x2)inproductintro@@projt1fst@@projt2snd@@proj_endlett3t1t2t3=letintrox1x2x3=(x1,x2,x3)inproductintro@@projt1(fun(x,_,_)->x)@@projt2(fun(_,x,_)->x)@@projt3(fun(_,_,x)->x)@@proj_endlett4t1t2t3t4=letintrox1x2x3x4=(x1,x2,x3,x4)inproductintro@@projt1(fun(x,_,_,_)->x)@@projt2(fun(_,x,_,_)->x)@@projt3(fun(_,_,x,_)->x)@@projt4(fun(_,_,_,x)->x)@@proj_endlett5t1t2t3t4t5=letintrox1x2x3x4x5=(x1,x2,x3,x4,x5)inproductintro@@projt1(fun(x,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_)->x)@@projt3(fun(_,_,x,_,_)->x)@@projt4(fun(_,_,_,x,_)->x)@@projt5(fun(_,_,_,_,x)->x)@@proj_endlett6t1t2t3t4t5t6=letintrox1x2x3x4x5x6=(x1,x2,x3,x4,x5,x6)inproductintro@@projt1(fun(x,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_)->x)@@projt5(fun(_,_,_,_,x,_)->x)@@projt6(fun(_,_,_,_,_,x)->x)@@proj_endlett7t1t2t3t4t5t6t7=letintrox1x2x3x4x5x6x7=(x1,x2,x3,x4,x5,x6,x7)inproductintro@@projt1(fun(x,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_)->x)@@projt7(fun(_,_,_,_,_,_,x)->x)@@proj_endlett8t1t2t3t4t5t6t7t8=letintrox1x2x3x4x5x6x7x8=(x1,x2,x3,x4,x5,x6,x7,x8)inproductintro@@projt1(fun(x,_,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_,_)->x)@@projt7(fun(_,_,_,_,_,_,x,_)->x)@@projt8(fun(_,_,_,_,_,_,_,x)->x)@@proj_endlett9t1t2t3t4t5t6t7t8t9=letintrox1x2x3x4x5x6x7x8x9=(x1,x2,x3,x4,x5,x6,x7,x8,x9)inproductintro@@projt1(fun(x,_,_,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_,_,_)->x)@@projt7(fun(_,_,_,_,_,_,x,_,_)->x)@@projt8(fun(_,_,_,_,_,_,_,x,_)->x)@@projt9(fun(_,_,_,_,_,_,_,_,x)->x)@@proj_endlett10t1t2t3t4t5t6t7t8t9t10=letintrox1x2x3x4x5x6x7x8x9x10=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)inproductintro@@projt1(fun(x,_,_,_,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_,_,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_,_,_,_)->x)@@projt7(fun(_,_,_,_,_,_,x,_,_,_)->x)@@projt8(fun(_,_,_,_,_,_,_,x,_,_)->x)@@projt9(fun(_,_,_,_,_,_,_,_,x,_)->x)@@projt10(fun(_,_,_,_,_,_,_,_,_,x)->x)@@proj_endlett11t1t2t3t4t5t6t7t8t9t10t11=letintrox1x2x3x4x5x6x7x8x9x10x11=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)inproductintro@@projt1(fun(x,_,_,_,_,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_,_,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_,_,_,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_,_,_,_,_)->x)@@projt7(fun(_,_,_,_,_,_,x,_,_,_,_)->x)@@projt8(fun(_,_,_,_,_,_,_,x,_,_,_)->x)@@projt9(fun(_,_,_,_,_,_,_,_,x,_,_)->x)@@projt10(fun(_,_,_,_,_,_,_,_,_,x,_)->x)@@projt11(fun(_,_,_,_,_,_,_,_,_,_,x)->x)@@proj_endlett12t1t2t3t4t5t6t7t8t9t10t11t12=letintrox1x2x3x4x5x6x7x8x9x10x11x12=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)inproductintro@@projt1(fun(x,_,_,_,_,_,_,_,_,_,_,_)->x)@@projt2(fun(_,x,_,_,_,_,_,_,_,_,_,_)->x)@@projt3(fun(_,_,x,_,_,_,_,_,_,_,_,_)->x)@@projt4(fun(_,_,_,x,_,_,_,_,_,_,_,_)->x)@@projt5(fun(_,_,_,_,x,_,_,_,_,_,_,_)->x)@@projt6(fun(_,_,_,_,_,x,_,_,_,_,_,_)->x)@@projt7(fun(_,_,_,_,_,_,x,_,_,_,_,_)->x)@@projt8(fun(_,_,_,_,_,_,_,x,_,_,_,_)->x)@@projt9(fun(_,_,_,_,_,_,_,_,x,_,_,_)->x)@@projt10(fun(_,_,_,_,_,_,_,_,_,x,_,_)->x)@@projt11(fun(_,_,_,_,_,_,_,_,_,_,x,_)->x)@@projt12(fun(_,_,_,_,_,_,_,_,_,_,_,x)->x)@@proj_endletcustom~encode~decoderep=letencode'x=(matchencodexwith|Oky->y|Errormsg->raise(Rejectmsg))inletdecode'y=(matchdecodeywith|Okx->x|Errormsg->raise(Rejectmsg))inproductdecode'@@projrepencode'@@proj_endletredactedt=Annot(`Redacted,t)letbool=FieldBoolletint=FieldIntletint16=FieldInt16letint32=FieldInt32letint64=FieldInt64letfloat=FieldFloatletstring=FieldStringletoctets=FieldOctetsletpdate=FieldPdateletptime=FieldPtimeletptime_span=FieldPtime_span