Module: Kobako::Codec::Utils

Defined in:
lib/kobako/codec/utils.rb

Overview

Byte-boundary helpers shared by the host-side encoder and decoder. Two concerns live here:

- UTF-8 assertion at the codec boundary
  ({docs/wire-codec.md}[link:../../../docs/wire-codec.md]
  § str/bin Encoding Rules and § Ext Types → ext 0x00). Used by
  {Decoder} when walking +str+ family payloads and by {Factory}
  when validating the +ext 0x00+ Symbol payload.
- +ArgumentError+ translation at the codec boundary
  ({with_boundary}) so the public taxonomy stays
  {Kobako::Codec::Error}.

Both helpers are pure — they only inspect inputs, never mutate them. The host↔guest Handle substitution walk lives in HandleWalk.

Class Method Summary collapse

Class Method Details

.assert_utf8!(string, label) ⇒ Object

Raise InvalidEncoding unless string‘s bytes are valid under its current encoding tag. label is the caller-supplied prefix for the error message (e.g. “str payload”, “Symbol payload”).

Raises:



27
28
29
30
31
# File 'lib/kobako/codec/utils.rb', line 27

def assert_utf8!(string, label)
  return if string.valid_encoding?

  raise InvalidEncoding, "#{label} is not valid UTF-8"
end

.with_boundaryObject

Run block at the codec boundary: a value object raises ArgumentError when an invariant is violated at construction, and this helper surfaces that as InvalidType so the public taxonomy stays Error and never leaks ArgumentError from the Ruby standard library.

Reach for this only where a value object is constructed outside a Decoder.decode block, whose rescue already performs the same mapping (worked example: Factory#unpack_handle building Handle.restore from a raw fixext payload). Do not use it for general-purpose validation outside the codec boundary —host-layer ArgumentError values should propagate unchanged.



45
46
47
48
49
# File 'lib/kobako/codec/utils.rb', line 45

def with_boundary
  yield
rescue ::ArgumentError => e
  raise InvalidType, e.message
end