---
 generator/utils.ml  | 13 +++++++++++--
 generator/utils.mli |  8 +++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/generator/utils.ml b/generator/utils.ml
index 61cce87..201fb54 100644
--- a/generator/utils.ml
+++ b/generator/utils.ml
@@ -419,7 +419,10 @@ let files_equal n1 n2 =
   | 1 -> false
   | i -> failwithf "%s: failed with error code %d" cmd i
 
-let output_to filename k =
+type formatter =
+  | Rustfmt
+
+let output_to ?(formatter = None) filename k =
   lineno := 1; col := 0;
   let filename_new = filename ^ ".new" in
   let c = open_out filename_new in
@@ -427,7 +430,13 @@ let output_to filename k =
   k ();
   close_out c;
   chan := NoOutput;
-
+  (match formatter with
+  | Some Rustfmt ->
+    (match system (sprintf "rustfmt %s" filename_new) with
+      | WEXITED 0 -> ()
+      | WEXITED i -> failwith (sprintf "Rustfmt failed with exit code %d" i)
+      | _ -> failwith "Rustfmt was killed or stopped by a signal.");
+  | None -> ());
   (* Is the new file different from the current file? *)
   if Sys.file_exists filename && files_equal filename filename_new then
     unlink filename_new                 (* same, so skip it *)
diff --git a/generator/utils.mli b/generator/utils.mli
index c4d47a3..d97d43a 100644
--- a/generator/utils.mli
+++ b/generator/utils.mli
@@ -52,7 +52,13 @@ val files_equal : string -> string -> bool
 val generate_header :
   ?extra_sources:string list -> ?copyright:string -> comment_style -> unit
 
-val output_to : string -> (unit -> 'a) -> unit
+(** Type of code formatter. *)
+type formatter =
+  | Rustfmt
+
+(** Redirect stdout to a file. Possibly formatting the code. *)
+val output_to : ?formatter:formatter option -> string -> (unit -> 'a) -> unit
+
 val pr : ('a, unit, string, unit) format4 -> 'a
 val pr_wrap : ?maxcol:int -> char -> (unit -> 'a) -> unit
 val pr_wrap_cstr : ?maxcol:int -> (unit -> 'a) -> unit
-- 
2.41.0

_______________________________________________
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs

Reply via email to