Iconvex.CodecSupport (iconvex v0.1.1)

Copy Markdown View Source

Shared callback helpers for codecs backed by Iconvex.TableCodec.

Package codecs must make table ownership explicit. Use table_entry/2 when the table belongs to a known OTP application, or provider_entry/1 when the table application is selected through Iconvex.Tables provider routing:

entry = Iconvex.CodecSupport.table_entry(:my_encoding, :my_package)
Iconvex.CodecSupport.decode(entry, bytes)

provider_entry = Iconvex.CodecSupport.provider_entry(:shared_encoding)
Iconvex.CodecSupport.encode(provider_entry, codepoints)

The helpers preserve the Iconvex.TableCodec strict, discard, substitution, streaming, and direct UTF-8 contracts. Direct UTF-8 fallback also preserves global first-error ordering: an unrepresentable character in the valid prefix wins over malformed UTF-8 that occurs later.

Summary

Types

The target-side policy accepted by chunk encoders.

A table entry accepted by the codec helpers.

An entry resolved through the current table-provider route.

An entry pinned to a table-owning OTP application.

Functions

Decodes a complete source binary using strict error handling.

Decodes one source chunk, retaining an incomplete suffix when non-final.

Decodes a complete source binary while discarding malformed units.

Returns the source bytes consumed when recovering from a decode error.

Decodes directly to UTF-8.

Encodes codepoints using strict unrepresentable-character handling.

Encodes one target chunk and retains any incomplete codepoint prefix.

Encodes codepoints while discarding unrepresentable characters.

Encodes UTF-8 directly, falling back through codepoints when required.

Encodes codepoints and invokes replacer for each unrepresentable character.

Applies substitution one codepoint at a time using an arbitrary encoder.

Builds a substituted codepoint sequence, then passes it once to encoder.

Validates UTF-8 and passes decoded codepoints to encoder.

Uses valid_encoder for valid UTF-8 and prefix_encoder only to establish first-error ordering when the source is malformed.

Returns the first malformed UTF-8 suffix and its absolute byte offset.

Builds an entry whose table application is selected by provider routing.

Builds an entry pinned to table_app.

Types

encode_policy()

@type encode_policy() :: :error | :discard | {:replace, (integer() -> [integer()])}

The target-side policy accepted by chunk encoders.

entry()

@type entry() :: table_entry() | provider_entry()

A table entry accepted by the codec helpers.

provider_entry()

@type provider_entry() :: %{id: atom()}

An entry resolved through the current table-provider route.

table_entry()

@type table_entry() :: %{id: atom(), table_app: atom()}

An entry pinned to a table-owning OTP application.

Functions

decode(entry, input)

@spec decode(entry(), binary()) :: {:ok, [integer()]} | tuple()

Decodes a complete source binary using strict error handling.

decode_chunk(entry, input, final?)

@spec decode_chunk(entry(), binary(), boolean()) ::
  {:ok, [integer()], binary()} | tuple()

Decodes one source chunk, retaining an incomplete suffix when non-final.

decode_discard(entry, input)

@spec decode_discard(entry(), binary()) :: {:ok, [integer()]}

Decodes a complete source binary while discarding malformed units.

decode_error_consumption(kind, sequence)

@spec decode_error_consumption(atom(), binary()) :: pos_integer()

Returns the source bytes consumed when recovering from a decode error.

decode_to_utf8(entry, input)

@spec decode_to_utf8(entry(), binary()) :: {:ok, binary()} | tuple()

Decodes directly to UTF-8.

Single-unit tables use the table engine's direct path. Other tables fall back to codepoints without changing errors or offsets.

encode(entry, codepoints)

@spec encode(entry(), [integer()]) :: {:ok, binary()} | tuple()

Encodes codepoints using strict unrepresentable-character handling.

encode_chunk(entry, codepoints, final?, policy)

@spec encode_chunk(entry(), [integer()], boolean(), encode_policy()) ::
  {:ok, binary(), [integer()]} | tuple()

Encodes one target chunk and retains any incomplete codepoint prefix.

encode_discard(entry, codepoints)

@spec encode_discard(entry(), [integer()]) :: {:ok, binary()}

Encodes codepoints while discarding unrepresentable characters.

encode_from_utf8(entry, input)

@spec encode_from_utf8(entry(), binary()) :: {:ok, binary()} | tuple()

Encodes UTF-8 directly, falling back through codepoints when required.

If a representable UTF-8 prefix already contains an unrepresentable target character, that target error is returned before a later malformed UTF-8 suffix.

encode_substitute(entry, codepoints, replacer)

@spec encode_substitute(entry(), [integer()], (integer() -> [integer()])) ::
  {:ok, binary()} | tuple()

Encodes codepoints and invokes replacer for each unrepresentable character.

encode_substitute_each(codepoints, encoder, replacer)

@spec encode_substitute_each(
  [integer()],
  ([integer()] -> {:ok, binary()} | tuple()),
  (integer() ->
     [integer()])
) ::
  {:ok, binary()} | tuple()

Applies substitution one codepoint at a time using an arbitrary encoder.

This is useful for codecs whose encoder cannot be expressed as a table entry but follows the standard unrepresentable-character tuple contract.

encode_substitute_transform(codepoints, encoder, replacer)

@spec encode_substitute_transform(
  [integer()],
  ([integer()] -> {:ok, binary()} | tuple()),
  (integer() ->
     [
       integer()
     ])
) ::
  {:ok, binary()} | tuple()

Builds a substituted codepoint sequence, then passes it once to encoder.

Each original and replacement segment is first validated independently, so replacement failures retain the encoder's original error tuple.

encode_utf8(input, encoder)

@spec encode_utf8(binary(), ([integer()] -> term())) :: term()

Validates UTF-8 and passes decoded codepoints to encoder.

Target errors in a valid prefix take precedence over a later malformed source suffix.

encode_utf8(input, valid_encoder, prefix_encoder)

@spec encode_utf8(binary(), (binary() -> term()), ([integer()] -> term())) :: term()

Uses valid_encoder for valid UTF-8 and prefix_encoder only to establish first-error ordering when the source is malformed.

malformed_utf8(input, offset \\ 0)

@spec malformed_utf8(binary(), non_neg_integer()) :: tuple()

Returns the first malformed UTF-8 suffix and its absolute byte offset.

provider_entry(id)

@spec provider_entry(atom()) :: provider_entry()

Builds an entry whose table application is selected by provider routing.

table_entry(id, table_app)

@spec table_entry(atom(), atom()) :: table_entry()

Builds an entry pinned to table_app.