Current section

Files

Jump to
toolshed lib_src core save_value.ex
Raw

lib_src/core/save_value.ex

# SPDX-FileCopyrightText: 2022 Masatoshi Nishiguchi
# SPDX-FileCopyrightText: 2023 Frank Hunleth
#
# SPDX-License-Identifier: Apache-2.0
#
defmodule Toolshed.Core.SaveValue do
@doc """
Save a value to a file as Elixir terms
## Examples
# Save the contents of SystemRegistry to a file
iex> SystemRegistry.match(:_) |> save_value("/root/sr.txt")
:ok
"""
@spec save_value(any(), Path.t(), keyword()) :: :ok | {:error, File.posix()}
def save_value(value, path, inspect_opts \\ []) do
opts =
Keyword.merge([pretty: true, limit: :infinity, printable_limit: :infinity], inspect_opts)
contents = inspect(value, opts)
File.write(path, contents)
end
end