Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_protocol_driver.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262openProtocol_convopenRuntimeopenStdLabelsmoduletypeParameters=sigvalfield_name:string->stringvalvariant_name:string->stringvalconstructors_without_arguments_as_string:boolvalomit_default_values:boolvaleager:boolvalstrict:boolendmoduleDefault_parameters:Parameters=structletfield_namename=nameletvariant_namename=nameletconstructors_without_arguments_as_string=trueletomit_default_values=trueleteager=trueletstrict=falseendmoduletypeDriver=sigtypetvalto_string_hum:t->stringvalto_list:t->tlistvalof_list:tlist->tvalis_list:t->boolvalto_alist:t->(string*t)listvalof_alist:(string*t)list->tvalis_alist:t->boolvalto_char:t->charvalof_char:char->tvalto_int:t->intvalof_int:int->tvalto_int32:t->int32valof_int32:int32->tvalto_int64:t->int64valof_int64:int64->tvalto_nativeint:t->nativeintvalof_nativeint:nativeint->tvalto_float:t->floatvalof_float:float->tvalto_string:t->stringvalof_string:string->tvalis_string:t->boolvalto_bool:t->boolvalof_bool:bool->tvalto_bytes:t->bytesvalof_bytes:bytes->tvalnull:tvalis_null:t->boolendletmanglestr=letchars=letchars=ref[]inString.iter~f:(funch->chars:=ch::!chars)str;List.rev!charsinletrecinner=function|'_'::'_'::cs->inner('_'::cs)|'_'::c::cs->Char.uppercase_asciic::innercs|'_'::[]->[]|c::cs->c::innercs|[]->[]inletres_arr=innerchars|>Array.of_listinString.init(Array.lengthres_arr)~f:(funi->res_arr.(i))moduleMake(Driver:Driver)(P:Parameters)=structtypet=Driver.ttypeerror=string*toptionexceptionProtocol_erroroferrorletmake_error?valuemsg=(msg,value)leterror_to_string_hum:error->string=function|(s,Somet)->Printf.sprintf"%s. Got: %s"s(Driver.to_string_humt)|(s,None)->s(* Register exception printer *)let()=Printexc.register_printer(function|Protocol_errorerr->Some(error_to_string_humerr)|_->None)letto_string_hum=Driver.to_string_humletraise_errorftfmt=Printf.kprintf(funs->raise(Protocol_error(s,t)))fmtlettry_with:(t->'a)->t->('a,error)Runtime.result=funft->matchftwith|v->Okv|exception(Protocol_errore)->Erroreletwraptfx=matchfxwith|v->v|exceptionHelper.Protocol_errors->raise(Protocol_error(s,Somet))|exceptione->raise(Protocol_error(Printexc.to_stringe,Somet))letto_record:(t,'a,'b)Record_in.t->'a->t->'b=funspecconstr->letspec=Helper.map_record_inP.field_namespecinletf=Helper.to_record~strict:P.strictspecconstrinfunt->wraptf(wraptDriver.to_alistt)letof_record:typea.(t,a,t)Record_out.t->a=funspec->letspec=Helper.map_record_outP.field_namespecinHelper.of_record~omit_default:P.omit_default_valuesDriver.of_alistspecletto_tuple:(t,'a,'b)Tuple_in.t->'a->t->'b=funspecconstr->letf=Helper.to_tuplespecconstrinfunt->wraptf(wraptDriver.to_listt)letof_tuple:(t,'a,t)Tuple_out.t->'a=funspec->Helper.of_tupleDriver.of_listspecletto_variant:(t,'a)Variant_in.tlist->t->'a=funspec->letf=Helper.to_variant(Helper.map_constructor_namesP.variant_namespec)inmatchP.constructors_without_arguments_as_stringwith|true->beginfunction|twhenDriver.is_stringt->wrapt(f(wraptDriver.to_stringt))[]|twhenDriver.is_listt->beginmatchDriver.to_listtwith|name::argswhenDriver.is_stringname->wraptf((Driver.to_stringname))args|_::_->raise_errorf(Somet)"First element in the list must be the constructor name when name when deserialising variant"|[]->raise_errorf(Somet)"Empty list found when deserialising variant"end|t->raise_errorf(Somet)"Expected list or string when deserialising variant"end|false->beginfunction|twhenDriver.is_listt->beginmatchDriver.to_listtwith|name::argswhenDriver.is_stringname->wrapt(f(Driver.to_stringname))args|_::_->raise_errorf(Somet)"First element in the list must be the constructor name when name when deserialising variant"|[]->raise_errorf(Somet)"Empty list found when deserialising variant"end|t->raise_errorf(Somet)"Expected list when deserialising variant"endletof_variant:string->(t,'a,t)Tuple_out.t->'a=letof_variantname=letname=P.variant_namename|>Driver.of_stringinfunction|[]whenP.constructors_without_arguments_as_string->name|ts->Driver.of_list(name::ts)infunnamespec->Helper.of_variantof_variantnamespecletget_option=function|twhenDriver.is_alistt->beginmatchDriver.to_alisttwith|[("__option",t)]->Somet|_->Noneend|_->None(* If the type is an empty list, thats also null. *)letto_option:(t->'a)->t->'aoption=funto_value_fun->function|twhenDriver.is_nullt->None|t->lett=match(get_optiont)withSomet->t|None->tinSome(to_value_funt)letof_option:('a->t)->'aoption->t=funof_value_fun->function|None->Driver.null|Somev->letmk_optiont=Driver.of_alist[("__option",t)]inmatchof_value_funvwith|twhenDriver.is_nullt->mk_optiont|twhen(get_optiont)<>None->mk_optiont|t->tletto_ref:(t->'a)->t->'aref=funto_value_funt->letv=to_value_funtinrefvletof_ref:('a->t)->'aref->t=funof_value_funv->of_value_fun!vletto_result:(t->'a)->(t->'b)->t->('a,'b)result=funto_okto_err->letok=Runtime.Tuple_in.(Cons(to_ok,Nil))inleterr=Runtime.Tuple_in.(Cons(to_err,Nil))into_variantRuntime.Variant_in.[Variant("Ok",ok,funv->Okv);Variant("Error",err,funv->Errorv)]letof_result:('a->t)->('b->t)->('a,'b)result->t=funof_okof_err->letof_ok=of_variant"Ok"Runtime.Tuple_out.(Cons(of_ok,Nil))inletof_err=of_variant"Error"Runtime.Tuple_out.(Cons(of_err,Nil))infunction|Okok->of_okok|Errorerr->of_errerrletto_list:(t->'a)->t->'alist=funto_value_funt->Helper.list_map~f:to_value_fun(wraptDriver.to_listt)letof_list:('a->t)->'alist->t=funof_value_funv->Helper.list_map~f:of_value_funv|>Driver.of_listletto_array:(t->'a)->t->'aarray=funto_value_funt->to_listto_value_funt|>Array.of_listletof_array:('a->t)->'aarray->t=funof_value_funv->Array.to_listv|>of_listof_value_funletto_lazy_t:(t->'a)->t->'alazy_t=funto_value_fun->matchP.eagerwith|true->funt->Lazy.from_val(to_value_funt)|false->funt->Lazy.from_fun(fun()->to_value_funt)letof_lazy_t:('a->t)->'alazy_t->t=funof_value_funv->Lazy.forcev|>of_value_funletto_chart=tryDriver.to_chartwith_->raise_errorf(Somet)"char expected"letof_char=Driver.of_charletto_intt=tryDriver.to_inttwith_->raise_errorf(Somet)"int expected"letof_int=Driver.of_intletto_int32t=tryDriver.to_int32twith_->raise_errorf(Somet)"int32 expected"letof_int32=Driver.of_int32letto_int64t=tryDriver.to_int64twith_->raise_errorf(Somet)"int64 expected"letof_int64=Driver.of_int64letto_nativeintt=tryDriver.to_nativeinttwith_->raise_errorf(Somet)"nativeint expected"letof_nativeint=Driver.of_nativeintletto_stringt=tryDriver.to_stringtwith_->raise_errorf(Somet)"string expected"letof_string=Driver.of_stringletto_floatt=tryDriver.to_floattwith_->raise_errorf(Somet)"float expected"letof_float=Driver.of_floatletto_boolt=tryDriver.to_booltwith_->raise_errorf(Somet)"bool expected"letof_bool=Driver.of_boolletto_bytest=tryDriver.to_bytestwith_->raise_errorf(Somet)"bytes expected"letof_bytes=Driver.of_bytesletto_unitt=to_option(fun_->())t|>functionSome_->raise_errorf(Somet)"Unit expected"|None->()letof_unit()=of_option(fun_->failwith"Should call with None")Noneend