Module: Kobako::Codec::Utils

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

Overview

Wire-codec helpers shared by the host-side encoders and decoders. The single concern today is UTF-8 assertion at the wire boundary (SPEC.md → Wire Codec → str/bin Encoding Rules and Ext Types →ext 0x00). Two call sites lean on this:

- {Decoder} validates +str+ family payloads as it walks the
  decoded value tree.
- {Factory} validates the +ext 0x00+ Symbol payload after
  re-tagging the binary bytes as UTF-8.

Encoding setup (re-tagging binary as UTF-8 when needed) stays at the caller — only the assertion shape is shared. The helper does not mutate string; it only inspects String#valid_encoding? against string‘s current encoding tag.

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”, “ext 0x00 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

.wire_boundaryObject

Run block at the wire boundary: every wire Value Object (Handle / Fault / Request / Response / Panic) raises ArgumentError when an invariant is violated at construction, and the wire boundary surfaces those violations to callers as InvalidType so the public taxonomy stays Error and never leaks ArgumentError from the Ruby standard library.

Wrap any block that constructs a wire Value Object from decoded bytes with this helper to keep the five decode sites uniform —Request / Response in Kobako::RPC, Panic map in Kobako::Outcome, and the Handle / Fault ext-type unpackers in Factory. Do not use it for general-purpose validation outside the wire boundary — host-layer ArgumentError values should propagate unchanged.



48
49
50
51
52
# File 'lib/kobako/codec/utils.rb', line 48

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