From 0cf38ffc916e0e5970e34df8ffdc58b180287a35 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 25 Feb 2025 18:58:53 +0100 Subject: [PATCH] Make output channel handling more readable --- src/odoc/html_fragment.ml | 6 ++---- src/odoc/indexing.ml | 7 ++----- src/odoc/odoc_file.ml | 6 +++--- src/odoc/rendering.ml | 7 +++---- src/odoc/sidebar.ml | 9 +++------ src/odoc/support_files.ml | 6 +++--- src/utils/odoc_utils.ml | 6 ++++++ 7 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/odoc/html_fragment.ml b/src/odoc/html_fragment.ml index 741f2e822e..40f6007eb5 100644 --- a/src/odoc/html_fragment.ml +++ b/src/odoc/html_fragment.ml @@ -1,3 +1,4 @@ +open Odoc_utils open Or_error let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input = @@ -40,11 +41,8 @@ let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input = ~open_details:false ~as_json:false ~remap:[] () in let html = Odoc_html.Generator.doc ~config ~xref_base_uri page in - let oc = open_out (Fs.File.to_string output) in - let fmt = Format.formatter_of_out_channel oc in - + Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun fmt -> Format.fprintf fmt "%a@." (Format.pp_print_list (Tyxml.Html.pp_elt ())) html; - close_out oc; Ok () in match Fs.File.read input with diff --git a/src/odoc/indexing.ml b/src/odoc/indexing.ml index dbe27af538..fb2874c2cc 100644 --- a/src/odoc/indexing.ml +++ b/src/odoc/indexing.ml @@ -24,11 +24,8 @@ let parse_input_files input = >>= fun files -> Ok (List.concat files) let compile_to_json ~output ~occurrences ~wrap ~simplified hierarchies = - let output_channel = - Fs.Directory.mkdir_p (Fs.File.dirname output); - open_out_bin (Fs.File.to_string output) - in - let output = Format.formatter_of_out_channel output_channel in + Fs.Directory.mkdir_p (Fs.File.dirname output); + Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun output -> if wrap then Format.fprintf output "let documents = "; let all = List.fold_left diff --git a/src/odoc/odoc_file.ml b/src/odoc/odoc_file.ml index b931027d7f..d07f1cf824 100644 --- a/src/odoc/odoc_file.ml +++ b/src/odoc/odoc_file.ml @@ -34,9 +34,9 @@ let magic = "odoc-%%VERSION%%" (** Exceptions while saving are allowed to leak. *) let save_ file f = Fs.Directory.mkdir_p (Fs.File.dirname file); - let oc = open_out_bin (Fs.File.to_string file) in - output_string oc magic; - Fun.protect ~finally:(fun () -> close_out oc) (fun () -> f oc) + Io_utils.with_open_out_bin (Fs.File.to_string file) (fun oc -> + output_string oc magic; + f oc) let save_unit file (root : Root.t) (t : t) = save_ file (fun oc -> diff --git a/src/odoc/rendering.ml b/src/odoc/rendering.ml index eb29f199b2..2d92c892e3 100644 --- a/src/odoc/rendering.ml +++ b/src/odoc/rendering.ml @@ -1,3 +1,4 @@ +open Odoc_utils open Odoc_document open Or_error open Odoc_model @@ -52,10 +53,8 @@ let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc let pages = renderer.Renderer.render extra sidebar doc in Renderer.traverse pages ~f:(fun filename content -> let filename = prepare ~extra_suffix ~output_dir:root_dir filename in - let oc = open_out (Fs.File.to_string filename) in - let fmt = Format.formatter_of_out_channel oc in - Format.fprintf fmt "%t@?" content; - close_out oc) + Io_utils.with_formatter_out (Fs.File.to_string filename) @@ fun fmt -> + Format.fprintf fmt "%t@?" content) let render_odoc ~resolver ~warnings_options ~syntax ~renderer ~output extra file = diff --git a/src/odoc/sidebar.ml b/src/odoc/sidebar.ml index f69eda5561..eaed81956c 100644 --- a/src/odoc/sidebar.ml +++ b/src/odoc/sidebar.ml @@ -4,12 +4,9 @@ open Odoc_utils let compile_to_json ~output sidebar = let json = Odoc_html.Sidebar.to_json sidebar in let text = Json.to_string json in - let output_channel = - Fs.Directory.mkdir_p (Fs.File.dirname output); - open_out_bin (Fs.File.to_string output) - in - Fun.protect ~finally:(fun () -> close_out output_channel) @@ fun () -> - Printf.fprintf output_channel "%s" text + Fs.Directory.mkdir_p (Fs.File.dirname output); + Io_utils.with_open_out_bin (Fs.File.to_string output) @@ fun oc -> + Printf.fprintf oc "%s" text let generate ~marshall ~output ~warnings_options:_ ~index = Odoc_file.load_index index >>= fun index -> diff --git a/src/odoc/support_files.ml b/src/odoc/support_files.ml index 606e358bb7..366aeda87b 100644 --- a/src/odoc/support_files.ml +++ b/src/odoc/support_files.ml @@ -1,3 +1,5 @@ +open Odoc_utils + let should_include ~without_theme file = if without_theme then match file with @@ -25,9 +27,7 @@ let write = let dir = Fs.File.dirname name in Fs.Directory.mkdir_p dir; let name = Fs.File.to_string name in - let channel = open_out name in - output_string channel content; - close_out channel) + Io_utils.with_open_out name (fun oc -> output_string oc content)) let print_filenames = iter_files (fun name _content -> print_endline (Fs.File.to_string name)) diff --git a/src/utils/odoc_utils.ml b/src/utils/odoc_utils.ml index e102cf244c..3f79aa3238 100644 --- a/src/utils/odoc_utils.ml +++ b/src/utils/odoc_utils.ml @@ -101,9 +101,15 @@ module Io_utils = struct let read_lines fname = List.rev (fold_lines fname (fun line acc -> line :: acc) []) + let with_open_out fname f = + _with_resource (open_out fname) ~close:close_out_noerr f + let with_open_out_bin fname f = _with_resource (open_out_bin fname) ~close:close_out_noerr f + let with_formatter_out fname f = + with_open_out fname (fun oc -> f (Format.formatter_of_out_channel oc)) + let marshal fname v = _with_resource (open_out_bin fname) ~close:close_out_noerr (fun oc -> Marshal.to_channel oc v [])