|
| 1 | +(*******************************************************************************) |
| 2 | +(* Volgo - a Versatile OCaml Library for Git Operations *) |
| 3 | +(* Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com> *) |
| 4 | +(* *) |
| 5 | +(* This file is part of Volgo. *) |
| 6 | +(* *) |
| 7 | +(* Volgo is free software; you can redistribute it and/or modify it under *) |
| 8 | +(* the terms of the GNU Lesser General Public License as published by the *) |
| 9 | +(* Free Software Foundation either version 3 of the License, or any later *) |
| 10 | +(* version, with the LGPL-3.0 Linking Exception. *) |
| 11 | +(* *) |
| 12 | +(* Volgo is distributed in the hope that it will be useful, but WITHOUT ANY *) |
| 13 | +(* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *) |
| 14 | +(* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and *) |
| 15 | +(* the file `NOTICE.md` at the root of this repository for more details. *) |
| 16 | +(* *) |
| 17 | +(* You should have received a copy of the GNU Lesser General Public License *) |
| 18 | +(* and the LGPL-3.0 Linking Exception along with this library. If not, see *) |
| 19 | +(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *) |
| 20 | +(*******************************************************************************) |
| 21 | + |
| 22 | +include Dyn |
| 23 | + |
| 24 | +let inline_record cons fields = Dyn.variant cons [ Dyn.record fields ] |
| 25 | + |
| 26 | +let to_sexp = |
| 27 | + let module Sexp = Sexplib0.Sexp in |
| 28 | + let module S = Sexplib0.Sexp_conv in |
| 29 | + let rec aux (dyn : Dyn.t) : Sexp.t = |
| 30 | + match[@coverage off] dyn with |
| 31 | + | Opaque -> Atom "<opaque>" |
| 32 | + | Unit -> List [] |
| 33 | + | Int i -> S.sexp_of_int i |
| 34 | + | Int32 i -> S.sexp_of_int32 i |
| 35 | + | Record fields -> |
| 36 | + List (List.map (fun (field, t) -> Sexp.List [ Atom field; aux t ]) fields) |
| 37 | + | Variant (v, args) -> |
| 38 | + (* Special pretty print of variants holding records. *) |
| 39 | + (match args with |
| 40 | + | [] -> Atom v |
| 41 | + | [ Record fields ] -> |
| 42 | + List |
| 43 | + (Atom v :: List.map (fun (field, t) -> Sexp.List [ Atom field; aux t ]) fields) |
| 44 | + | _ -> List (Atom v :: List.map aux args)) |
| 45 | + | Bool b -> S.sexp_of_bool b |
| 46 | + | String a -> S.sexp_of_string a |
| 47 | + | Bytes a -> S.sexp_of_bytes a |
| 48 | + | Int64 i -> S.sexp_of_int64 i |
| 49 | + | Nativeint i -> S.sexp_of_nativeint i |
| 50 | + | Char c -> S.sexp_of_char c |
| 51 | + | Float f -> S.sexp_of_float f |
| 52 | + | Option o -> S.sexp_of_option aux o |
| 53 | + | List l -> S.sexp_of_list aux l |
| 54 | + | Array a -> S.sexp_of_array aux a |
| 55 | + | Tuple t -> List (List.map aux t) |
| 56 | + | Map m -> List (List.map (fun (k, v) -> Sexp.List [ aux k; aux v ]) m) |
| 57 | + | Set s -> List (List.map aux s) |
| 58 | + in |
| 59 | + aux |
| 60 | +;; |
0 commit comments