add examples and integrate into build
I suppose the next step will be to translate the C examples into their zig counterparts. This is also good dog food.
This commit is contained in:
52
build.zig
52
build.zig
@@ -12,10 +12,11 @@ pub fn build(b: *std.Build) void {
|
|||||||
.source_file = .{ .path = "src/nats.zig" },
|
.source_file = .{ .path = "src/nats.zig" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const nats_c = nats_build.nats_c_lib(
|
const nats_c = nats_build.nats_c_lib(b, .{
|
||||||
b,
|
.name = "nats-c",
|
||||||
.{ .name = "nats-c", .target = target, .optimize = optimize },
|
.target = target,
|
||||||
);
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
const tests = b.addTest(.{
|
const tests = b.addTest(.{
|
||||||
.root_source_file = .{ .path = "tests/main.zig" },
|
.root_source_file = .{ .path = "tests/main.zig" },
|
||||||
@@ -26,8 +27,49 @@ pub fn build(b: *std.Build) void {
|
|||||||
tests.addModule("nats", nats);
|
tests.addModule("nats", nats);
|
||||||
tests.linkLibrary(nats_c);
|
tests.linkLibrary(nats_c);
|
||||||
|
|
||||||
b.installArtifact(tests);
|
|
||||||
const run_main_tests = b.addRunArtifact(tests);
|
const run_main_tests = b.addRunArtifact(tests);
|
||||||
const test_step = b.step("test", "Run tests");
|
const test_step = b.step("test", "Run tests");
|
||||||
test_step.dependOn(&run_main_tests.step);
|
test_step.dependOn(&run_main_tests.step);
|
||||||
|
|
||||||
|
add_examples(b, .{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
.nats_module = nats,
|
||||||
|
.nats_c = nats_c,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const ExampleOptions = struct {
|
||||||
|
target: std.zig.CrossTarget,
|
||||||
|
optimize: std.builtin.OptimizeMode,
|
||||||
|
nats_module: *std.Build.Module,
|
||||||
|
nats_c: *std.Build.Step.Compile,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Example = struct {
|
||||||
|
name: []const u8,
|
||||||
|
file: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const examples = [_]Example{
|
||||||
|
.{ .name = "request_reply", .file = "examples/request_reply.zig" },
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn add_examples(b: *std.build, options: ExampleOptions) void {
|
||||||
|
const example_step = b.step("examples", "build examples");
|
||||||
|
|
||||||
|
inline for (examples) |example| {
|
||||||
|
const ex_exe = b.addExecutable(.{
|
||||||
|
.name = example.name,
|
||||||
|
.root_source_file = .{ .path = example.file },
|
||||||
|
.target = options.target,
|
||||||
|
.optimize = options.optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
ex_exe.addModule("nats", options.nats_module);
|
||||||
|
ex_exe.linkLibrary(options.nats_c);
|
||||||
|
|
||||||
|
const install = b.addInstallArtifact(ex_exe, .{});
|
||||||
|
example_step.dependOn(&install.step);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
41
examples/request_reply.zig
Normal file
41
examples/request_reply.zig
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const nats = @import("nats");
|
||||||
|
|
||||||
|
fn onMessage(
|
||||||
|
userdata: *bool,
|
||||||
|
connection: *nats.Connection,
|
||||||
|
subscription: *nats.Subscription,
|
||||||
|
message: *nats.Message,
|
||||||
|
) void {
|
||||||
|
_ = subscription;
|
||||||
|
|
||||||
|
std.debug.print("Subject \"{s}\" received message: \"{s}\"\n", .{
|
||||||
|
message.getSubject(),
|
||||||
|
message.getData() orelse "[null]",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (message.getReply()) |reply| {
|
||||||
|
connection.publishString(reply, "salutations") catch @panic("HELP");
|
||||||
|
}
|
||||||
|
|
||||||
|
userdata.* = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
const connection = try nats.Connection.connectTo(nats.default_server_url);
|
||||||
|
defer connection.destroy();
|
||||||
|
|
||||||
|
var done = false;
|
||||||
|
const subscription = try connection.subscribe(bool, "channel", onMessage, &done);
|
||||||
|
defer subscription.destroy();
|
||||||
|
|
||||||
|
while (!done) {
|
||||||
|
const reply = try connection.requestString("channel", "greetings", 1000);
|
||||||
|
defer reply.destroy();
|
||||||
|
|
||||||
|
std.debug.print("Reply \"{s}\" got message: {s}\n", .{
|
||||||
|
reply.getSubject(),
|
||||||
|
reply.getData() orelse "[null]",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
34
src/nats.zig
34
src/nats.zig
@@ -38,40 +38,6 @@ pub const StatsCounts = sta_.StatsCounts;
|
|||||||
const Status = err_.Status;
|
const Status = err_.Status;
|
||||||
pub const Error = err_.Error;
|
pub const Error = err_.Error;
|
||||||
|
|
||||||
fn onMessage(userdata: *bool, connection: *Connection, subscription: *Subscription, message: *Message) void {
|
|
||||||
_ = subscription;
|
|
||||||
|
|
||||||
std.debug.print("Subject \"{s}\" received message: \"{s}\"\n", .{
|
|
||||||
message.getSubject(),
|
|
||||||
message.getData() orelse "[null]",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (message.getReply()) |reply| {
|
|
||||||
connection.publishString(reply, "salutations") catch @panic("HELP");
|
|
||||||
}
|
|
||||||
|
|
||||||
userdata.* = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() !void {
|
|
||||||
const connection = try Connection.connectTo(default_server_url);
|
|
||||||
defer connection.destroy();
|
|
||||||
|
|
||||||
var done = false;
|
|
||||||
const subscription = try connection.subscribe(bool, "channel", onMessage, &done);
|
|
||||||
defer subscription.destroy();
|
|
||||||
|
|
||||||
while (!done) {
|
|
||||||
const reply = try connection.requestString("channel", "greetings", 1000);
|
|
||||||
defer reply.destroy();
|
|
||||||
|
|
||||||
std.debug.print("Reply \"{s}\" got message: {s}\n", .{
|
|
||||||
reply.getSubject(),
|
|
||||||
reply.getData() orelse "[null]",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getVersion() [:0]const u8 {
|
pub fn getVersion() [:0]const u8 {
|
||||||
const verString = nats_c.nats_GetVersion();
|
const verString = nats_c.nats_GetVersion();
|
||||||
return std.mem.sliceTo(verString, 0);
|
return std.mem.sliceTo(verString, 0);
|
||||||
|
Reference in New Issue
Block a user