@@ -703,3 +703,137 @@ module Job = struct
703703 ;;
704704 end
705705end
706+
707+ module Compound_user_error = struct
708+ type t =
709+ { main : User_message .t
710+ ; related : User_message .t list
711+ }
712+
713+ let create ~main ~related =
714+ let () =
715+ List. iter related ~f: (fun (related : User_message.t ) ->
716+ match related.loc with
717+ | Some _ -> ()
718+ | None ->
719+ Code_error. raise
720+ " related messages must have locations"
721+ [ " related" , String (Stdune.User_message. to_string related) ])
722+ in
723+ { main; related }
724+ ;;
725+
726+ let sexp =
727+ let open Conv in
728+ let from { main; related } = main, related in
729+ let to_ (main , related ) = create ~main ~related in
730+ let main = field " main" (required User_message. sexp_without_annots) in
731+ let related = field " related" (required (list User_message. sexp_without_annots)) in
732+ iso (record (both main related)) to_ from
733+ ;;
734+
735+ let to_dyn { main; related } =
736+ let open Dyn in
737+ record
738+ [ " main" , string (Stdune.User_message. to_string main)
739+ ; " related" , (list string ) (List. map related ~f: Stdune.User_message. to_string)
740+ ]
741+ ;;
742+
743+ let annot =
744+ Stdune.User_message.Annots.Key. create ~name: " compound-user-error" (Dyn. list to_dyn)
745+ ;;
746+
747+ let make ~main ~related = create ~main ~related
748+
749+ let make_loc ~dir { Ocamlc_loc. path; chars; lines } : Stdune.Loc.t =
750+ let pos_fname =
751+ let dir = Stdune.Path. drop_optional_build_context_maybe_sandboxed dir in
752+ Stdune.Path. to_absolute_filename (Stdune.Path. relative dir path)
753+ in
754+ let pos_lnum_start, pos_lnum_stop =
755+ match lines with
756+ | Single i -> i, i
757+ | Range (i , j ) -> i, j
758+ in
759+ let pos_cnum_start, pos_cnum_stop =
760+ match chars with
761+ | None -> 0 , 0
762+ | Some (x , y ) -> x, y
763+ in
764+ let pos = { Lexing. pos_fname; pos_lnum = 0 ; pos_bol = 0 ; pos_cnum = 0 } in
765+ let start = { pos with pos_lnum = pos_lnum_start; pos_cnum = pos_cnum_start } in
766+ let stop = { pos with pos_lnum = pos_lnum_stop; pos_cnum = pos_cnum_stop } in
767+ Stdune.Loc. create ~start ~stop
768+ ;;
769+
770+ let parse_output ~dir s =
771+ Ocamlc_loc. parse s
772+ |> List. map ~f: (fun (report : Ocamlc_loc.report ) ->
773+ let make_message (loc , message ) =
774+ let loc = make_loc ~dir loc in
775+ let message = Pp. verbatim message in
776+ Stdune.User_message. make ~loc [ message ]
777+ in
778+ let main = make_message (report.loc, report.message) in
779+ let related = List. map report.related ~f: make_message in
780+ make ~main ~related )
781+ ;;
782+ end
783+
784+ module Build_outcome_with_diagnostics = struct
785+ type t =
786+ | Success
787+ | Failure of Compound_user_error .t list
788+
789+ let sexp_v1 =
790+ let open Conv in
791+ let success = constr " Success" unit (fun () -> Success ) in
792+ let failure = constr " Failure" unit (fun () -> Failure [] ) in
793+ let variants = [ econstr success; econstr failure ] in
794+ sum variants (function
795+ | Success -> case () success
796+ | Failure _ -> case () failure)
797+ ;;
798+
799+ let sexp_v2 =
800+ let open Conv in
801+ let success = constr " Success" unit (fun () -> Success ) in
802+ let failure =
803+ constr " Failure" (list Compound_user_error. sexp) (fun errors -> Failure errors)
804+ in
805+ let variants = [ econstr success; econstr failure ] in
806+ sum variants (function
807+ | Success -> case () success
808+ | Failure errors -> case errors failure)
809+ ;;
810+
811+ let sexp = sexp_v2
812+ end
813+
814+ module Files_to_promote = struct
815+ type t =
816+ | All
817+ | These of Stdune.Path.Source .t list * (Stdune.Path.Source .t -> unit )
818+
819+ let on_missing fn =
820+ Stdune.User_warning. emit
821+ [ Pp. paragraphf
822+ " Nothing to promote for %s."
823+ (Stdune.Path.Source. to_string_maybe_quoted fn)
824+ ]
825+ ;;
826+
827+ let sexp =
828+ let open Conv in
829+ let to_ = function
830+ | [] -> All
831+ | paths -> These (List. map ~f: Stdune.Path.Source. of_string paths, on_missing)
832+ in
833+ let from = function
834+ | All -> []
835+ | These (paths , _ ) -> List. map ~f: Stdune.Path.Source. to_string paths
836+ in
837+ iso (list Path. sexp) to_ from
838+ ;;
839+ end
0 commit comments