authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-16 14:58:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-16 17:20:03-07:00
logad726587cc66b1ae07a463e1d591362130090170
tree4570e78198717f0fdf2a2b8c5add0345a8909d6e
parentaac301a655d63c239d39e3cd5a73154e091be703

zig fmt: update to new I/O API


1 files changed, 14 insertions(+), 52 deletions(-)

src/fmt.zig+14-52
......@@ -35,15 +35,12 @@ const Fmt = struct {
3535 gpa: Allocator,
3636 arena: Allocator,
3737 out_buffer: std.Io.Writer.Allocating,
38 stdout_writer: *fs.File.Writer,
3839
3940 const SeenMap = std.AutoHashMap(fs.File.INode, void);
4041};
4142
42pub fn run(
43 gpa: Allocator,
44 arena: Allocator,
45 args: []const []const u8,
46) !void {
43pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4744 var color: Color = .auto;
4845 var stdin_flag = false;
4946 var check_flag = false;
......@@ -60,8 +57,7 @@ pub fn run(
6057 const arg = args[i];
6158 if (mem.startsWith(u8, arg, "-")) {
6259 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
63 const stdout = std.fs.File.stdout().deprecatedWriter();
64 try stdout.writeAll(usage_fmt);
60 try fs.File.stdout().writeAll(usage_fmt);
6561 return process.cleanExit();
6662 } else if (mem.eql(u8, arg, "--color")) {
6763 if (i + 1 >= args.len) {
......@@ -156,13 +152,16 @@ pub fn run(
156152 process.exit(code);
157153 }
158154
159 return std.fs.File.stdout().writeAll(formatted);
155 return fs.File.stdout().writeAll(formatted);
160156 }
161157
162158 if (input_files.items.len == 0) {
163159 fatal("expected at least one source file argument", .{});
164160 }
165161
162 var stdout_buffer: [4096]u8 = undefined;
163 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
164
166165 var fmt: Fmt = .{
167166 .gpa = gpa,
168167 .arena = arena,
......@@ -172,6 +171,7 @@ pub fn run(
172171 .force_zon = force_zon,
173172 .color = color,
174173 .out_buffer = .init(gpa),
174 .stdout_writer = &stdout_writer,
175175 };
176176 defer fmt.seen.deinit();
177177 defer fmt.out_buffer.deinit();
......@@ -198,46 +198,10 @@ pub fn run(
198198 if (fmt.any_error) {
199199 process.exit(1);
200200 }
201 try fmt.stdout_writer.interface.flush();
201202}
202203
203const FmtError = error{
204 SystemResources,
205 OperationAborted,
206 IoPending,
207 BrokenPipe,
208 Unexpected,
209 WouldBlock,
210 Canceled,
211 FileClosed,
212 DestinationAddressRequired,
213 DiskQuota,
214 FileTooBig,
215 MessageTooBig,
216 InputOutput,
217 NoSpaceLeft,
218 AccessDenied,
219 OutOfMemory,
220 RenameAcrossMountPoints,
221 ReadOnlyFileSystem,
222 LinkQuotaExceeded,
223 FileBusy,
224 EndOfStream,
225 Unseekable,
226 NotOpenForWriting,
227 UnsupportedEncoding,
228 InvalidEncoding,
229 ConnectionResetByPeer,
230 SocketNotConnected,
231 LockViolation,
232 NetNameDeleted,
233 InvalidArgument,
234 ProcessNotFound,
235 ConnectionTimedOut,
236 NotOpenForReading,
237 StreamTooLong,
238} || fs.File.OpenError;
239
240fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {
204fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) !void {
241205 fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) {
242206 error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path),
243207 else => {
......@@ -254,7 +218,7 @@ fn fmtPathDir(
254218 check_mode: bool,
255219 parent_dir: fs.Dir,
256220 parent_sub_path: []const u8,
257) FmtError!void {
221) !void {
258222 var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true });
259223 defer dir.close();
260224
......@@ -290,7 +254,7 @@ fn fmtPathFile(
290254 check_mode: bool,
291255 dir: fs.Dir,
292256 sub_path: []const u8,
293) FmtError!void {
257) !void {
294258 const source_file = try dir.openFile(sub_path, .{});
295259 var file_closed = false;
296260 errdefer if (!file_closed) source_file.close();
......@@ -381,8 +345,7 @@ fn fmtPathFile(
381345 return;
382346
383347 if (check_mode) {
384 const stdout = std.fs.File.stdout().deprecatedWriter();
385 try stdout.print("{s}\n", .{file_path});
348 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
386349 fmt.any_error = true;
387350 } else {
388351 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
......@@ -390,8 +353,7 @@ fn fmtPathFile(
390353
391354 try af.file.writeAll(fmt.out_buffer.getWritten());
392355 try af.finish();
393 const stdout = std.fs.File.stdout().deprecatedWriter();
394 try stdout.print("{s}\n", .{file_path});
356 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
395357 }
396358}
397359