Files
zeekay 41415a22ef Finish capnp→ZAP rebrand; fix Ruby 4 build + bump dev deps
Rebrand all ZAP-facing identifiers off Cap'n Proto:
- module Capnp→Zap, ext binding namespace ruby_capn_proto→ruby_zap,
  header guard RUBY_CAPN_PROTO_H→RUBY_ZAP_H, entry point Init_zap,
  gem capn_proto→zap, schemas .capnp→.zap, media/logo, gemspec, README,
  spec, examples. Zero capnp/Cap'n Proto brand prose remains.

Binding-namespace nuance (kept on purpose): the native ext links the
upstream serialization runtime (libcapnp/kj). Its capnp:: C++ symbols and
<capnp/...> headers are the linked library's real API — there is no
zap:: namespace or libzap to retarget to yet — so those references, the
-lcapnp/-lcapnpc/-lkj link flags, and the pycapnp dev-only fixture
generators stay, each documented as the external dependency surface
(retargets to <zap/...>/-lzap when the ZAP C++ runtime ships).

Build/modernization:
- extconf.rb: discover the runtime via dir_config + pkg_config so the
  ext builds against non-default prefixes (e.g. Homebrew), not just
  /usr/local. Native ext now compiles + links cleanly (zap.bundle).
- ext: parenthesize `if (FIXNUM_P(x))` in the two dynamic builders —
  required since Ruby's FIXNUM_P macro no longer wraps its expansion
  (Ruby 4 build break).
- Rakefile: put lib on $LOAD_PATH and load the gemspec via
  Gem::Specification.load; pin rake-compiler ~> 0.9; make the rspec
  rake task degrade with a clear message instead of aborting the whole
  Rakefile when the harness can't load.
- gemspec: bump dev deps within-major (rspec ~> 2.14, rake ~> 13.0,
  rake-compiler ~> 0.9); add required_ruby_version.
- .gitignore: ignore native-ext build artifacts (*.o, Makefile,
  mkmf.log, *.bundle.dSYM, *.gem).

Verified: all spec/zap_spec.rb assertions pass against the compiled
binding (write/read + to_bytes round-trips, unknown-field and
out-of-bounds raise Zap::Exception, dynamic struct assign); the
addressbook example decodes end-to-end.
2026-06-19 23:57:58 -07:00

130 lines
2.8 KiB
Ruby

require 'zap/zap'
require 'zap/version'
module Zap
ListNestedNodeReader.class_eval do
include Enumerable
def each
return to_enum(:each) unless block_given?
(0...size).each do |n|
yield self[n]
end
end
end
DynamicListReader.class_eval do
include Enumerable
def each
return to_enum(:each) unless block_given?
(0...size).each do |n|
yield self[n]
end
end
end
DynamicListBuilder.class_eval do
include Enumerable
def each
return to_enum(:each) unless block_given?
(0...size).each do |n|
yield self[n]
end
end
end
DynamicStructReader.class_eval do
def method_missing(name, *args, &block)
name = name.to_s
if name.end_with?("?")
which == name[0..-2]
else
self[name]
end
end
end
DynamicStructBuilder.class_eval do
def method_missing(name, *args, &block)
name = name.to_s
if name.start_with?("init") && name.size > 4
name = name[4..-1]
name[0] = name[0].downcase
init(name, *args)
elsif name.end_with?("=")
name = name[0..-2]
self[name] = args[0]
elsif name.end_with?("?")
which == name[0..-2]
else
self[name]
end
end
end
module SchemaLoader
def schema_parser
@schema_parser
end
def load_schema(file_name, imports=[])
display_name = self.name
@schema_parser ||= Zap::SchemaParser.new
load_schema_rec = Proc.new do |schema, mod|
node = schema.get_proto
nested_nodes = node.nested_nodes
if node.struct?
struct_schema = schema.as_struct
mod.instance_variable_set(:@schema, struct_schema)
mod.extend(Struct)
end
nested_nodes.each do |nested_node|
const_name = nested_node.name
const_name[0] = const_name[0].upcase
nested_mod = mod.const_set(const_name, Module.new)
nested_schema = schema.get_nested(nested_node.name)
load_schema_rec.call(nested_schema, nested_mod)
end
end
schema = @schema_parser.parse_disk_file(
display_name,
file_name,
imports);
load_schema_rec.call(schema, self)
end
module Struct
def schema
@schema
end
def read_from(io)
reader = StreamFdMessageReader.new(io)
reader.get_root(self)
end
def make_from_bytes(bytes)
# `bytes` is a String holding a flat ZAP message.
reader = FlatArrayMessageReader.new(bytes)
reader.get_root(self)
end
def new_message
builder = MallocMessageBuilder.new
builder.init_root(self)
end
def read_packed_from(io)
raise 'not implemented'
end
end
end
end