@@ -3,49 +3,49 @@ const super = @import("superhtml");
33
44const FileType = enum { html , super };
55
6- pub fn run (gpa : std.mem.Allocator , args : []const []const u8 ) ! void {
6+ pub fn run (gpa : std.mem.Allocator , args : []const []const u8 ) ! noreturn {
77 const cmd = Command .parse (args );
8- var any_error = false ;
8+
9+ var errors = false ;
910 switch (cmd .mode ) {
1011 .stdin = > {
1112 var fr = std .fs .File .stdin ().reader (&.{});
1213 var aw : std.Io.Writer.Allocating = .init (gpa );
1314 _ = try fr .interface .streamRemaining (& aw .writer );
1415 const in_bytes = try aw .toOwnedSliceSentinel (0 );
1516
16- const out_bytes = try fmtHtml (gpa , null , in_bytes , cmd .strict );
17-
18- try std . fs . File . stdout (). writeAll ( out_bytes ) ;
17+ const result = try fmtHtml (gpa , null , in_bytes , cmd .strict );
18+ try std . fs . File . stdout (). writeAll ( result . bytes );
19+ errors |= result . errors ;
1920 },
2021 .stdin_super = > {
2122 var fr = std .fs .File .stdin ().reader (&.{});
2223 var aw : std.Io.Writer.Allocating = .init (gpa );
2324 _ = try fr .interface .streamRemaining (& aw .writer );
2425 const in_bytes = try aw .toOwnedSliceSentinel (0 );
2526
26- const out_bytes = try fmtSuper (gpa , null , in_bytes , cmd .strict );
27- try std .fs .File .stdout ().writeAll (out_bytes );
27+ const result = try fmtSuper (gpa , null , in_bytes , cmd .strict );
28+ try std .fs .File .stdout ().writeAll (result .bytes );
29+ errors |= result .errors ;
2830 },
2931 .paths = > | paths | {
3032 // checkFile will reset the arena at the end of each call
3133 var arena_impl = std .heap .ArenaAllocator .init (gpa );
3234 for (paths ) | path | {
33- formatFile (
35+ errors |= formatFile (
3436 & arena_impl ,
3537 cmd .check ,
3638 std .fs .cwd (),
3739 path ,
3840 path ,
39- & any_error ,
4041 cmd .strict ,
4142 ) catch | err | switch (err ) {
4243 error .IsDir , error .AccessDenied = > {
43- formatDir (
44+ errors |= formatDir (
4445 gpa ,
4546 & arena_impl ,
4647 cmd .check ,
4748 path ,
48- & any_error ,
4949 cmd .strict ,
5050 ) catch | dir_err | {
5151 std .debug .print ("Error walking dir '{s}': {s}\n " , .{
@@ -54,6 +54,7 @@ pub fn run(gpa: std.mem.Allocator, args: []const []const u8) !void {
5454 });
5555 std .process .exit (1 );
5656 };
57+ continue ;
5758 },
5859 else = > {
5960 std .debug .print ("Error while accessing '{s}': {s}\n " , .{
@@ -66,39 +67,38 @@ pub fn run(gpa: std.mem.Allocator, args: []const []const u8) !void {
6667 },
6768 }
6869
69- if (any_error ) {
70- std .process .exit (1 );
71- }
70+ std .process .exit (@intFromBool (errors ));
7271}
7372
7473fn formatDir (
7574 gpa : std.mem.Allocator ,
7675 arena_impl : * std.heap.ArenaAllocator ,
7776 check : bool ,
7877 path : []const u8 ,
79- any_error : * bool ,
8078 strict : bool ,
81- ) ! void {
79+ ) ! bool {
8280 var dir = try std .fs .cwd ().openDir (path , .{ .iterate = true });
8381 defer dir .close ();
8482 var walker = dir .walk (gpa ) catch oom ();
8583 defer walker .deinit ();
84+ var errors = false ;
8685 while (try walker .next ()) | item | {
8786 switch (item .kind ) {
8887 .file = > {
89- try formatFile (
88+ errors |= try formatFile (
9089 arena_impl ,
9190 check ,
9291 item .dir ,
9392 item .basename ,
9493 item .path ,
95- any_error ,
9694 strict ,
9795 );
9896 },
9997 else = > {},
10098 }
10199 }
100+
101+ return errors ;
102102}
103103
104104fn formatFile (
@@ -107,9 +107,8 @@ fn formatFile(
107107 base_dir : std.fs.Dir ,
108108 sub_path : []const u8 ,
109109 full_path : []const u8 ,
110- any_error : * bool ,
111110 strict : bool ,
112- ) ! void {
111+ ) ! bool {
113112 defer _ = arena_impl .reset (.retain_capacity );
114113 const arena = arena_impl .allocator ();
115114
@@ -132,10 +131,10 @@ fn formatFile(
132131 if (std .mem .eql (u8 , ext , ".shtml" )) {
133132 break :blk .super ;
134133 }
135- return ;
134+ return false ;
136135 };
137136
138- const out_bytes = switch (file_type ) {
137+ const result = switch (file_type ) {
139138 .html = > try fmtHtml (
140139 arena ,
141140 full_path ,
@@ -150,54 +149,64 @@ fn formatFile(
150149 ),
151150 };
152151
153- if (std .mem .eql (u8 , out_bytes , in_bytes )) return ;
152+ if (std .mem .eql (u8 , result . bytes , in_bytes )) return result . errors ;
154153
155154 var stdout_writer = std .fs .File .stdout ().writer (&.{});
156155 const stdout = & stdout_writer .interface ;
157156 if (check ) {
158- any_error .* = true ;
159157 try stdout .print ("{s}\n " , .{full_path });
160- return ;
158+ return result . errors ;
161159 }
162160
163161 var af = try base_dir .atomicFile (sub_path , .{ .write_buffer = &.{} });
164162 defer af .deinit ();
165163
166- try af .file_writer .interface .writeAll (out_bytes );
164+ try af .file_writer .interface .writeAll (result . bytes );
167165 try af .finish ();
168166 try stdout .print ("{s}\n " , .{full_path });
167+ return result .errors ;
169168}
170169
170+ const Result = struct {
171+ bytes : []const u8 ,
172+ errors : bool ,
173+ };
174+
171175pub fn fmtHtml (
172176 arena : std.mem.Allocator ,
173177 path : ? []const u8 ,
174178 code : [:0 ]const u8 ,
175179 strict : bool ,
176- ) ! [] const u8 {
180+ ) ! Result {
177181 const ast = try super .html .Ast .init (arena , code , .html , strict );
178182 if (ast .errors .len > 0 ) {
179183 var ew = std .fs .File .stderr ().writer (&.{});
180184 try ast .printErrors (code , path , & ew .interface );
181- std .process .exit (1 );
185+ if ( ast . has_syntax_errors ) std .process .exit (1 );
182186 }
183187
184- return std .fmt .allocPrint (arena , "{f}" , .{ast .formatter (code )});
188+ return .{
189+ .bytes = try std .fmt .allocPrint (arena , "{f}" , .{ast .formatter (code )}),
190+ .errors = ast .errors .len > 0 ,
191+ };
185192}
186193
187194fn fmtSuper (
188195 arena : std.mem.Allocator ,
189196 path : ? []const u8 ,
190197 code : [:0 ]const u8 ,
191198 strict : bool ,
192- ) ! [] const u8 {
199+ ) ! Result {
193200 const ast = try super .html .Ast .init (arena , code , .superhtml , strict );
194201 if (ast .errors .len > 0 ) {
195202 var ew = std .fs .File .stderr ().writer (&.{});
196203 try ast .printErrors (code , path , & ew .interface );
197- std .process .exit (1 );
198204 }
199205
200- return std .fmt .allocPrint (arena , "{f}" , .{ast .formatter (code )});
206+ return .{
207+ .bytes = try std .fmt .allocPrint (arena , "{f}" , .{ast .formatter (code )}),
208+ .errors = ast .errors .len > 0 ,
209+ };
201210}
202211
203212fn oom () noreturn {
0 commit comments