Skip to content

Commit 82cc7bd

Browse files
committed
wip: upgrade to zig-master
the main breakage here is caused by writergate. there's some weirdness going on to do with file paths and the build system too though, so that should be dealt with before merging. also the new writer and reader don't provide any error specificity, so we don't know why a read or a write failed now. this should also be considered: perhaps creating a custom interface is warranted?
1 parent c0411db commit 82cc7bd

File tree

10 files changed

+200
-183
lines changed

10 files changed

+200
-183
lines changed

build.zig.zon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
.fingerprint = 0x9947018c924eecb2,
55
.dependencies = .{
66
.zfat = .{
7-
.url = "https://github.com/ZigEmbeddedGroup/zfat/archive/3ce06d43a4e04d387034dcae2f486b050701f321.tar.gz",
8-
.hash = "zfat-0.0.0-AAAAAMYlcABdh06Mn9CNk8Ccy_3bBFgJr8wo4jKza1q-",
7+
.url = "https://github.com/CascadeOS/zfat/archive/refs/heads/0.15.zip",
8+
.hash = "zfat-0.16.0-SNNK9fKtTgASssfmCblZwRMLU4pndVtwxTNhYCegBOyA",
99
},
1010
.args = .{
11-
.url = "git+https://github.com/ikskuh/zig-args.git#9425b94c103a031777fdd272c555ce93a7dea581",
12-
.hash = "args-0.0.0-CiLiqv_NAAC97fGpk9hS2K681jkiqPsWP6w3ucb_ctGH",
11+
.url = "git+https://github.com/ikskuh/zig-args.git#8ae26b44a884ff20dca98ee84c098e8f8e94902f",
12+
.hash = "args-0.0.0-CiLiqojRAACGzDRO7A9dw7kWSchNk29caJZkXuMCb0Cn",
1313
},
14-
},
14+
},
1515
.paths = .{
1616
"build.zig",
1717
"build.zig.zon",

src/BuildInterface.zig

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ pub fn createDisk(dimmer: Interface, size: u64, content: Content) std.Build.Lazy
6363
}
6464

