Imp.Saving (Imp v0.5.0)

Copy Markdown View Source

JSON save/load helpers for portable program state.

Saved programs are treated as an external trust boundary. Loading validates the artifact shape, allowlists adapters and provider clients, and never restores credentials from disk.

Summary

Functions

Same as dump(program, []).

Returns program as a JSON-safe map that load/2 turns back into it.

Loads a program from the portable map dump/2 returns.

Loads a program from the portable map dump/2 returns, raising ArgumentError where load/2 returns an error.

Reads a program artifact written by save!/3 and returns the program.

Writes program to path as a checksummed JSON artifact, readable with read!/2. The file is written atomically with mode 0600 and carries no credentials. Takes the same :registry option as dump/2.

Functions

dump(program)

@spec dump(struct()) :: map()

Same as dump(program, []).

dump(program, opts)

@spec dump(struct(), keyword()) :: map()

Returns program as a JSON-safe map that load/2 turns back into it.

Credentials are left out. A program that holds callbacks (metrics, tool runners, policies) is dumped by their names in the :registry, an Imp.Saving.Registry.

load(state, opts \\ [])

@spec load(map(), keyword()) :: {:ok, struct()} | {:error, Exception.t()}

Loads a program from the portable map dump/2 returns.

Returns {:ok, program}, or {:error, %ArgumentError{}} when state is not a program Imp can load: a missing or unknown "type", a malformed field, an adapter or client outside the allowlist, or a callback name the :registry does not hold. Nothing in state is turned into an atom that does not already exist, and no credential is read from it.

Options:

  • :registry - an Imp.Saving.Registry naming the callbacks a saved program refers to (metrics, tool runners, policies).

load!(state, opts \\ [])

@spec load!(map(), keyword()) :: struct()

Loads a program from the portable map dump/2 returns, raising ArgumentError where load/2 returns an error.

read!(path, opts \\ [])

@spec read!(Path.t(), keyword()) :: struct()

Reads a program artifact written by save!/3 and returns the program.

The artifact's checksum is verified before anything in it is loaded. Raises File.Error when the file cannot be read and ArgumentError when it is not a checksummed Imp artifact or its program cannot be loaded. Takes the same :registry option as load/2.

save!(program, path, opts \\ [])

@spec save!(struct(), Path.t(), keyword()) :: :ok

Writes program to path as a checksummed JSON artifact, readable with read!/2. The file is written atomically with mode 0600 and carries no credentials. Takes the same :registry option as dump/2.