123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159# 1 "lib-rpc/protocol.ml"(**************************************************************************)(* *)(* OCamlFormat *)(* *)(* Copyright (c) Facebook, Inc. and its affiliates. *)(* *)(* This source code is licensed under the MIT license found in *)(* the LICENSE file in the root directory of this source tree. *)(* *)(**************************************************************************)typeformat_args={path:stringoption;config:(string*string)listoption}letempty_args={path=None;config=None}moduleVersion=structtypet=V1|V2letto_string=functionV1->"v1"|V2->"v2"letof_string=function|"v1"|"V1"->SomeV1|"v2"|"V2"->SomeV2|_->NoneendmoduleMake(IO:IO.S)=structmoduletypeCommand_S=sigtypetvalread_input:IO.ic->tIO.tvaloutput:IO.oc->t->unitIO.tendmoduleInit=structtypet=[`Halt|`Unknown|`Versionofstring]letread_inputic=letopenIOinreadic>>=function|None->return`Halt|Some(Atom"Halt")->return`Halt|Some(List[Atom"Version";Atomv])->return(`Versionv)|Some_->return`Unknownletto_sexp=letopenCsexpinfunction|`Versionv->List[Atom"Version";Atomv]|_->assertfalseletoutputoct=IO.writeoc[to_sexpt]endmoduleV1=structtypet=[`Halt|`Unknown|`Errorofstring|`Configof(string*string)list|`Formatofstring]letread_inputic=letopenCsexpinletopenIOinreadic>>=function|None->return`Halt|Some(List[Atom"Format";Atomx])->return(`Formatx)|Some(List[Atom"Config";Listl])->letc=List.fold_left(funacc->function|List[Atomname;Atomvalue]->(name,value)::acc|_->acc)[]l|>List.revinreturn(`Configc)|Some(List[Atom"Error";Atomx])->return(`Errorx)|Some(Atom"Halt")->return`Halt|Some_->return`Unknownletto_sexp=letopenCsexpinfunction|`Formatx->List[Atom"Format";Atomx]|`Configc->letl=List.map(fun(name,value)->List[Atomname;Atomvalue])cinList[Atom"Config";Listl]|`Errorx->List[Atom"Error";Atomx]|`Halt->Atom"Halt"|_->assertfalseletoutputoct=IO.writeoc[to_sexpt]endmoduleV2=structtypet=[`Halt|`Unknown|`Errorofstring|`Formatofstring*format_args]letread_inputic=letopenCsexpinletopenIOinletcsexp_to_configcsexpl=List.filter_map(function|List[Atomname;Atomvalue]->Some(name,value)|_->None)csexplinreadic>>=function|None->return`Halt|Some(List(Atom"Format"::Atomx::l))->letextractargscsexp=matchcsexpwith|List[Atom"Config";Listl]->{argswithconfig=Some(csexp_to_configl)}|List[Atom"Path";Atompath]->{argswithpath=Somepath}|_->argsinletargs=List.fold_leftextractempty_argslinreturn(`Format(x,args))|Some(List[Atom"Error";Atomx])->return(`Errorx)|Some(Atom"Halt")->return`Halt|Some_->return`Unknownletto_sexp=letopenCsexpinfunction|`Format(x,{path;config})->letmap_confignameconfig=letc=List.map(fun(name,value)->List[Atomname;Atomvalue])configinList[Atomname;Listc]inletofp=Option.map(funpath->List[Atom"Path";Atompath])pathandoconfig=Option.map(map_config"Config")configinList(List.filter_map(funi->i)[Some(Atom"Format");Some(Atomx);ofp;oconfig])|`Errorx->List[Atom"Error";Atomx]|`Halt->Atom"Halt"|_->assertfalseletoutputoct=IO.writeoc[to_sexpt]endend