keep up with zig
keep up with zig
keep up with zig build system
BARE Message Encoding implementation in Zig.
The trunk
branch is tested against Zig master
branch on build.
# Use -Doptimize=Release{Fast, Safe, Small} for release build.
zig build
# Run tests.
zig build test
Use the Zig build system
to add the "bare"
module to your project.
Declare your types as usual, then use Encoder
to encode them,
and Decoder
to decode them:
const Foo = union(enum) {
x: u64,
y: []const u8,
z: void,
};
const Bar = struct {
a: f32,
b: u8,
c: Foo,
};
const x = Bar{
.a = 3.14,
.b = 2,
.c = .{ .y = "hello" },
};
test "example" {
var buf: [12]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);
const reader = fbs.reader();
const writer = fbs.writer();
var e = encoder(writer);
try e.encode(x);
try fbs.seekTo(0);
var d = decoder(std.testing.allocator, reader);
defer d.deinit();
const y = try d.decode(Bar);
std.log.info("x: {}", .{x});
std.log.info("y: {}", .{y});
}
Hash maps and slices require allocation;
call deinit
on a Decoder
to free any memory that was allocated.
All BARE types are supported, although data<length>
and data
are the same
as [length]u8
and []u8
, and string
is also the same as []u8
.
usize
and c_uint
are encoded as variable-length unsigned integers,
and isize
and c_int
are encoded as variable-length signed integers.
For map[type A]type B
, std.HashMap
is used.
When using Encoder
, all BARE invariants are enforced at compile-time.
There are tests ported from go-bare in an attempt to ensure interoperability. Please report any interoperability issues you find.
If you can port relevant test cases, that would also be very welcome. :)
There is no schema parser or code generation.
I don't need it myself, but I would gladly accept contributions.
Send patches and questions to ~alva/zig-bare@lists.sr.ht.