Skip to content

Commit 208d2d6

Browse files
authored
Merge pull request #118 from kubkon/update-zig
Updates for latest Zig
2 parents a6c2cd8 + e02c085 commit 208d2d6

9 files changed

Lines changed: 113 additions & 122 deletions

File tree

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
.gyro
2-
.zigmod
3-
deps.zig
4-
zig-cache
51
.zig-cache
62
zig-out
3+
.direnv

examples/yaml.zig

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const Yaml = @import("yaml").Yaml;
55

66
const mem = std.mem;
77

8-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
8+
var gpa_alloc = std.heap.GeneralPurposeAllocator(.{}){};
9+
const gpa = gpa_alloc.allocator();
910

1011
const usage =
1112
\\Usage: yaml <path-to-yaml>
@@ -54,29 +55,38 @@ fn logFn(
5455

5556
pub const std_options: std.Options = .{ .logFn = logFn };
5657

57-
pub fn main() !void {
58-
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
59-
defer arena.deinit();
60-
const allocator = arena.allocator();
58+
pub fn main(init: std.process.Init.Minimal) !void {
59+
var arena_alloc = std.heap.ArenaAllocator.init(gpa);
60+
defer arena_alloc.deinit();
61+
const arena = arena_alloc.allocator();
6162

62-
const all_args = try std.process.argsAlloc(allocator);
63+
var threaded: std.Io.Threaded = .init(gpa, .{
64+
.argv0 = .init(init.args),
65+
.environ = init.environ,
66+
});
67+
defer threaded.deinit();
68+
const io = threaded.io();
69+
70+
const all_args = try init.args.toSlice(arena);
6371
const args = all_args[1..];
6472

65-
const stdout = std.fs.File.stdout();
66-
const stderr = std.fs.File.stderr();
73+
var stdout_buffer: [1024]u8 = undefined;
74+
var stdout = std.Io.File.stdout().writer(io, &stdout_buffer);
75+
var stderr_buffer: [1024]u8 = undefined;
76+
var stderr = std.Io.File.stderr().writer(io, &stderr_buffer);
6777

6878
var file_path: ?[]const u8 = null;
6979
var arg_index: usize = 0;
7080
while (arg_index < args.len) : (arg_index += 1) {
7181
if (mem.eql(u8, "-h", args[arg_index]) or mem.eql(u8, "--help", args[arg_index])) {
72-
return stdout.writeAll(usage);
82+
return stdout.interface.writeAll(usage);
7383
} else if (mem.eql(u8, "--debug-log", args[arg_index])) {
7484
if (arg_index + 1 >= args.len) {
75-
return stderr.writeAll("fatal: expected [scope] after --debug-log\n\n");
85+
return stderr.interface.writeAll("fatal: expected [scope] after --debug-log\n\n");
7686
}
7787
arg_index += 1;
7888
if (!build_options.enable_logging) {
79-
try stderr.writeAll("warn: --debug-log will have no effect as program was not built with -Dlog\n\n");
89+
try stderr.interface.writeAll("warn: --debug-log will have no effect as program was not built with -Dlog\n\n");
8090
} else {
8191
try log_scopes.append(args[arg_index]);
8292
}
@@ -86,26 +96,28 @@ pub fn main() !void {
8696
}
8797

8898
if (file_path == null) {
89-
return stderr.writeAll("fatal: no input path to yaml file specified\n\n");
99+
return stderr.interface.writeAll("fatal: no input path to yaml file specified\n\n");
90100
}
91101

92-
const file = try std.fs.cwd().openFile(file_path.?, .{});
93-
defer file.close();
102+
const file = try std.Io.Dir.cwd().openFile(io, file_path.?, .{});
103+
defer file.close(io);
104+
105+
var file_buffer: [1024]u8 = undefined;
106+
var file_reader = file.reader(io, &file_buffer);
94107

95-
const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
108+
const source = try file_reader.interface.allocRemaining(arena, .unlimited);
96109

97110
var yaml: Yaml = .{ .source = source };
98-
defer yaml.deinit(allocator);
111+
defer yaml.deinit(arena);
99112

100-
yaml.load(allocator) catch |err| switch (err) {
113+
yaml.load(arena) catch |err| switch (err) {
101114
error.ParseFailure => {
102115
assert(yaml.parse_errors.errorMessageCount() > 0);
103-
yaml.parse_errors.renderToStdErr(.{ .ttyconf = std.io.tty.detectConfig(stderr) });
116+
yaml.parse_errors.renderToStderr(io, .{}, .auto) catch {};
104117
return error.ParseFailure;
105118
},
106119
else => return err,
107120
};
108121

109-
var writer = stdout.writer(&[0]u8{});
110-
try yaml.stringify(&writer.interface);
122+
try yaml.stringify(&stdout.interface);
111123
}

flake.lock

Lines changed: 20 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description = "Flake for developing zig-yaml";
33

44
inputs = {
5-
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
5+
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
66
flake-utils.url = "github:numtide/flake-utils";
77
zig.url = "github:mitchellh/zig-overlay";
88
zls.url = "github:zigtools/zls";
@@ -46,7 +46,8 @@
4646
buildInputs = [
4747
zig
4848
zls
49-
] ++ linuxSpecific;
49+
]
50+
++ linuxSpecific;
5051
};
5152

5253
# For compatibility with older versions of the `nix` binary

src/Parser.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,11 @@ fn getLineInfo(source: []const u8, line_col: LineCol) struct {
772772
};
773773

774774
const span_start: u32 = span_start: {
775-
const trimmed = mem.trimLeft(u8, line, " ");
775+
const trimmed = mem.trimStart(u8, line, " ");
776776
break :span_start @intCast(mem.indexOf(u8, line, trimmed).?);
777777
};
778778

779-
const span_end: u32 = @intCast(mem.trimRight(u8, line, " \r\n").len);
779+
const span_end: u32 = @intCast(mem.trimEnd(u8, line, " \r\n").len);
780780

781781
return .{
782782
.line = line,

src/Parser/test.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ fn parseError2(source: []const u8, comptime format: []const u8, args: anytype) !
630630

631631
var given: std.Io.Writer.Allocating = .init(testing.allocator);
632632
defer given.deinit();
633-
try bundle.renderToWriter(.{ .ttyconf = .no_color }, &given.writer);
633+
try bundle.renderToWriter(.{}, &given.writer);
634634

635635
const expected = try std.fmt.allocPrint(testing.allocator, format, args);
636636
defer testing.allocator.free(expected);

src/Yaml.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ pub const YamlError = error{
308308

309309
pub const StringifyError = error{
310310
OutOfMemory,
311-
} || YamlError || std.fs.File.WriteError || std.Io.Writer.Error;
311+
} || YamlError || std.Io.Writer.Error;
312312

313313
pub const List = []Value;
314314
pub const Map = std.StringArrayHashMapUnmanaged(Value);

0 commit comments

Comments
 (0)