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 {...@@ -35,15 +35,12 @@ const Fmt = struct {
35 gpa: Allocator,35 gpa: Allocator,
36 arena: Allocator,36 arena: Allocator,
37 out_buffer: std.Io.Writer.Allocating,37 out_buffer: std.Io.Writer.Allocating,
38 stdout_writer: *fs.File.Writer,
3839
39 const SeenMap = std.AutoHashMap(fs.File.INode, void);40 const SeenMap = std.AutoHashMap(fs.File.INode, void);
40};41};
4142
42pub fn run(43pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
43 gpa: Allocator,
44 arena: Allocator,
45 args: []const []const u8,
46) !void {
47 var color: Color = .auto;44 var color: Color = .auto;
48 var stdin_flag = false;45 var stdin_flag = false;
49 var check_flag = false;46 var check_flag = false;
...@@ -60,8 +57,7 @@ pub fn run(...@@ -60,8 +57,7 @@ pub fn run(
60 const arg = args[i];57 const arg = args[i];
61 if (mem.startsWith(u8, arg, "-")) {58 if (mem.startsWith(u8, arg, "-")) {
62 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {59 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
63 const stdout = std.fs.File.stdout().deprecatedWriter();60 try fs.File.stdout().writeAll(usage_fmt);
64 try stdout.writeAll(usage_fmt);
65 return process.cleanExit();61 return process.cleanExit();
66 } else if (mem.eql(u8, arg, "--color")) {62 } else if (mem.eql(u8, arg, "--color")) {
67 if (i + 1 >= args.len) {63 if (i + 1 >= args.len) {
...@@ -156,13 +152,16 @@ pub fn run(...@@ -156,13 +152,16 @@ pub fn run(
156 process.exit(code);152 process.exit(code);
157 }153 }
158154
159 return std.fs.File.stdout().writeAll(formatted);155 return fs.File.stdout().writeAll(formatted);
160 }156 }
161157
162 if (input_files.items.len == 0) {158 if (input_files.items.len == 0) {
163 fatal("expected at least one source file argument", .{});159 fatal("expected at least one source file argument", .{});
164 }160 }
165161
162 var stdout_buffer: [4096]u8 = undefined;
163 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
164
166 var fmt: Fmt = .{165 var fmt: Fmt = .{
167 .gpa = gpa,166 .gpa = gpa,
168 .arena = arena,167 .arena = arena,
...@@ -172,6 +171,7 @@ pub fn run(...@@ -172,6 +171,7 @@ pub fn run(
172 .force_zon = force_zon,171 .force_zon = force_zon,
173 .color = color,172 .color = color,
174 .out_buffer = .init(gpa),173 .out_buffer = .init(gpa),
174 .stdout_writer = &stdout_writer,
175 };175 };
176 defer fmt.seen.deinit();176 defer fmt.seen.deinit();
177 defer fmt.out_buffer.deinit();177 defer fmt.out_buffer.deinit();
...@@ -198,46 +198,10 @@ pub fn run(...@@ -198,46 +198,10 @@ pub fn run(
198 if (fmt.any_error) {198 if (fmt.any_error) {
199 process.exit(1);199 process.exit(1);
200 }200 }
201 try fmt.stdout_writer.interface.flush();
201}202}
202203
203const FmtError = error{204fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) !void {
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 {
241 fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) {205 fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) {
242 error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path),206 error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path),
243 else => {207 else => {
...@@ -254,7 +218,7 @@ fn fmtPathDir(...@@ -254,7 +218,7 @@ fn fmtPathDir(
254 check_mode: bool,218 check_mode: bool,
255 parent_dir: fs.Dir,219 parent_dir: fs.Dir,
256 parent_sub_path: []const u8,220 parent_sub_path: []const u8,
257) FmtError!void {221) !void {
258 var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true });222 var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true });
259 defer dir.close();223 defer dir.close();
260224
...@@ -290,7 +254,7 @@ fn fmtPathFile(...@@ -290,7 +254,7 @@ fn fmtPathFile(
290 check_mode: bool,254 check_mode: bool,
291 dir: fs.Dir,255 dir: fs.Dir,
292 sub_path: []const u8,256 sub_path: []const u8,
293) FmtError!void {257) !void {
294 const source_file = try dir.openFile(sub_path, .{});258 const source_file = try dir.openFile(sub_path, .{});
295 var file_closed = false;259 var file_closed = false;
296 errdefer if (!file_closed) source_file.close();260 errdefer if (!file_closed) source_file.close();
...@@ -381,8 +345,7 @@ fn fmtPathFile(...@@ -381,8 +345,7 @@ fn fmtPathFile(
381 return;345 return;
382346
383 if (check_mode) {347 if (check_mode) {
384 const stdout = std.fs.File.stdout().deprecatedWriter();348 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
385 try stdout.print("{s}\n", .{file_path});
386 fmt.any_error = true;349 fmt.any_error = true;
387 } else {350 } else {
388 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });351 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
...@@ -390,8 +353,7 @@ fn fmtPathFile(...@@ -390,8 +353,7 @@ fn fmtPathFile(
390353
391 try af.file.writeAll(fmt.out_buffer.getWritten());354 try af.file.writeAll(fmt.out_buffer.getWritten());
392 try af.finish();355 try af.finish();
393 const stdout = std.fs.File.stdout().deprecatedWriter();356 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
394 try stdout.print("{s}\n", .{file_path});
395 }357 }
396}358}
397359