Skip to content

Commit b41a102

Browse files
committed
Update for latest Zig changes
In particular, the removal of openIterableDir: 519ba9bb654a4d5caf7440160f018b7a3ae1e95a and the removal of std.meta.trait: ziglang/zig#18061
1 parent 48e0920 commit b41a102

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/Template.zig

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -448,22 +448,21 @@ pub const Value = union(enum) {
448448
bool => return .{ .bool = object },
449449
else => {},
450450
}
451-
if (comptime std.meta.trait.isZigString(Type))
452-
return .{ .string = object };
453-
if (comptime std.meta.trait.isTuple(Type)) {
454-
var array = std.ArrayListUnmanaged(Value){};
455-
inline for (object) |item| try array.append(allocator, try init(allocator, item));
456-
return .{ .array = array };
457-
}
458-
if (comptime std.meta.trait.isIndexable(Type)) {
459-
var array = std.ArrayListUnmanaged(Value){};
460-
for (object) |item| try array.append(allocator, try init(allocator, item));
461-
return .{ .array = array };
462-
}
463451
switch (@typeInfo(Type)) {
464-
.Struct => |the_struct| {
452+
.Array => |array_type| return initArray(allocator, object, array_type.child),
453+
.Pointer => |pointer_type| return if (pointer_type.size == .Slice)
454+
initArray(allocator, object, pointer_type.child)
455+
else switch (@typeInfo(pointer_type.child)) {
456+
.Array => |array_type| initArray(allocator, object, array_type.child),
457+
else => @compileError("invalid pointer type: " ++ @typeName(Type)),
458+
},
459+
.Struct => |struct_type| if (struct_type.is_tuple) {
460+
var array = std.ArrayListUnmanaged(Value){};
461+
inline for (object) |item| try array.append(allocator, try init(allocator, item));
462+
return .{ .array = array };
463+
} else {
465464
var dict = std.StringHashMapUnmanaged(Value){};
466-
inline for (the_struct.fields) |field| {
465+
inline for (struct_type.fields) |field| {
467466
const field_value = try init(allocator, @field(object, field.name));
468467
try dict.put(allocator, field.name, field_value);
469468
}
@@ -472,6 +471,13 @@ pub const Value = union(enum) {
472471
else => @compileError("invalid type: " ++ @typeName(Type)),
473472
}
474473
}
474+
475+
fn initArray(allocator: Allocator, object: anytype, comptime ItemType: type) !Value {
476+
if (ItemType == u8) return .{ .string = object };
477+
var array = std.ArrayListUnmanaged(Value){};
478+
for (object) |item| try array.append(allocator, try init(allocator, item));
479+
return .{ .array = array };
480+
}
475481
};
476482

477483
test "value" {

src/main.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ fn parseEnvironment() Environment {
116116

117117
fn readPosts(allocator: Allocator, reporter: *Reporter, include_drafts: bool) ![]const Post {
118118
var posts = std.ArrayList(Post).init(allocator);
119-
var iterable = try fs.cwd().openIterableDir(constants.post_dir, .{});
120-
defer iterable.close();
121-
var iter = iterable.iterate();
119+
var dir = try fs.cwd().openDir(constants.post_dir, .{});
120+
defer dir.close();
121+
var iter = dir.iterate();
122122
while (try iter.next()) |entry| {
123123
if (entry.name[0] == '.') continue;
124124
var scanner = Scanner{
125-
.source = try iterable.dir.readFileAlloc(allocator, entry.name, constants.max_file_size),
125+
.source = try dir.readFileAlloc(allocator, entry.name, constants.max_file_size),
126126
.filename = try fs.path.join(allocator, &.{ constants.post_dir, entry.name }),
127127
.reporter = reporter,
128128
};
@@ -140,10 +140,10 @@ fn cmpPostsReverseChronological(_: void, lhs: Post, rhs: Post) bool {
140140

141141
fn readTemplates(allocator: Allocator, reporter: *Reporter) !std.StringHashMap(Template) {
142142
var templates = std.StringHashMap(Template).init(allocator);
143-
var iterable = try fs.cwd().openIterableDir(constants.template_dir, .{});
144-
defer iterable.close();
143+
var dir = try fs.cwd().openDir(constants.template_dir, .{});
144+
defer dir.close();
145145
{
146-
var iter = iterable.iterate();
146+
var iter = dir.iterate();
147147
while (try iter.next()) |entry| {
148148
if (entry.name[0] == '.') continue;
149149
const key = try allocator.dupe(u8, entry.name);
@@ -154,7 +154,7 @@ fn readTemplates(allocator: Allocator, reporter: *Reporter) !std.StringHashMap(T
154154
while (iter.next()) |entry| {
155155
const name = entry.key_ptr.*;
156156
var scanner = Scanner{
157-
.source = try iterable.dir.readFileAlloc(allocator, name, constants.max_file_size),
157+
.source = try dir.readFileAlloc(allocator, name, constants.max_file_size),
158158
.filename = try fs.path.join(allocator, &.{ constants.template_dir, name }),
159159
.reporter = reporter,
160160
};

0 commit comments

Comments
 (0)