Letterpress.Artifact (Letterpress v0.1.0)

Copy Markdown View Source

The immutable, portable result of compiling a notification template.

An artifact contains compiled subject, HTML, and text templates together with normalized variable definitions, compiler provenance, source maps, non-error diagnostics, and hashes that bind those fields together. It contains no recipient values or other resolved delivery data.

Persist or transport artifacts through encode/1 and decode/1. Decoding rejects missing fields, extra fields, unsupported versions, malformed provenance, invalid channel combinations, and content-hash mismatches.

Example

iex> schema = %{
...>   "version" => 1,
...>   "variables" => %{
...>     "name" => %{"type" => "string", "context" => "text"}
...>   }
...> }
iex> {:ok, artifact, []} =
...>   Letterpress.compile("text/liquid@1", "Hello {{ name }}", schema)
iex> {:ok, json} = Letterpress.Artifact.encode(artifact)
iex> Letterpress.Artifact.decode(json) == {:ok, artifact}
true

Summary

Types

t()

A verified version-1 compiled artifact.

Functions

Decodes and verifies an artifact map or JSON document.

Verifies an artifact and encodes it as canonical JSON.

Returns the JSON-native map covered by the artifact's content hash.

Types

t()

@type t() :: %Letterpress.Artifact{
  artifact_version: 1,
  compiler: map(),
  content_sha256: String.t(),
  html: String.t() | nil,
  lint: [map()],
  options_sha256: String.t(),
  profile: String.t(),
  schema_sha256: String.t(),
  source_map: map(),
  source_sha256: String.t(),
  subject: String.t() | nil,
  text: String.t() | nil,
  translation_units: [map()],
  variables: [map()]
}

A verified version-1 compiled artifact.

Functions

decode(json)

@spec decode(binary() | map()) :: {:ok, t()} | {:error, term()}

Decodes and verifies an artifact map or JSON document.

Map input must use string keys and JSON-native values. The success value is a t/0; failures return a stable reason atom or the JSON decoder error.

encode(artifact)

@spec encode(t()) :: {:ok, binary()} | {:error, term()}

Verifies an artifact and encodes it as canonical JSON.

Object keys are sorted and insignificant whitespace is omitted, so the same artifact produces the same bytes on every encode.

to_map(artifact)

@spec to_map(t()) :: map()

Returns the JSON-native map covered by the artifact's content hash.

This projection is useful when a JSON encoder or storage adapter owns the final serialization. Prefer encode/1 when exact canonical bytes matter.