6565
fn renderContent(wfs: *std.Build.Step.WriteFile, allocator: std.mem.Allocator, content: Content) struct { []const u8, ContentWriter.VariableMap } {
66-
var code: std.ArrayList(u8) = .init(allocator);
66+
var code = std.Io.Writer.Allocating.init(allocator);
6767
defer code.deinit();
6868

6969
var variables: ContentWriter.VariableMap = .init(allocator);
7070

7171
var cw: ContentWriter = .{
72-
.code = code.writer(),
72+
.code = &code.writer,
7373
.wfs = wfs,
7474
.vars = &variables,
7575
};
@@ -99,7 +99,7 @@ const ContentWriter = struct {
9999
pub const VariableMap = std.StringArrayHashMap(struct { std.Build.LazyPath, ContentWriter.UsageHint });
100100

101101
wfs: *std.Build.Step.WriteFile,
102-
code: std.ArrayList(u8).Writer,
102+
code: *std.Io.Writer,
103103
vars: *VariableMap,
104104

105105
fn render(cw: ContentWriter, content: Content) !void {
@@ -117,7 +117,7 @@ const ContentWriter = struct {
117117
},
118118

119119
.paste_file => |data| {
120-
try cw.code.print("paste-file {}", .{cw.fmtLazyPath(data, .file)});
120+
try cw.code.print("paste-file {f}", .{cw.fmtLazyPath(data, .file)});
121121
},
122122

123123
.mbr_part_table => |data| {
@@ -176,7 +176,7 @@ const ContentWriter = struct {
176176
try cw.code.writeByte('\n');
177177

178178
if (part.name) |name| {
179-
try cw.code.print(" name \"{}\"\n", .{std.zig.fmtEscapes(name)});
179+
try cw.code.print(" name \"{f}\"\n", .{std.zig.fmtString(name)});
180180
}
181181
if (part.offset) |offset| {
182182
try cw.code.print(" offset {d}\n", .{offset});
@@ -198,7 +198,7 @@ const ContentWriter = struct {
198198
@tagName(data.format),
199199
});
200200
if (data.label) |label| {
201-
try cw.code.print(" label {}\n", .{
201+
try cw.code.print(" label {f}\n", .{
202202
fmtPath(label),
203203
});
204204
}
@@ -213,29 +213,29 @@ const ContentWriter = struct {
213213
fn renderFileSystemTree(cw: ContentWriter, fs: FileSystem) !void {
214214
for (fs.items) |item| {
215215
switch (item) {
216-
.empty_dir => |dir| try cw.code.print("mkdir {}\n", .{
216+
.empty_dir => |dir| try cw.code.print("mkdir {f}\n", .{
217217
fmtPath(dir),
218218
}),
219219

220-
.copy_dir => |copy| try cw.code.print("copy-dir {} {}\n", .{
220+
.copy_dir => |copy| try cw.code.print("copy-dir {f} {f}\n", .{
221221
fmtPath(copy.destination),
222222
cw.fmtLazyPath(copy.source, .directory),
223223
}),
224224

225-
.copy_file => |copy| try cw.code.print("copy-file {} {}\n", .{
225+
.copy_file => |copy| try cw.code.print("copy-file {f} {f}\n", .{
226226
fmtPath(copy.destination),
227227
cw.fmtLazyPath(copy.source, .file),
228228
}),
229229

230-
.include_script => |script| try cw.code.print("!include {}\n", .{
230+
.include_script => |script| try cw.code.print("!include {f}\n", .{
231231
cw.fmtLazyPath(script, .file),
232232
}),
233233
}
234234
}
235235
}
236236

237-
const PathFormatter = std.fmt.Formatter(formatPath);
238-
const LazyPathFormatter = std.fmt.Formatter(formatLazyPath);
237+
const PathFormatter = std.fmt.Alt([]const u8, formatPath);
238+
const LazyPathFormatter = std.fmt.Alt(struct { ContentWriter, std.Build.LazyPath, UsageHint }, formatLazyPath);
239239
const UsageHint = enum { file, directory };
240240

241241
fn fmtLazyPath(cw: ContentWriter, path: std.Build.LazyPath, hint: UsageHint) LazyPathFormatter {
@@ -248,13 +248,9 @@ const ContentWriter = struct {
248248

249249
fn formatLazyPath(
250250
data: struct { ContentWriter, std.Build.LazyPath, UsageHint },
251-
comptime fmt: []const u8,
252-
options: std.fmt.FormatOptions,
253-
writer: anytype,
254-
) !void {
251+
writer: *std.Io.Writer,
252+
) error{WriteFailed}!void {
255253
const cw, const path, const hint = data;
256-
_ = fmt;
257-
_ = options;
258254

259255
switch (path) {
260256
.cwd_relative,
@@ -267,7 +263,7 @@ const ContentWriter = struct {
267263

268264
std.debug.assert(std.fs.path.isAbsolute(full_path));
269265

270-
try writer.print("{}", .{
266+
try writer.print("{f}", .{
271267
fmtPath(full_path),
272268
});
273269
},
@@ -278,7 +274,7 @@ const ContentWriter = struct {
278274
const var_id = cw.vars.count() + 1;
279275
const var_name = cw.wfs.step.owner.fmt("PATH{}", .{var_id});
280276

281-
try cw.vars.put(var_name, .{ path, hint });
277+
cw.vars.put(var_name, .{ path, hint }) catch return error.WriteFailed;
282278

283279
try writer.print("${s}", .{var_name});
284280
},
@@ -287,13 +283,8 @@ const ContentWriter = struct {
287283

288284
fn formatPath(
289285
path: []const u8,
290-
comptime fmt: []const u8,
291-
options: std.fmt.FormatOptions,
292-
writer: anytype,
286+
writer: *std.Io.Writer,
293287
) !void {
294-
_ = fmt;
295-
_ = options;
296-
297288
const is_safe_word = for (path) |char| {
298289
switch (char) {
299290
'A'...'Z',
@@ -318,7 +309,7 @@ const ContentWriter = struct {
318309
if (c == '\\') {
319310
try writer.writeAll("/");
320311
} else {
321-
try writer.print("{}", .{std.zig.fmtEscapes(&[_]u8{c})});
312+
try writer.print("{f}", .{std.zig.fmtString(&[_]u8{c})});
322313
}
323314
}
324315

@@ -419,7 +410,7 @@ pub const FatFs = struct {
419410

420411
pub const FileSystemBuilder = struct {
421412
b: *std.Build,
422-
list: std.ArrayListUnmanaged(FileSystem.Item),
413+
list: std.ArrayList(FileSystem.Item),
423414

424415
pub fn init(b: *std.Build) FileSystemBuilder {
425416
return FileSystemBuilder{

src/Parser.zig

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ pub fn get_include_path(parser: Parser, allocator: std.mem.Allocator, rel_includ
103103
if (parser.file_stack.len == parser.max_include_depth)
104104
return error.MaxIncludeDepthReached;
105105

106-
const top_path = if (parser.file_stack.len > 0)
107-
parser.file_stack[parser.file_stack.len - 1].path
108-
else
109-
"";
106+
// const top_path = if (parser.file_stack.len > 0)
107+
// parser.file_stack[parser.file_stack.len - 1].path
108+
// else
109+
// "";
110+
111+
const top_path = ""; // TODO what the fuck, the actual issue here needs to be triaged. this workaround fixes things for me for now though.
110112

111113
const abs_include_path = try std.fs.path.resolvePosix(
112114
allocator,
@@ -208,10 +210,12 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
208210
if (!has_includes)
209211
return content_slice;
210212

211-
var unescaped: std.ArrayList(u8) = .init(parser.arena.allocator());
212-
defer unescaped.deinit();
213+
const allocator = parser.arena.allocator();
214+
215+
var unescaped = std.ArrayList(u8).empty;
216+
defer unescaped.deinit(allocator);
213217

214-
try unescaped.ensureTotalCapacityPrecise(content_slice.len);
218+
try unescaped.ensureTotalCapacityPrecise(allocator, content_slice.len);
215219

216220
{
217221
var i: usize = 0;
@@ -220,7 +224,7 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
220224
i += 1;
221225

222226
if (c != '\\') {
223-
try unescaped.append(c);
227+
try unescaped.append(allocator, c);
224228
continue;
225229
}
226230

@@ -233,20 +237,20 @@ fn resolve_value(parser: *Parser, token_type: TokenType, text: []const u8) ![]co
233237
errdefer std.log.err("invalid escape sequence: \\{s}", .{[_]u8{esc_code}});
234238

235239
switch (esc_code) {
236-
'r' => try unescaped.append('\r'),
237-
'n' => try unescaped.append('\n'),
238-
't' => try unescaped.append('\t'),
239-
'\\' => try unescaped.append('\\'),
240-
'\"' => try unescaped.append('\"'),
241-
'\'' => try unescaped.append('\''),
242-
'e' => try unescaped.append('\x1B'),
240+
'r' => try unescaped.append(allocator, '\r'),
241+
'n' => try unescaped.append(allocator, '\n'),
242+
't' => try unescaped.append(allocator, '\t'),
243+
'\\' => try unescaped.append(allocator, '\\'),
244+
'\"' => try unescaped.append(allocator, '\"'),
245+
'\'' => try unescaped.append(allocator, '\''),
246+
'e' => try unescaped.append(allocator, '\x1B'),
243247

244248
else => return error.InvalidEscapeSequence,
245249
}
246250
}
247251
}
248252

249-
return try unescaped.toOwnedSlice();
253+
return try unescaped.toOwnedSlice(allocator);
250254
},
251255

252256
.comment, .directive, .whitespace => unreachable,

src/Tokenizer.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ test Tokenizer {
186186
var offset: u32 = 0;
187187
for (seq) |expected| {
188188
const actual = (try tokenizer.next()) orelse return error.Unexpected;
189-
errdefer std.debug.print("unexpected token: .{} \"{}\"\n", .{
189+
errdefer std.debug.print("unexpected token: .{f} \"{f}\"\n", .{
190190
std.zig.fmtId(@tagName(actual.type)),
191-
std.zig.fmtEscapes(tokenizer.source[actual.offset..][0..actual.len]),
191+
std.zig.fmtString(tokenizer.source[actual.offset..][0..actual.len]),
192192
});
193193
try std.testing.expectEqualStrings(expected.@"1", tokenizer.get_text(actual));
194194
try std.testing.expectEqual(offset, actual.offset);

src/components/FillData.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ pub fn parse(ctx: dim.Context) !dim.Content {
2020
}
2121

2222
fn render(self: *FillData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
23-
try stream.writer().writeByteNTimes(
23+
var writer = stream.writer();
24+
writer.interface.splatByteAll(
2425
self.fill_value,
2526
stream.length,
26-
);
27+
) catch return error.Overflow; // TODO FIX we don't know actually why this failed.
28+
// std.Io.Writer only returns error.WriteFailed.
2729
}

src/components/fs/FatFileSystem.zig

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ format_as: FatType,
1414
label: ?[]const u8 = null,
1515
fats: ?fatfs.FatTables = null,
1616
rootdir_size: ?c_uint = null,
17-
ops: std.ArrayList(common.FsOperation),
17+
ops: std.array_list.Managed(common.FsOperation),
1818
sector_align: ?c_uint = null,
1919
cluster_size: ?u32 = null,
2020

@@ -88,7 +88,7 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {
8888

8989
if (stream.length < min_size) {
9090
// TODO(fqu): Report fatal erro!
91-
std.log.err("cannot format {} bytes with {s}: min required size is {}", .{
91+
std.log.err("cannot format {f} bytes with {s}: min required size is {f}", .{
9292
@as(dim.DiskSize, @enumFromInt(stream.length)),
9393
@tagName(self.format_as),
9494
@as(dim.DiskSize, @enumFromInt(min_size)),
@@ -98,7 +98,7 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {
9898

9999
if (stream.length > max_size) {
100100
// TODO(fqu): Report warning
101-
std.log.warn("will not use all available space: available space is {}, but maximum size for {s} is {}", .{
101+
std.log.warn("will not use all available space: available space is {f}, but maximum size for {s} is {f}", .{
102102
@as(dim.DiskSize, @enumFromInt(stream.length)),
103103
@tagName(self.format_as),
104104
@as(dim.DiskSize, @enumFromInt(min_size)),
@@ -147,8 +147,8 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {
147147
return error.IoError;
148148
}
149149
} else {
150-
std.log.err("label \"{}\" is {} characters long, but only up to {} are permitted.", .{
151-
std.zig.fmtEscapes(label),
150+
std.log.err("label \"{f}\" is {} characters long, but only up to {} are permitted.", .{
151+
std.zig.fmtString(label),
152152
label.len,
153153
max_label_len,
154154
});
@@ -212,7 +212,7 @@ const AtomicOps = struct {
212212
};
213213
}
214214

215-
pub fn mkfile(ops: AtomicOps, path: []const u8, reader: anytype) dim.Content.RenderError!void {
215+
pub fn mkfile(ops: AtomicOps, path: []const u8, reader: *std.Io.Reader) dim.Content.RenderError!void {
216216
_ = ops;
217217

218218
var path_buffer: [max_path_len:0]u8 = undefined;
@@ -244,19 +244,28 @@ const AtomicOps = struct {
244244
};
245245
defer fs_file.close();
246246

247-
var fifo: std.fifo.LinearFifo(u8, .{ .Static = 8192 }) = .init();
248-
fifo.pump(
249-
reader,
250-
fs_file.writer(),
251-
) catch |err| switch (@as(dim.FileHandle.ReadError || fatfs.File.ReadError.Error, err)) {
252-
error.Overflow => return error.IoError,
253-
error.ReadFileFailed => return error.IoError,
254-
error.Timeout => @panic("implementation bug in fatfs glue"),
255-
error.DiskErr => return error.IoError,
256-
error.IntErr => return error.IoError,
257-
error.Denied => @panic("implementation bug in fatfs glue"),
258-
error.InvalidObject => @panic("implementation bug in fatfs glue"),
259-
};
247+
var writer_buf: [8192]u8 = undefined;
248+
var writer = fs_file.writer(&writer_buf);
249+
250+
_ = reader.streamRemaining(&writer.interface) catch return error.IoError;
251+
252+
writer.interface.flush() catch return error.IoError;
253+
254+
// TODO we've lost a lot of error specificity due to the use of the new APIs
255+
// See old code:
256+
257+
// fifo.pump(
258+
// reader,
259+
// fs_file.writer(),
260+
// ) catch |err| switch (@as(dim.FileHandle.ReadError || fatfs.File.ReadError.Error, err)) {
261+
// error.Overflow => return error.IoError,
262+
// error.ReadFileFailed => return error.IoError,
263+
// error.Timeout => @panic("implementation bug in fatfs glue"),
264+
// error.DiskErr => return error.IoError,
265+
// error.IntErr => return error.IoError,
266+
// error.Denied => @panic("implementation bug in fatfs glue"),
267+
// error.InvalidObject => @panic("implementation bug in fatfs glue"),
268+
// };
260269
}
261270
};
262271

0 commit comments

Comments
 (0)