lib/zen/src/site/path/capacity.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2
3 const plan = @import("plan.zig");
4
5 pub const Limits = plan.Limits;
6 pub const DeriveError = error{CapacityOverflow};
7
8 pub const Capacity = struct {
9 limits: Limits,
10 target_path_offset: usize,
11 public_path_offset: usize,
12 storage_bytes: usize,
13
14 pub fn derive(limits: Limits) DeriveError!Capacity {
15 return .{
16 .limits = limits,
17 .target_path_offset = 0,
18 .public_path_offset = limits.max_target_path_bytes,
19 .storage_bytes = try added(limits.max_target_path_bytes, limits.max_public_path_bytes),
20 };
21 }
22 };
23
24 fn added(left: usize, right: usize) DeriveError!usize {
25 return std.math.add(usize, left, right) catch error.CapacityOverflow;
26 }
27
28 fn independent(limits: Limits) DeriveError!Capacity {
29 const storage_bytes = @as(u128, limits.max_target_path_bytes) + limits.max_public_path_bytes;
30 if (storage_bytes > std.math.maxInt(usize)) return error.CapacityOverflow;
31 return .{
32 .limits = limits,
33 .target_path_offset = 0,
34 .public_path_offset = limits.max_target_path_bytes,
35 .storage_bytes = @intCast(storage_bytes),
36 };
37 }
38
39 test "site path capacity matches an independent byte model" {
40 comptime {
41 @stardustClaim(
42 @import("alloc_phase").capacity.witness(@import("./root.zig").Storage, "zen_site_path_capacity"),
43 null,
44 null,
45 null,
46 null,
47 null,
48 null,
49 );
50 }
51
52 const limits = Limits{
53 .max_target_path_bytes = 83,
54 .max_public_path_bytes = 84,
55 };
56 const capacity = try Capacity.derive(limits);
57 try std.testing.expectEqual(try independent(limits), capacity);
58 try std.testing.expectEqual(@as(usize, 167), capacity.storage_bytes);
59 }
60
61 test "site path capacity handles zero and rejects overflow" {
62 try std.testing.expectEqual(@as(usize, 0), (try Capacity.derive(.{
63 .max_target_path_bytes = 0,
64 .max_public_path_bytes = 0,
65 })).storage_bytes);
66 try std.testing.expectError(error.CapacityOverflow, Capacity.derive(.{
67 .max_target_path_bytes = std.math.maxInt(usize),
68 .max_public_path_bytes = 1,
69 }));
70 }