From d4e7694b4fedcf532f469095fb07df60f134e9d2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:40:11 -0300 Subject: idioms #12, Query and Stream: Stream.query returns OutOfMemory for Query to report, walkDir's anyerror is the three errors it has, Query's rows say their counts through buffers proven big enough, its open, produce, noteRows, fillList and land report an allocation that failed (reportError) instead of returning quietly, tagPrefix names its set, and std.fs.path is std.Io.Dir.path Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Stream.zig | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/Stream.zig') diff --git a/src/Stream.zig b/src/Stream.zig index 6a7dc324..2641fc73 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -491,16 +491,16 @@ pub fn cancelTask(s: *Stream) void { /// listing of the files (`.files`), under `dir`. The generation before it /// is over (its worker canceled, its batches dropped when they come); /// `typed`, it starts once the typing pauses, else at the next step. -pub fn query(p: *Pardes, target: u32, kind: Kind, pattern: []const u8, dir: []const u8, how: enum { typed, now }) void { +pub fn query(p: *Pardes, target: u32, kind: Kind, pattern: []const u8, dir: []const u8, how: enum { typed, now }) error{OutOfMemory}!void { const s = &p.stream; s.cancelTask(); s.gen +%= 1; s.kind = kind; s.target = target; s.pattern.clearRetainingCapacity(); - s.pattern.appendSlice(p.gpa, pattern) catch return; + try s.pattern.appendSlice(p.gpa, pattern); s.dir.clearRetainingCapacity(); - s.dir.appendSlice(p.gpa, dir) catch return; + try s.dir.appendSlice(p.gpa, dir); s.skipped = 0; s.state = .waiting; s.due_ns = if (how == .typed) p.clock.now_ns + debounce_ns else p.clock.now_ns; @@ -858,7 +858,7 @@ const Worker = struct { } /// Whether the cap ended it (on the loop). - fn walkDir(w: *Worker, dir: std.Io.Dir, prefix: []const u8, depth: usize) anyerror!bool { + fn walkDir(w: *Worker, dir: std.Io.Dir, prefix: []const u8, depth: usize) error{ Canceled, OutOfMemory, Refused }!bool { try w.io.checkCancel(); var arena: std.heap.ArenaAllocator = .init(w.gpa); defer arena.deinit(); @@ -991,7 +991,7 @@ const Worker = struct { if (w.job.kind == .files) { // a dot-file is passed over, as the walk passes over // dot-directories - if (std.mem.startsWith(u8, std.fs.path.basename(line.items), ".")) continue; + if (std.mem.startsWith(u8, std.Io.Dir.path.basename(line.items), ".")) continue; if (try w.file(w.shown(line.items))) { cut = true; break :read; @@ -1170,7 +1170,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw var name_buf: [64]u8 = undefined; for (0..files) |i| { const name = try std.fmt.bufPrint(&name_buf, "d{d:0>2}/f{d:0>3}.txt", .{ i / 10, i }); - try tmp.dir.createDirPath(testing.io, std.fs.path.dirname(name).?); + try tmp.dir.createDirPath(testing.io, std.Io.Dir.path.dirname(name).?); try tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = "a needle line\nsome hay here\nneedle again\nhay hay\nmore hay\n" }); } const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; @@ -1178,7 +1178,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw defer p.deinit(); const pane = try p.setTestFile("x\n"); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); shell.attach(p); @@ -1204,17 +1204,17 @@ test "Stream: a stale generation's rows never land in the query pane, however tw const b = if (round % 2 == 0) "hay" else "needle"; q.found.clearRetainingCapacity(); q.found_bytes.clearRetainingCapacity(); - query(p, qp.serial, .grep, a, root, .now); + try query(p, qp.serial, .grep, a, root, .now); p.update(.tick); for (0..round % 3) |_| shell.pump(p); var rogue: Rogue = .{ .q = &shell.q, .gen = p.stream.gen }; q.found.clearRetainingCapacity(); q.found_bytes.clearRetainingCapacity(); if (round % 4 == 3) { - query(p, qp.serial, .grep, b, root, .typed); + try query(p, qp.serial, .grep, b, root, .typed); try testing.expectEqual(State.waiting, p.stream.state); p.advance(p.clock.now_ns + debounce_ns + std.time.ns_per_ms); - } else query(p, qp.serial, .grep, b, root, .now); + } else try query(p, qp.serial, .grep, b, root, .now); try testing.expect(p.stream.gen != rogue.gen); const thread = try std.Thread.spawn(.{}, Rogue.run, .{&rogue}); const finished = shell.finish(p, 20_000); @@ -1255,7 +1255,7 @@ test "Stream: the listing of files for Find on a worker, a cap of hits stops the defer p.deinit(); const pane = try p.setTestFile("x\n"); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); p.stream.engine = .walk; // No host workers: the listing runs on the loop at the next tick. @@ -1368,7 +1368,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's defer p.deinit(); const pane = try p.setTestFile("x\n"); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); shell.attach(p); @@ -1418,7 +1418,7 @@ test "Stream: a Grep's rows recoloured are made again, never written, and a dump defer p.deinit(); const pane = try p.setTestFile(text.items); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); shell.attach(p); defer { @@ -1488,7 +1488,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them, // the file open in the pane that asks: its text painted, not the disk's const pane = try p.setTestFile(text); p.gpa.free(pane.file.?.path); - pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + pane.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); shell.attach(p); defer { -- cgit v1.3