Skip to content

Commit 3676a3d

Browse files
authored
refactor(zig-cookbook): adopt Zig 0.16 I/O system (#122)
Refactor main entry points to accept std.process.Init. Migrate file I/O, networking, and crypto operations to use the new std.Io interface. Removes zigcli dependency and bumps minimum Zig version to 0.16.0.
1 parent b33690a commit 3676a3d

94 files changed

Lines changed: 452 additions & 431 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
public/
22
zig-out/
33
zig-cache/
4+
zig-pkg/
45
.zig-cache/
56
.DS_Store
67
.tool-versions

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEMINI.md

GEMINI.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Zig Cookbook - Project Context
2+
3+
## Project Overview
4+
`zig-cookbook` is a multilingual collection of Zig (0.16.x) programming recipes and examples. It demonstrates common programming tasks using Zig's standard library and specialized packages. The project generates a static documentation website using the [zine](https://zine-ssg.io) static site generator.
5+
6+
## Key Technologies
7+
- **Zig (0.16.0)**: The primary programming language.
8+
- **Zine**: Static site generator for documentation.
9+
- **Docker**: Used for running databases (Postgres, MySQL) required by some examples.
10+
- **system dependencies**: Some examples link against C libraries like `libpq`, `mysqlclient`, and `sqlite3`.
11+
12+
## Directory Structure
13+
- `assets/src/`: Contains all Zig example source files (e.g., `01-01.zig`).
14+
- `src/`: Contains localized documentation in Super Markdown (`.smd`) format, organized by language (`en-US`, `zh-CN`).
15+
- `lib/`: C header files and helper source code for C interop examples.
16+
- `layouts/`: Templates and UI components for the documentation website.
17+
- `i18n/`: Internationalization configuration files (`.ziggy`).
18+
19+
## Building and Running
20+
The project uses the standard Zig build system.
21+
22+
### Running Examples
23+
- **Specific Example**: `zig build run-{chapter}-{seq}` (e.g., `zig build run-01-01`).
24+
- **All Examples**: `zig build run-all`.
25+
- **Compile Check**: `zig build check`.
26+
27+
### Local Documentation
28+
- **Preview Site**: `make serve` (starts `zine` on port 1313).
29+
- **Zine Preview**: Alternatively, use `zine` directly.
30+
31+
### Dependencies & Environment
32+
- **Install System Libraries**: `make install-deps` (supports macOS via brew and Linux via apt).
33+
- **Databases**: `docker-compose up -d` to start the required Postgres and MySQL instances.
34+
- **Environment Variables**: `source env.sh` may be required on some systems to set `PKG_CONFIG_PATH` for database clients.
35+
36+
## Development Conventions
37+
- **Zig Version**: Targets Zig 0.16.x. Adheres to modern Zig idioms like `std.Io` and `std.process.Init` entry points.
38+
- **Testing**: Many examples include `std.testing` assertions within their `main` or as helper blocks to verify correctness.
39+
- **C Interop**: Uses `b.addTranslateC` in `build.zig` to interface with C libraries.
40+
- **Formatting**: The project uses `prettier` for linting `.smd` files (via `make lint`).
41+
42+
## Project-Specific Tips
43+
- When adding a new recipe:
44+
1. Add the Zig code to `assets/src/`.
45+
2. Create corresponding `.smd` files in `src/en-US/` and `src/zh-CN/`.
46+
3. Update `build.zig` if the example has special library dependencies.
47+
- Use `std.debug.print` for output in examples as they are meant for learning.

README-zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
[Zig Cookbook](https://github.com/zigcc/zig-cookbook) 是一系列简单的 Zig 示例程序合集,展示了完成常见编程任务的良好实践。
1111

12-
> - 主分支跟踪 Zig 0.15.x,并通过 GitHub Actions 在 Linux 和 macOS 上进行测试。
12+
> - 主分支跟踪 Zig 0.16.x,并通过 GitHub Actions 在 Linux 和 macOS 上进行测试。
1313
> - 更早版本的 Zig 支持可以在[其他分支](https://github.com/zigcc/zig-cookbook/branches)中找到。
1414
1515
# 如何使用

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
[Zig cookbook](https://github.com/zigcc/zig-cookbook) is a collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks.
1313

14-
> - Main branch tracks Zig 0.15.x, and is tested on Linux and macOS via GitHub actions.
14+
> - Main branch tracks Zig 0.16.x, and is tested on Linux and macOS via GitHub actions.
1515
> - Earlier Zig support could be found in [other branches](https://github.com/zigcc/zig-cookbook/branches).
1616
1717
# How to use

assets/src/01-01.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const std = @import("std");
2-
const fs = std.fs;
32
const print = std.debug.print;
43

5-
pub fn main() !void {
6-
const file = try fs.cwd().openFile("tests/zig-zen.txt", .{});
7-
defer file.close();
4+
pub fn main(init: std.process.Init) !void {
5+
const io = init.io;
6+
7+
const file = try std.Io.Dir.cwd().openFile(io, "tests/zig-zen.txt", .{});
8+
defer file.close(io);
89

910
var file_buffer: [4096]u8 = undefined;
10-
var reader = file.reader(&file_buffer);
11+
var reader = file.reader(io, &file_buffer);
1112
var line_no: usize = 0;
1213
while (try reader.interface.takeDelimiter('\n')) |line| {
1314
line_no += 1;

assets/src/01-02.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
const std = @import("std");
2-
const fs = std.fs;
3-
const print = std.debug.print;
42

53
const filename = "/tmp/zig-cookbook-01-02.txt";
64

7-
pub fn main() !void {
5+
pub fn main(init: std.process.Init) !void {
86
if (.windows == @import("builtin").os.tag) {
97
std.debug.print("MMap is not supported in Windows\n", .{});
108
return;
119
}
1210

13-
const file = try fs.cwd().createFile(filename, .{
11+
const io = init.io;
12+
const file = try std.Io.Dir.cwd().createFile(io, filename, .{
1413
.read = true,
1514
.truncate = true,
1615
.exclusive = false, // Set to true will ensure this file is created by us
1716
});
18-
defer file.close();
17+
defer file.close(io);
1918
const content_to_write = "hello zig cookbook";
2019

2120
// Before mmap, we need to ensure file isn't empty
22-
try file.setEndPos(content_to_write.len);
21+
try file.setLength(io, content_to_write.len);
2322

24-
const md = try file.stat();
23+
const md = try file.stat(io);
2524
try std.testing.expectEqual(md.size, content_to_write.len);
2625

2726
const ptr = try std.posix.mmap(
2827
null,
2928
content_to_write.len,
30-
std.posix.PROT.READ | std.posix.PROT.WRITE,
29+
.{ .READ = true, .WRITE = true },
3130
.{ .TYPE = .SHARED },
3231
file.handle,
3332
0,

assets/src/01-03.zig

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
//! Find files that have been modified in the last 24 hours
22

33
const std = @import("std");
4-
const builtin = @import("builtin");
5-
const fs = std.fs;
64
const print = std.debug.print;
75

8-
pub fn main() !void {
9-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10-
defer _ = gpa.deinit();
11-
const allocator = gpa.allocator();
6+
pub fn main(init: std.process.Init) !void {
7+
const gpa = init.gpa;
8+
const io = init.io;
129

13-
var iter_dir = try fs.cwd().openDir("src", .{ .iterate = true });
14-
defer iter_dir.close();
10+
var iter_dir = try std.Io.Dir.cwd().openDir(io, "src", .{ .iterate = true });
11+
defer iter_dir.close(io);
1512

16-
var walker = try iter_dir.walk(allocator);
13+
var walker = try iter_dir.walk(gpa);
1714
defer walker.deinit();
1815

19-
const now = std.time.nanoTimestamp();
20-
while (try walker.next()) |entry| {
16+
const now_ns = std.Io.Clock.real.now(io).nanoseconds;
17+
while (try walker.next(io)) |entry| {
2118
if (entry.kind != .file) {
2219
continue;
2320
}
2421

25-
const stat = try iter_dir.statFile(entry.path);
26-
const last_modified = stat.mtime;
27-
const duration = now - last_modified;
22+
const stat = try iter_dir.statFile(io, entry.path, .{});
23+
const last_modified = stat.mtime.nanoseconds;
24+
const duration = now_ns - last_modified;
2825
if (duration < std.time.ns_per_hour * 24) {
2926
print("Last modified: {d} seconds ago, size:{d} bytes, filename: {s}\n", .{
3027
@divTrunc(duration, std.time.ns_per_s),

assets/src/01-04.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Test file/directory existence
22

33
const std = @import("std");
4-
const fs = std.fs;
54

6-
pub fn main() !void {
5+
pub fn main(init: std.process.Init) !void {
6+
const io = init.io;
77
const filename = "build.zig";
88
var found = true;
9-
fs.cwd().access(filename, .{}) catch |e| switch (e) {
9+
std.Io.Dir.cwd().access(io, filename, .{}) catch |e| switch (e) {
1010
error.FileNotFound => found = false,
1111
else => return e,
1212
};

assets/src/01-05.zig

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
const std = @import("std");
2-
const fs = std.fs;
32
const print = std.debug.print;
43

5-
pub fn main() !void {
6-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7-
defer if (gpa.deinit() != .ok) @panic("leak");
8-
const allocator = gpa.allocator();
4+
pub fn main(init: std.process.Init) !void {
5+
const gpa = init.gpa;
6+
const io = init.io;
97

108
// In order to walk the directry, `iterate` must be set to true.
11-
var dir = try fs.cwd().openDir("zig-out", .{ .iterate = true });
12-
defer dir.close();
9+
var dir = try std.Io.Dir.cwd().openDir(io, "zig-out", .{ .iterate = true });
10+
defer dir.close(io);
1311

14-
var walker = try dir.walk(allocator);
12+
var walker = try dir.walk(gpa);
1513
defer walker.deinit();
1614

17-
while (try walker.next()) |entry| {
15+
while (try walker.next(io)) |entry| {
1816
print("path: {s}, basename:{s}, type:{s}\n", .{
1917
entry.path,
2018
entry.basename,

0 commit comments

Comments
 (0)