OmniBuf.Opaque (omnibuf v0.1.0)

Copy Markdown View Source

Lazy zero-decode Opaque packet views and in-place iodata wire splicing.

Unchanged fields are never decoded and are preserved as zero-copy sub-binaries until the final IO.iodata_to_binary/1 call.

Summary

Functions

Convenience helper that extracts a single WireSubMsg field's raw payload bytes, concatenating multiple occurrences if present so repeated submessages merge cleanly.

Scans bin in O(fields) time and returns all matching occurrences (in wire order) of target_field with target_wt, as a list of {wire_type, rest_after_tag}.

Scans bin in O(fields) time and returns the raw value binary (starting right after the tag) of the last occurrence of target_field with target_wt (or either WireBoolFalse/WireBoolTrue when target_wt == :bool).

Splices dirty field replacements into packet without decoding untouched fields.

Functions

extract_submsg(bin, field_num)

@spec extract_submsg(binary(), non_neg_integer()) ::
  {:ok, binary() | nil} | {:error, OmniBuf.Error.t()}

Convenience helper that extracts a single WireSubMsg field's raw payload bytes, concatenating multiple occurrences if present so repeated submessages merge cleanly.

find_all(bin, target_field, target_wt)

@spec find_all(binary(), non_neg_integer(), 0..15 | :bool) ::
  {:ok, [{0..15, binary()}]} | {:error, OmniBuf.Error.t()}

Scans bin in O(fields) time and returns all matching occurrences (in wire order) of target_field with target_wt, as a list of {wire_type, rest_after_tag}.

find_last(bin, target_field, target_wt)

@spec find_last(binary(), non_neg_integer(), 0..15 | :bool) ::
  {:ok, {:found, 0..15, binary()} | :not_found} | {:error, OmniBuf.Error.t()}

Scans bin in O(fields) time and returns the raw value binary (starting right after the tag) of the last occurrence of target_field with target_wt (or either WireBoolFalse/WireBoolTrue when target_wt == :bool).

Returns {:ok, {:found, wire_type, rest_after_tag}}, {:ok, :not_found}, or {:error, err}.

splice(packet, dirty)

@spec splice(binary(), %{optional(non_neg_integer()) => iodata()}) ::
  {:ok, binary()} | {:error, OmniBuf.Error.t()}

Splices dirty field replacements into packet without decoding untouched fields.

dirty is a map %{field_number => encoded_field_iodata} where encoded_field_iodata is the full [Tag, Value] iodata for that field (or [] to omit/clear the field).

Preserves untouched byte runs as zero-copy sub-binaries, drops duplicate occurrences of spliced fields, validates framing of skipped fields, and appends any dirty fields not originally present in packet in ascending field-number order.