Class: Kobako::Transport::Yield

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

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag:, value:) ⇒ Yield

Returns a new instance of Yield.

Parameters:

  • tag: (Integer)
  • value: (Object)


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

Instance Attribute Details

#tagInteger (readonly)

Returns the value of attribute tag.

Returns:

  • (Integer)


10
11
12
# File 'sig/kobako/transport/yield.rbs', line 10

def tag
  @tag
end

#valueObject (readonly)

Returns the value of attribute value.

Returns:

  • (Object)


11
12
13
# File 'sig/kobako/transport/yield.rbs', line 11

def value
  @value
end

Class Method Details

.decode(bytes) ⇒ Yield

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.

Parameters:

  • bytes (String)

Returns:

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) # : String

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

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


29
# File 'sig/kobako/transport/yield.rbs', line 29

def ==: (untyped other) -> bool

#break?Boolean

Returns:

  • (Boolean)


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

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

#encodeString

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

Returns:

  • (String)


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

#hashInteger

Returns:

  • (Integer)


31
# File 'sig/kobako/transport/yield.rbs', line 31

def hash: () -> Integer

#ok?Boolean

Returns:

  • (Boolean)


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

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

#withYield

Parameters:

  • tag: (Integer)
  • value: (Object)

Returns:



19
# File 'sig/kobako/transport/yield.rbs', line 19

def with: (?tag: Integer, ?value: untyped) -> Yield