Usage
The 7 examples below are taken from zig-starter's README.
const std = @import("std");
const starter = @import("starter");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var lib = try starter.StarterLib.init(allocator);
defer lib.deinit();
const result = lib.processData("Hello, Zig!");
if (result.isOk()) {
const data = result.unwrap();
defer allocator.free(data);
std.debug.print("Processed: {s}\n", .{data});
}
}
const errors = @import("zig-error-handling");
fn riskyOperation() errors.Result(i32, error{Failed}) {
if (some_condition) {
return errors.Result(i32, error{Failed}).ok(42);
} else {
return errors.Result(i32, error{Failed}).err(error.Failed);
}
}
// Usage
const result = riskyOperation();
if (result.isOk()) {
std.debug.print("Success: {}\n", .{result.unwrap()});
} else {
std.debug.print("Error: {}\n", .{result.unwrapErr()});
}
const testing = @import("zig-test-framework");
test "example test" {
const value = 42;
try testing.expect(value == 42);
try testing.expectEqual(42, value);
}
test "string comparison" {
const str = "hello";
try testing.expectEqualStrings("hello", str);
}
test "error handling" {
try testing.expectError(error.EmptyData, someFunction());
}
.{
.name = "my-project",
.version = "0.1.0",
// ...
}
const my_dep_mod = b.addModule("my-dep", .{
.root_source_file = b.path("../my-new-dep/src/root.zig"),
.target = target,
});
// Add to imports
lib_mod.addImport("my-dep", my_dep_mod);
var new_cmd = try app.addCommand("mycommand", .{
.description = "My new command",
.handler = myCommandHandler,
});
try new_cmd.addArgument("arg", .{
.description = "Command argument",
.required = true,
});
.root_source_file = b.path("../zig-cli/src/root.zig"),