Skip to content

Commit 7c6cb83

Browse files
authored
feat: support zig master (#36)
* feat: support zig master * update README
1 parent 53562ec commit 7c6cb83

File tree

12 files changed

+83
-26
lines changed

12 files changed

+83
-26
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: CI
22

33
on:
44
workflow_dispatch:
5+
schedule:
6+
- cron: '10 20 * * *'
57
pull_request:
68
paths:
79
- "**.zig"
@@ -21,12 +23,12 @@ jobs:
2123
fail-fast: false
2224
matrix:
2325
os: [ubuntu-latest, macos-latest]
26+
zig: [0.11.0, master]
2427
steps:
2528
- uses: actions/checkout@v4
2629
- uses: goto-bus-stop/setup-zig@v2
2730
with:
28-
version: 0.11.0
29-
# version: master
31+
version: ${{ matrix.zig }}
3032
- name: Start services
3133
uses: ikalnytskyi/action-setup-postgres@v4
3234
with:
@@ -41,13 +43,16 @@ jobs:
4143
if: matrix.os == 'macos-latest'
4244
run: |
4345
echo "PKG_CONFIG_PATH=$(brew --prefix)/opt/libpq/lib/pkgconfig" >> ${GITHUB_ENV}
46+
- name: Hack
47+
if: matrix.zig == '0.11.0'
48+
run: |
49+
# 0.11.0 will report following error with zon file, so delete now.
50+
# error: TarUnsupportedFileType
51+
rm build.zig.zon
4452
- name: Run examples(Unix)
4553
if: matrix.os != 'windows-latest'
4654
run: |
47-
# TODO: 0.11.0 will report following error with zon file, so delete now.
48-
# error: TarUnsupportedFileType
4955
pkg-config --libs --cflags libpq
50-
rm build.zig.zon
5156
zig fmt --check src/
5257
zig build run-all --summary all
5358

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
zig 0.11.0
2-
# zig master
1+
# zig 0.11.0
2+
zig master

book-src/15-02-string.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# String Parsing
2+
3+
## String to number/enum
4+
5+
```zig
6+
{{#include ../src/15-02.zig }}
7+
```

book-src/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
- [Web Programming]()
2626

2727
- [Make HTTP requests](./05-01-http-requests.md)
28-
- [Download]()
2928

3029
- [Algorithms]()
3130

@@ -68,4 +67,4 @@
6867

6968
- [Text Processing]()
7069
- [Regex Expressions](15-01-regex.md)
71-
- [String](15-02-string.md)
70+
- [String Parsing](15-02-string.md)

book-src/intro.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
[![](https://github.com/zigcc/zig-cookbook/actions/workflows/ci.yml/badge.svg)](https://github.com/zigcc/zig-cookbook/actions/workflows/ci.yml)
44
[![](https://github.com/zigcc/zig-cookbook/actions/workflows/pages.yml/badge.svg)](https://github.com/zigcc/zig-cookbook/actions/workflows/pages.yml)
55

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

88
# How to use
99

1010
[The website](https://zigcc.github.io/zig-cookbook/) is generated by [mdbook](https://rust-lang.github.io/mdBook/), `mdbook serve` will start a server at `http://localhost:3000` for preview.
1111

1212
Each recipe is accompanied by an illustrative example named after its corresponding sequence number. These examples can be executed using the command `zig build run-{chapter-num}-{sequence-num}`, or `zig build run-all` to execute all.
1313

14-
> - Currently Zig 0.11.0 is required to run those examples.
14+
> - Currently both 0.11.0 and master are supported to run those recipes.
1515
> - Some example may depend on system libraries, use `make install-deps` to install them.
1616
17+
> Note: Some recipes can't compile in 0.11.0, so they are skipped.
18+
1719
# Contributing
1820

1921
This cookbook is a work in progress, and we welcome contributions from the community. If you have a favorite recipe that you'd like to share, please submit a [pull request](https://github.com/zigcc/zig-cookbook/pulls).

build.zig

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,17 @@ fn addExample(b: *std.Build, run_all: *std.build.Step) !void {
5353
.target = .{},
5454
});
5555
lib.addIncludePath(.{ .path = "lib" });
56-
lib.addCSourceFiles(
57-
&.{"lib/regex_slim.c"},
58-
&.{"-std=c99"},
59-
);
56+
if (gt_zig_0_11) {
57+
lib.addCSourceFiles(.{
58+
.files = &.{"lib/regex_slim.c"},
59+
.flags = &.{"-std=c99"},
60+
});
61+
} else {
62+
lib.addCSourceFiles(
63+
&.{"lib/regex_slim.c"},
64+
&.{"-std=c99"},
65+
);
66+
}
6067
lib.linkLibC();
6168
exe.linkLibrary(lib);
6269
exe.addIncludePath(.{ .path = "lib" });

src/01-02.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn main() !void {
2424
const md = try file.metadata();
2525
print("File size: {d}\n", .{md.size()});
2626

27-
var ptr = try std.os.mmap(
27+
const ptr = try std.os.mmap(
2828
null,
2929
20,
3030
std.os.PROT.READ | std.os.PROT.WRITE,

src/01-03.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ const std = @import("std");
44
const builtin = @import("builtin");
55
const fs = std.fs;
66
const print = std.debug.print;
7-
8-
const current_zig = builtin.zig_version;
7+
const is_zig_11 = builtin.zig_version.minor == 11;
98

109
pub fn main() !void {
1110
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1211
defer _ = gpa.deinit();
1312
const allocator = gpa.allocator();
1413

1514
// There are API changes between 0.11.0 and master, so we need to use different APIs depending on the version.
16-
var iter_dir = if (comptime current_zig.minor == 11)
15+
var iter_dir = if (is_zig_11)
1716
try fs.cwd().openIterableDir(".", .{
1817
.no_follow = true, // `true` means it won't dereference the symlinks.
1918
})
2019
else
21-
fs.cwd().openDir(".", .{ .iterate = true });
20+
try fs.cwd().openDir("src", .{ .iterate = true });
2221
defer iter_dir.close();
2322

2423
var walker = try iter_dir.walk(allocator);
@@ -30,7 +29,11 @@ pub fn main() !void {
3029
continue;
3130
}
3231

33-
const stat = try iter_dir.dir.statFile(entry.path);
32+
const stat = if (is_zig_11)
33+
try iter_dir.dir.statFile(entry.path)
34+
else
35+
try iter_dir.statFile(entry.path);
36+
3437
const last_modified = stat.mtime;
3538
const duration = now - last_modified;
3639
if (duration < std.time.ns_per_hour * 24) {

src/05-01.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const std = @import("std");
22
const print = std.debug.print;
33
const http = std.http;
4+
const is_zig_11 = @import("builtin").zig_version.minor == 11;
45

56
pub fn main() !void {
7+
if (!is_zig_11) {
8+
return;
9+
}
610
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
711
defer _ = gpa.deinit();
812
const allocator = gpa.allocator();

src/10-02.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ pub fn main() !void {
1212

1313
// Encode
1414
const encoded_length = Encoder.calcSize(src.len);
15-
var encoded_buffer = try allocator.alloc(u8, encoded_length);
15+
const encoded_buffer = try allocator.alloc(u8, encoded_length);
1616
defer allocator.free(encoded_buffer);
1717

1818
_ = Encoder.encode(encoded_buffer, src);
1919
try std.testing.expectEqualStrings("aGVsbG8gemln", encoded_buffer);
2020

2121
// Decode
2222
const decoded_length = try Decoder.calcSizeForSlice(encoded_buffer);
23-
var decoded_buffer = try allocator.alloc(u8, decoded_length);
23+
const decoded_buffer = try allocator.alloc(u8, decoded_length);
2424
defer allocator.free(decoded_buffer);
2525

2626
try Decoder.decode(decoded_buffer, encoded_buffer);

0 commit comments

Comments
 (0)