123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116(* Copyright 2024 Yawar Amin
This file is part of dream-html.
dream-html is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
dream-html 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along with
dream-html. If not, see <https://www.gnu.org/licenses/>. *)type'aty=string->('a,string)resulttype'at=(string,string)Hashtbl.t->('a,(string*string)list)resultleterror_expected_bool="error.expected.bool"leterror_expected_char="error.expected.char"leterror_expected_single="error.expected.single"leterror_expected_int="error.expected.int"leterror_expected_int32="error.expected.int32"leterror_expected_int64="error.expected.int64"leterror_expected_number="error.expected.number"leterror_required="error.required"leterrornamemsg=Error[name,msg]letensuremessageconditionfieldtynamevalues=matchfieldtynamevalueswith|Okvasok->ifconditionvthenokelseerrornamemessage|Error_aserror->errorletrecalltyresult=function|[]->result|x::xs->(matchtyx,resultwith|Okt,Okr->allty(Ok(t::r))xs|(Error_ase),_->e|_,(Error_ase)->e)letallty=allty(Ok[])letlisttynamevalues=matchallty(Hashtbl.find_allvaluesname)with|Ok_asok->ok|Errormsg->errornamemsgletoptionaltynamevalues=matchHashtbl.find_optvaluesnamewith|None->OkNone|Somes->(matchtyswith|Okv->Ok(Somev)|Errormsg->errornamemsg)letrequiredtynamevalues=matchHashtbl.find_optvaluesnamewith|None->errornameerror_required|Somes->(matchtyswith|Okv->Okv|Errormsg->errornamemsg)letstrings=Oksletints=tryOk(int_of_strings)withFailure_->Errorerror_expected_intletint32s=tryOk(Int32.of_strings)withFailure_->Errorerror_expected_int32letint64s=tryOk(Int64.of_strings)withFailure_->Errorerror_expected_int64letchars=ifString.lengths=1thenOks.[0]elseErrorerror_expected_charletfloats=tryOk(float_of_strings)withFailure_->Errorerror_expected_numberletbool=function|"true"->Oktrue|"false"->Okfalse|_->Errorerror_expected_boollet(let+)formfvalues=matchformvalueswith|Okv->Ok(fv)|Errore->Errorelet(and+)form1form2values=matchform1values,form2valueswith|Okv1,Okv2->Ok(v1,v2)|Ok_,Errore2->Errore2|Errore1,Ok_->Errore1|Errore1,Errore2->Error(e2@e1)let(or)form1form2values=matchform1valueswith|Ok_asok->ok|Error_->form2valuesletvalidateformvalues=lethtbl=Hashtbl.create10inList.iter(fun(name,value)->Hashtbl.addhtblnamevalue)values;formhtblletpp_error=letopenFmtinbrackets(list~sep:semi(pair~sep:commastringstring))