1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)
open Syntax
let get_printer filename =
let ext = Fpath.get_ext filename in
match ext with
| ".wat" ->
let+ v = Parse.Text.Module.from_file filename in
fun fmt () -> Text.pp_modul fmt v
| ".wast" ->
let+ v = Parse.Text.Script.from_file filename in
fun fmt () -> Text.pp_script fmt v
| ext -> Error (`Unsupported_file_extension ext)
let cmd_one inplace file =
match get_printer file with
| Error _e as e -> e
| Ok pp ->
if inplace then
let* res =
Bos.OS.File.with_oc file
(fun chan () ->
let fmt = Stdlib.Format.formatter_of_out_channel chan in
Ok (Format.pp fmt "%a@\n" pp ()) )
()
in
res
else Ok (Format.pp_std "%a@\n" pp ())
let cmd inplace files = list_iter (cmd_one inplace) files
let format_file_to_string file =
let+ pp = get_printer file in
Format.asprintf "%a@\n" pp ()