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
open Ast

(*
--# Serialize Value
--#
--# Serializes a value to a `.tobj` file.
--#
--# @name serialize
--# @param value :: Any Value to serialize.
--# @param path :: String Output file path.
--# @return :: NA
--# @family base
--# @seealso deserialize
--# @export
*)
let register env =
  Env.add "serialize"
    (make_builtin ~name:"serialize" 2 (fun args _env ->
      match args with
      | [value; VString path] ->
          let value = Arrow_bridge.prepare_value_for_serialization value in
          (match Serialization.serialize_to_file path value with
          | Ok () -> (VNA NAGeneric)
          | Error msg -> Error.make_error FileError (Printf.sprintf "serialize failed: %s" msg))
      | [_; _] -> Error.type_error "Function `serialize` expects (Any, String)."
      | _ -> Error.arity_error_named "serialize" 2 (List.length args)
    ))
    env