OCaml codecs for the Citation File Format (CFF)
at main 34 lines 1.1 kB view raw
1(*--------------------------------------------------------------------------- 2 Copyright (c) 2026 The ocaml-cff programmers. All rights reserved. 3 SPDX-License-Identifier: ISC 4 ---------------------------------------------------------------------------*) 5 6(** Unix file I/O for CFF. *) 7 8let of_yaml_string s = 9 let reader = Bytesrw.Bytes.Reader.of_string s in 10 Yamlt.decode ~layout:true Cff.jsont reader 11;; 12 13let to_yaml_string t = 14 let buf = Buffer.create 1024 in 15 let writer = Bytesrw.Bytes.Writer.of_buffer buf in 16 match Yamlt.encode ~format:Yamlt.Block Cff.jsont t ~eod:true writer with 17 | Ok () -> Ok (Buffer.contents buf) 18 | Error e -> Error e 19;; 20 21let of_file path = 22 match In_channel.with_open_text path In_channel.input_all with 23 | s -> of_yaml_string s 24 | exception Sys_error e -> Error e 25;; 26 27let to_file path t = 28 match to_yaml_string t with 29 | Error e -> Error e 30 | Ok s -> 31 (match Out_channel.with_open_text path (fun oc -> Out_channel.output_string oc s) with 32 | () -> Ok () 33 | exception Sys_error e -> Error e) 34;;