Module: Kobako::Codec::Decoder

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

Overview

Module-level entry point for the host side of the kobako wire (SPEC.md → Wire Codec → 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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kobako/codec/decoder.rb', line 26

def self.decode(bytes)
  value = Factory.load(bytes.b)
  validate_utf8!(value)
  value
# msgpack gem raises these for type/format violations; +ArgumentError+
# also comes from our ext-type validators (Handle id range, Exception
# type whitelist).
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
rescue ::EncodingError => e
  raise InvalidEncoding, e.message
end