Skip to content

Commit

Permalink
add zig example
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Oct 3, 2024
1 parent e3326c0 commit b7c8998
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/ziggity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zig-out
zig-cache
26 changes: 26 additions & 0 deletions examples/ziggity/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const std = @import("std");

// if you're not using WASI, change this to .freestanding
const BUILD_TARGET_OS = .wasi;

pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{
.default_target = .{ .abi = .musl, .os_tag = BUILD_TARGET_OS, .cpu_arch = .wasm32 },
});
const pdk_module = b.dependency("extism-pdk", .{ .target = target, .optimize = optimize }).module("extism-pdk");

var plugin = b.addExecutable(.{
.name = "plugin",
.root_source_file = b.path("src/pdk.zig"),
.target = target,
.optimize = optimize,
});
if (BUILD_TARGET_OS == .wasi) {
plugin.wasi_exec_model = .reactor;
}
plugin.rdynamic = true;
plugin.entry = .disabled; // or add an empty `pub fn main() void {}` to your code
plugin.root_module.addImport("extism-pdk", pdk_module);
b.installArtifact(plugin);
}
36 changes: 36 additions & 0 deletions examples/ziggity/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.{
.name = "ziggity",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.@"extism-pdk" = .{
.url = "https://github.com/extism/zig-pdk/archive/refs/tags/v1.1.0.tar.gz",
.hash = "1220e0aae36b81e1b99f86717dc67101fe0dcdc57c0ac05e1100aefb291f6dc4f7e0",
},
},
.paths = .{
// This makes *all* files, recursively, included in this package. It is generally
// better to explicitly list the files and directories instead, to insure that
// fetching from tarballs, file system paths, and version control all result
// in the same contents hash.
"",
// For example...
//"build.zig",
//"build.zig.zon",
//"src",
//"LICENSE",
//"README.md",
},
}
21 changes: 21 additions & 0 deletions examples/ziggity/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const std = @import("std");
const schema = @import("schema.zig");
const Host = schema.Host;

///
/// It takes IncomingEvent as input (An incoming event)
pub fn handle(input: schema.IncomingEvent) !void {
if (!std.mem.eql(u8, "content", input.kind)) {
return;
}

_ = try Host.react(schema.OutgoingReaction{
.messageId = input.message.id,
.with = "🦎",
});

_ = try Host.react(schema.OutgoingReaction{
.messageId = input.message.id,
.with = "⚡️",
});
}
38 changes: 38 additions & 0 deletions examples/ziggity/src/pdk.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// THIS FILE WAS GENERATED BY `xtp-zig-bindgen`. DO NOT EDIT.
const std = @import("std");
const extism = @import("extism-pdk");

const user = @import("main.zig");
const schema = @import("schema.zig");

const _plugin = extism.Plugin.init(std.heap.wasm_allocator);

const ERR_PRINTING_MSG: []const u8 = "std.fmt.allocPrint failed when formatting plugin error";

export fn handle() i32 {
// Get the input data
// in JSON
const json_input = _plugin.getJsonOpt(schema.IncomingEvent, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
defer json_input.deinit();

var input = json_input.value();
// decode all the inner buffer fields from base64 (may be no-op)
input = (input.XXX__decodeBase64Fields() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
}).*;

// Call the implementation function
user.handle(input) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};

return 0;
}
Loading

0 comments on commit b7c8998

Please sign in to comment.