Skip to content

majiayu000/spellbook / skills/zig-project

zig-project

Verified

Modern Zig project architecture guide. Use when creating Zig projects (systems programming, CLI tools, game dev, high-performance services). Covers explicit allocators, comptime, error handling, and build system.

templates/executable/src/lib.zig

View on GitHub
const std = @import("std");

/// Error set for library operations
pub const LibError = error{
    InvalidInput,
    FormatError,
};

/// Creates a greeting string. Caller owns returned memory.
pub fn createGreeting(allocator: std.mem.Allocator, name: []const u8) ![]u8 {
    if (name.len == 0) {
        return LibError.InvalidInput;
    }

    return std.fmt.allocPrint(allocator, "Hello, {s}!", .{name});
}

/// Example struct with explicit allocator
pub const Buffer = struct {
    allocator: std.mem.Allocator,
    data: []u8,

    pub fn init(allocator: std.mem.Allocator, size: usize) !Buffer {
        const data = try allocator.alloc(u8, size);
        return .{
            .allocator = allocator,
            .data = data,
        };
    }

    pub fn deinit(self: *Buffer) void {
        self.allocator.free(self.data);
        self.* = undefined;
    }

    pub fn fill(self: *Buffer, value: u8) void {
        @memset(self.data, value);
    }
};

// Tests

test "createGreeting with valid name" {
    const allocator = std.testing.allocator;

    const greeting = try createGreeting(allocator, "Zig");
    defer allocator.free(greeting);

    try std.testing.expectEqualStrings("Hello, Zig!", greeting);
}

test "createGreeting with empty name returns error" {
    const allocator = std.testing.allocator;

    const result = createGreeting(allocator, "");
    try std.testing.expectError(LibError.InvalidInput, result);
}

test "Buffer init and deinit" {
    const allocator = std.testing.allocator;

    var buffer = try Buffer.init(allocator, 1024);
    defer buffer.deinit();

    try std.testing.expect(buffer.data.len == 1024);
}

test "Buffer fill" {
    const allocator = std.testing.allocator;

    var buffer = try Buffer.init(allocator, 4);
    defer buffer.deinit();

    buffer.fill(0xAB);

    try std.testing.expectEqualSlices(u8, &[_]u8{ 0xAB, 0xAB, 0xAB, 0xAB }, buffer.data);
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall