Class: Kobako::Transport::Yield

Inherits:
Object
  • Object
show all
Defined in:
lib/kobako/transport/yield.rb

Overview

Value object for a single YieldResponse envelope (docs/wire-codec.md YieldResponse Envelope).

The wire form is a one-byte tag followed by an msgpack payload. The three live tags are 0x01 (ok), 0x02 (break), and 0x04 (error); 0x03 is reserved and rejected by both sides.

value carries whatever the wire payload decoded to — a plain Ruby value for the ok / break tags, and a {“class”, “message”, “backtrace”} Hash for the error tag. No further shape constraint is enforced here; the host-side dispatcher decides how to translate each variant into Ruby control flow.

Lives alongside the other envelope value objects (Request, Response) since it is the guest-to-host shape used mid-dispatch-frame to answer a __kobako_yield_to_block re-entry.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag:, value:) ⇒ Yield

Returns a new instance of Yield.



42
43
44
45
46
47
48
49
# File 'lib/kobako/transport/yield.rb', line 42

def initialize(tag:, value:)
  unless Kobako::Transport::LIVE_TAGS.include?(tag)
    raise ArgumentError,
          "Yield tag must be one of #{Kobako::Transport::LIVE_TAGS.inspect}, got #{tag.inspect}"
  end

  super
end

Class Method Details

.decode(bytes) ⇒ Object

Decode bytes into a Kobako::Transport::Yield. Rejects empty input, the reserved tag 0x03, and any tag outside LIVE_TAGS by raising Kobako::Codec::InvalidType — these are wire violations per the SPEC’s YieldResponse envelope contract.

Raises:



65
66
67
68
69
70
71
72
73
74
# File 'lib/kobako/transport/yield.rb', line 65

def self.decode(bytes)
  bytes = bytes.b
  raise Codec::InvalidType, "YieldResponse must carry at least one byte" if bytes.empty?

  tag = bytes.getbyte(0) # : Integer
  body = bytes.byteslice(1, bytes.bytesize - 1) || +""

  reject_dead_tag!(tag)
  new(tag: tag, value: Codec::Decoder.decode(body))
end

Instance Method Details

#break?Boolean

Returns:

  • (Boolean)


52
# File 'lib/kobako/transport/yield.rb', line 52

def break? = tag == Kobako::Transport::TAG_BREAK

#encodeObject

Encode this Yield to YieldResponse bytes: one tag byte followed by an msgpack-encoded value.



57
58
59
# File 'lib/kobako/transport/yield.rb', line 57

def encode
  [tag].pack("C") + Codec::Encoder.encode(value)
end

#error?Boolean

Returns:

  • (Boolean)


53
# File 'lib/kobako/transport/yield.rb', line 53

def error? = tag == Kobako::Transport::TAG_ERROR

#ok?Boolean

Returns:

  • (Boolean)


51
# File 'lib/kobako/transport/yield.rb', line 51

def ok?    = tag == Kobako::Transport::TAG_OK