# `Imp.Saving`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/saving.ex#L1)

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.

# `dump`

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

Same as `dump(program, [])`.

# `dump`

```elixir
@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`

```elixir
@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!`

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

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

# `read!`

```elixir
@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!`

```elixir
@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`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
