11const builtin = @import ("builtin" );
22const std = @import ("std" );
3+ const Io = std .Io ;
4+ const Allocator = std .mem .Allocator ;
35const super = @import ("superhtml" );
46
57const FileType = enum { html , super };
68
7- pub fn run (gpa : std.mem. Allocator , args : []const []const u8 ) ! noreturn {
9+ pub fn run (io : Io , gpa : Allocator , args : []const []const u8 ) ! noreturn {
810 const cmd = Command .parse (args );
911 var any_error = false ;
1012 switch (cmd .mode ) {
1113 .stdin = > {
12- var fr = std .fs .File .stdin ().reader (&.{});
14+ var fr = std .Io .File .stdin ().reader (io , &.{});
1315 var aw : std.Io.Writer.Allocating = .init (gpa );
1416 _ = try fr .interface .streamRemaining (& aw .writer );
1517 const in_bytes = try aw .toOwnedSliceSentinel (0 );
1618
17- try checkHtml (gpa , null , in_bytes , cmd .syntax_only );
19+ try checkHtml (io , gpa , null , in_bytes , cmd .syntax_only );
1820 },
1921 .stdin_super = > {
20- var fr = std .fs .File .stdin ().reader (&.{});
22+ var fr = std .Io .File .stdin ().reader (io , &.{});
2123 var aw : std.Io.Writer.Allocating = .init (gpa );
2224 _ = try fr .interface .streamRemaining (& aw .writer );
2325 const in_bytes = try aw .toOwnedSliceSentinel (0 );
2426
25- try checkSuper (gpa , null , in_bytes , cmd .syntax_only );
27+ try checkSuper (io , gpa , null , in_bytes , cmd .syntax_only );
2628 },
2729 .paths = > | paths | {
2830 // checkFile will reset the arena at the end of each call
2931 var arena_impl = std .heap .ArenaAllocator .init (gpa );
3032 for (paths ) | path | {
3133 checkFile (
34+ io ,
3235 & arena_impl ,
33- std . fs .cwd (),
36+ Io . Dir .cwd (),
3437 path ,
3538 path ,
3639 & any_error ,
3740 cmd .syntax_only ,
3841 ) catch | err | switch (err ) {
3942 error .IsDir , error .AccessDenied = > {
4043 checkDir (
44+ io ,
4145 gpa ,
4246 & arena_impl ,
4347 path ,
@@ -69,20 +73,22 @@ pub fn run(gpa: std.mem.Allocator, args: []const []const u8) !noreturn {
6973}
7074
7175fn checkDir (
76+ io : Io ,
7277 gpa : std.mem.Allocator ,
7378 arena_impl : * std.heap.ArenaAllocator ,
7479 path : []const u8 ,
7580 any_error : * bool ,
7681 syntax_only : bool ,
7782) ! void {
78- var dir = try std . fs .cwd ().openDir (path , .{ .iterate = true });
79- defer dir .close ();
83+ var dir = try Io . Dir .cwd ().openDir (io , path , .{ .iterate = true });
84+ defer dir .close (io );
8085 var walker = dir .walk (gpa ) catch oom ();
8186 defer walker .deinit ();
82- while (try walker .next ()) | item | {
87+ while (try walker .next (io )) | item | {
8388 switch (item .kind ) {
8489 .file = > {
8590 try checkFile (
91+ io ,
8692 arena_impl ,
8793 item .dir ,
8894 item .basename ,
@@ -97,8 +103,9 @@ fn checkDir(
97103}
98104
99105fn checkFile (
106+ io : Io ,
100107 arena_impl : * std.heap.ArenaAllocator ,
101- base_dir : std.fs .Dir ,
108+ base_dir : Io .Dir ,
102109 sub_path : []const u8 ,
103110 full_path : []const u8 ,
104111 any_error : * bool ,
@@ -108,14 +115,8 @@ fn checkFile(
108115 defer _ = arena_impl .reset (.retain_capacity );
109116 const arena = arena_impl .allocator ();
110117
111- const in_bytes = if (builtin .zig_version .minor == 15 ) try base_dir .readFileAllocOptions (
112- arena ,
113- sub_path ,
114- super .max_size ,
115- null ,
116- .of (u8 ),
117- 0 ,
118- ) else try base_dir .readFileAllocOptions (
118+ const in_bytes = try base_dir .readFileAllocOptions (
119+ io ,
119120 sub_path ,
120121 arena ,
121122 .limited (super .max_size ),
@@ -139,12 +140,14 @@ fn checkFile(
139140
140141 switch (file_type ) {
141142 .html = > try checkHtml (
143+ io ,
142144 arena ,
143145 full_path ,
144146 in_bytes ,
145147 syntax_only ,
146148 ),
147149 .super = > try checkSuper (
150+ io ,
148151 arena ,
149152 full_path ,
150153 in_bytes ,
@@ -154,35 +157,37 @@ fn checkFile(
154157}
155158
156159pub fn checkHtml (
160+ io : Io ,
157161 arena : std.mem.Allocator ,
158162 path : ? []const u8 ,
159163 code : [:0 ]const u8 ,
160164 syntax_only : bool ,
161165) ! void {
162166 const ast = try super .html .Ast .init (arena , code , .html , syntax_only );
163167 if (ast .errors .len > 0 ) {
164- var stderr = std . fs . File .stderr ().writer (&.{});
168+ var stderr = Io . File .stderr ().writer (io , &.{});
165169 try ast .printErrors (code , path , & stderr .interface );
166170 std .process .exit (1 );
167171 }
168172}
169173
170174fn checkSuper (
175+ io : Io ,
171176 arena : std.mem.Allocator ,
172177 path : ? []const u8 ,
173178 code : [:0 ]const u8 ,
174179 syntax_only : bool ,
175180) ! void {
176181 const html = try super .html .Ast .init (arena , code , .superhtml , syntax_only );
177182 if (html .errors .len > 0 ) {
178- var stderr = std . fs . File .stderr ().writer (&.{});
183+ var stderr = Io . File .stderr ().writer (io , &.{});
179184 try html .printErrors (code , path , & stderr .interface );
180185 std .process .exit (1 );
181186 }
182187
183188 const s = try super .Ast .init (arena , html , code );
184189 if (s .errors .len > 0 ) {
185- var stderr = std . fs . File .stderr ().writer (&.{});
190+ var stderr = Io . File .stderr ().writer (io , &.{});
186191 try s .printErrors (code , path , & stderr .interface );
187192 std .process .exit (1 );
188193 }
0 commit comments