@@ -11,7 +11,7 @@ const max_script_size = 10 * DiskSize.MiB;
11
11
12
12
const Options = struct {
13
13
output : ? []const u8 = null ,
14
- size : ? DiskSize = null ,
14
+ size : DiskSize = DiskSize . empty ,
15
15
script : ? []const u8 = null ,
16
16
@"import-env" : bool = false ,
17
17
};
@@ -79,6 +79,11 @@ pub fn main() !u8 {
79
79
if (bad_args )
80
80
return 1 ;
81
81
82
+ const size_limit : u64 = options .size .size_in_bytes ();
83
+ if (size_limit == 0 ) {
84
+ return fatal ("--size must be given!" );
85
+ }
86
+
82
87
var current_dir = try std .fs .cwd ().openDir ("." , .{});
83
88
defer current_dir .close ();
84
89
@@ -122,21 +127,14 @@ pub fn main() !u8 {
122
127
return 1 ;
123
128
}
124
129
125
- const root_size_estimation = try root_content .guess_required_size ();
126
- std .log .info ("root size: {}" , .{root_size_estimation });
127
-
128
130
{
129
131
var output_file = try current_dir .atomicFile (output_path , .{});
130
132
defer output_file .deinit ();
131
133
132
- const size_limit : ? u64 = if (options .size ) | disk_size | blk : {
133
- try output_file .file .setEndPos (disk_size .size_in_bytes ());
134
-
135
- break :blk disk_size .size_in_bytes ();
136
- } else null ;
134
+ try output_file .file .setEndPos (size_limit );
137
135
138
136
var stream = BinaryStream {
139
- .capacity = size_limit orelse 0 ,
137
+ .capacity = size_limit ,
140
138
};
141
139
142
140
try root_content .render (& stream );
@@ -346,23 +344,7 @@ pub const Content = struct {
346
344
obj : * anyopaque ,
347
345
vtable : * const VTable ,
348
346
349
- pub const empty : Content = .{
350
- .obj = undefined ,
351
- .vtable = & emptyVTable ,
352
- };
353
-
354
- const emptyVTable : VTable = blk : {
355
- const Wrap = struct {
356
- fn render (_ : * anyopaque , _ : * BinaryStream ) RenderError ! void {}
357
- fn guess_size_fn (_ : * anyopaque ) GuessError ! SizeGuess {
358
- return .{ .exact = 0 };
359
- }
360
- };
361
- break :blk .{
362
- .render_fn = Wrap .render ,
363
- .guess_size_fn = Wrap .guess_size_fn ,
364
- };
365
- };
347
+ pub const empty : Content = @import ("components/EmptyData.zig" ).parse (undefined ) catch unreachable ;
366
348
367
349
pub fn create_handle (obj : * anyopaque , vtable : * const VTable ) Content {
368
350
return .{ .obj = obj , .vtable = vtable };
@@ -373,22 +355,13 @@ pub const Content = struct {
373
355
try content .vtable .render_fn (content .obj , stream );
374
356
}
375
357
376
- /// Attempts to determine the required size of the content.
377
- ///
378
- /// This may not be an exact guess, so the result can have
379
- pub fn guess_required_size (content : Content ) GuessError ! SizeGuess {
380
- return try content .vtable .guess_size_fn (content .obj );
381
- }
382
-
383
358
pub const VTable = struct {
384
359
render_fn : * const fn (* anyopaque , * BinaryStream ) RenderError ! void ,
385
- guess_size_fn : * const fn (* anyopaque ) GuessError ! SizeGuess ,
386
360
387
361
pub fn create (
388
362
comptime Container : type ,
389
363
comptime funcs : struct {
390
364
render_fn : * const fn (* Container , * BinaryStream ) RenderError ! void ,
391
- guess_size_fn : * const fn (* Container ) GuessError ! SizeGuess ,
392
365
},
393
366
) * const VTable {
394
367
const Wrap = struct {
@@ -398,15 +371,9 @@ pub const Content = struct {
398
371
stream ,
399
372
);
400
373
}
401
- fn guess_size (self : * anyopaque ) GuessError ! SizeGuess {
402
- return funcs .guess_size_fn (
403
- @ptrCast (@alignCast (self )),
404
- );
405
- }
406
374
};
407
375
return comptime &.{
408
376
.render_fn = Wrap .render ,
409
- .guess_size_fn = Wrap .guess_size ,
410
377
};
411
378
}
412
379
};
@@ -541,12 +508,6 @@ pub const FileHandle = struct {
541
508
}
542
509
};
543
510
544
- pub const SizeGuess = union (enum ) {
545
- unknown ,
546
- exact : u64 ,
547
- at_least : u64 ,
548
- };
549
-
550
511
pub const BinaryStream = struct {
551
512
pub const WriteError = error {IoError };
552
513
pub const Writer = std .io .Writer (* BinaryStream , WriteError , write_some );
@@ -580,6 +541,8 @@ const DiskSize = enum(u64) {
580
541
const MiB = 1024 * 1024 ;
581
542
const GiB = 1024 * 1024 * 1024 ;
582
543
544
+ pub const empty : DiskSize = @enumFromInt (0 );
545
+
583
546
_ ,
584
547
585
548
pub fn parse (str : []const u8 ) error { InvalidSize , Overflow }! DiskSize {
0 commit comments