Module: Kobako::Codec::Decoder

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

Overview

Module-level entry point for the host side of the kobako wire (docs/wire-codec.md § Type Mapping).

Translates msgpack gem exceptions into the kobako error taxonomy (Truncated, InvalidType, InvalidEncoding, UnsupportedType) so callers can pattern-match on the SPEC’s wire-violation categories without leaking the gem’s internal exception classes.

Public API is a single function — Decoder.decode. The decoder is stateless; the MessagePack::Unpacker instance is built per call because callers always decode exactly one wire value at a time.

Class Method Summary collapse

Class Method Details

.decode(bytes) ⇒ Object

Decode bytes into one Ruby value and validate transitively against the SPEC type mapping. Raises Truncated, InvalidType, or InvalidEncoding on wire violations.

When a block is given, the decoded value is yielded and the block’s result is returned — wire Value Objects use this to build themselves from the decoded payload. The block runs inside this method’s rescue, so a Value Object’s ArgumentError invariant failure surfaces as InvalidType without a separate Utils.with_boundary wrapper at the call site.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kobako/codec/decoder.rb', line 33

def self.decode(bytes)
  value = Factory.load(bytes.b)
  validate_utf8!(value)
  block_given? ? yield(value) : value
# msgpack gem raises the format/type errors below; +ArgumentError+
# comes from our ext-type validators (Handle id range, Exception type
# whitelist) and from a yielded block's Value Object invariants — both
# are wire violations, so both map to {InvalidType}.
rescue ::MessagePack::UnknownExtTypeError, ::MessagePack::MalformedFormatError,
       ::MessagePack::StackError, ::ArgumentError => e
  raise InvalidType, e.message
# +UnpackError+ is the gem's umbrella class for short-read /
# incomplete-buffer faults; +EOFError+ covers underflow at the
# buffer edge.
rescue ::MessagePack::UnpackError, ::EOFError => e
  raise Truncated, e.message
end