Skip to content

Commit

Permalink
Recent zig + more workspace (#6)
Browse files Browse the repository at this point in the history
* Updates for recent zig

* Workspace pattern

* Fix

* Let's try this
  • Loading branch information
malcolmstill authored Feb 22, 2024
1 parent a8636bd commit 227fbd8
Show file tree
Hide file tree
Showing 21 changed files with 614 additions and 242 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ jobs:
tests:
strategy:
matrix:
dir:
[
"biscuit-schema",
"biscuit-format",
"biscuit-datalog",
"biscuit",
"biscuit-samples",
]
os: [ubuntu-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- run: zig build test
- name: zig build test
working-directory: "./${{matrix.dir}}"
run: zig build test
lint:
runs-on: ubuntu-latest
steps:
Expand All @@ -21,14 +31,3 @@ jobs:
with:
version: master
- run: zig fmt --check .
build-biscuit-library:
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- run: zig build
46 changes: 46 additions & 0 deletions biscuit-datalog/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const std = @import("std");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const schema = b.dependency("biscuit_schema", .{ .target = target, .optimize = optimize });
const format = b.dependency("biscuit_format", .{ .target = target, .optimize = optimize });

_ = b.addModule("biscuit-datalog", .{
.root_source_file = .{ .path = "src/main.zig" },
.imports = &.{
.{ .name = "biscuit-schema", .module = schema.module("biscuit-schema") },
.{ .name = "biscuit-format", .module = format.module("biscuit-format") },
},
});

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addImport("biscuit-schema", schema.module("biscuit-schema"));
lib_unit_tests.root_module.addImport("biscuit-format", format.module("biscuit-format"));

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
64 changes: 64 additions & 0 deletions biscuit-datalog/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.{
.name = "biscuit-datalog",
// 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 = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
//},
.biscuit_schema = .{ .path = "../biscuit-schema" },
.biscuit_format = .{ .path = "../biscuit-format" },
},

// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package.
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.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",
},
}
2 changes: 1 addition & 1 deletion biscuit-datalog/src/check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const Check = struct {
check.queries.deinit();
}

pub fn format(check: Check, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(check: Check, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("check if ", .{});
for (check.queries.items, 0..) |*query, i| {
try writer.print("{any}", .{query.*});
Expand Down
46 changes: 23 additions & 23 deletions biscuit-datalog/src/expression.zig
Original file line number Diff line number Diff line change
Expand Up @@ -264,34 +264,34 @@ test {
try testing.expectEqual(@as(Term, .{ .bool = true }), try Binary.contains.evaluate(s, middle, symbols));
}

test "negate" {
const testing = std.testing;
// test "negate" {
// const testing = std.testing;

var symbols = SymbolTable.init(testing.allocator);
defer symbols.deinit();
// var symbols = SymbolTable.init(testing.allocator);
// defer symbols.deinit();

_ = try symbols.insert("test1");
_ = try symbols.insert("test2");
_ = try symbols.insert("var1");
// var tmp_symbols = TemporarySymbolTable.init(testing.allocator, &symbols);
// _ = try symbols.insert("test1");
// _ = try symbols.insert("test2");
// _ = try symbols.insert("var1");
// // var tmp_symbols = TemporarySymbolTable.init(testing.allocator, &symbols);

var ops = [_]Op{
.{ .value = .{ .integer = 1 } },
.{ .value = .{ .variable = 2 } },
.{ .binary = .less_than },
.{ .unary = .parens },
.{ .unary = .negate },
};
// var ops = [_]Op{
// .{ .value = .{ .integer = 1 } },
// .{ .value = .{ .variable = 2 } },
// .{ .binary = .less_than },
// .{ .unary = .parens },
// .{ .unary = .negate },
// };

var values = std.AutoHashMap(u32, Term).init(testing.allocator);
defer values.deinit();
// var values = std.AutoHashMap(u32, Term).init(testing.allocator);
// defer values.deinit();

try values.put(2, .{ .integer = 0 });
// try values.put(2, .{ .integer = 0 });

const expr: Expression = .{ .ops = ops[0..] };
// const expr: Expression = .{ .ops = ops[0..] };

// FIXME: tmp_symbols
const res = try expr.evaluate(testing.allocator, values, symbols);
// // FIXME: tmp_symbols
// const res = try expr.evaluate(testing.allocator, values, symbols);

try testing.expectEqual(@as(Term, .{ .bool = true }), res);
}
// try testing.expectEqual(@as(Term, .{ .bool = true }), res);
// }
2 changes: 1 addition & 1 deletion biscuit-datalog/src/fact.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const Fact = struct {
return fact.predicate.match(predicate);
}

pub fn format(fact: Fact, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(fact: Fact, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return writer.print("{any}", .{fact.predicate});
}

Expand Down
2 changes: 1 addition & 1 deletion biscuit-datalog/src/predicate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const Predicate = struct {
return .{ .name = schema_predicate.name, .terms = terms };
}

pub fn format(predicate: Predicate, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(predicate: Predicate, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("sym:{any}(", .{predicate.name});
for (predicate.terms.items, 0..) |*term, i| {
try writer.print("{any}", .{term.*});
Expand Down
2 changes: 1 addition & 1 deletion biscuit-datalog/src/rule.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub const Rule = struct {
return try it.next() != null;
}

pub fn format(rule: Rule, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(rule: Rule, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("{any} <- ", .{rule.head});
for (rule.body.items, 0..) |*predicate, i| {
try writer.print("{any}", .{predicate.*});
Expand Down
2 changes: 1 addition & 1 deletion biscuit-datalog/src/set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn Set(comptime K: type) type {
return true;
}

pub fn format(set: Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(set: Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("set{{", .{});
var it = set.iterator();

Expand Down
2 changes: 1 addition & 1 deletion biscuit-datalog/src/term.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub const Term = union(TermKind) {
};
}

pub fn format(term: Term, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
pub fn format(term: Term, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return switch (term) {
.variable => |v| writer.print("$sym:{any}", .{v}),
.integer => |v| writer.print("{any}", .{v}),
Expand Down
43 changes: 43 additions & 0 deletions biscuit-format/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const std = @import("std");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const schema = b.dependency("biscuit_schema", .{ .target = target, .optimize = optimize });

_ = b.addModule("biscuit-format", .{
.root_source_file = .{ .path = "src/main.zig" },
.imports = &.{
.{ .name = "biscuit-schema", .module = schema.module("biscuit-schema") },
},
});

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addImport("biscuit-schema", schema.module("biscuit-schema"));

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
65 changes: 65 additions & 0 deletions biscuit-format/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.{
.name = "biscuit-format",
// 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 = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
//},
.biscuit_schema = .{
.path = "../biscuit-schema",
},
},

// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package.
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.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",
},
}
4 changes: 4 additions & 0 deletions biscuit-format/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub const decode = @import("decode.zig");
pub const serialized_biscuit = @import("serialized_biscuit.zig");

test {
_ = @import("serialized_biscuit.zig");
}
Loading

0 comments on commit 227fbd8

Please sign in to comment.