Skip to content

Commit

Permalink
WIP: zig-clap v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed Nov 18, 2024
1 parent cfa86f6 commit 36e0f9f
Show file tree
Hide file tree
Showing 3 changed files with 1,245 additions and 2 deletions.
8 changes: 6 additions & 2 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2132,14 +2132,18 @@ test "usage" {

test {
_ = args;
_ = ccw;
_ = parsers;
_ = streaming;
_ = ccw;
_ = v1;
_ = v2;
}

pub const args = @import("clap/args.zig");
pub const ccw = @import("clap/codepoint_counting_writer.zig");
pub const parsers = @import("clap/parsers.zig");
pub const streaming = @import("clap/streaming.zig");
pub const ccw = @import("clap/codepoint_counting_writer.zig");
pub const v1 = @This();
pub const v2 = @import("clap/v2.zig");

const std = @import("std");
59 changes: 59 additions & 0 deletions clap/types.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub fn isArrayListUnmanaged(comptime T: type) bool {
if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "Slice"))
return false;

const ptr_info = switch (@typeInfo(T.Slice)) {
.pointer => |info| info,
else => return false,
};

return T == std.ArrayListAlignedUnmanaged(ptr_info.child, null) or
T == std.ArrayListAlignedUnmanaged(ptr_info.child, ptr_info.alignment);
}

test isArrayListUnmanaged {
try std.testing.expect(!isArrayListUnmanaged(u8));
try std.testing.expect(!isArrayListUnmanaged([]const u8));
try std.testing.expect(!isArrayListUnmanaged(struct {
pub const Slice = []const u8;
}));
try std.testing.expect(isArrayListUnmanaged(std.ArrayListUnmanaged(u8)));
}

pub fn allFieldsHaveDefaults(comptime T: type) bool {
const info = switch (@typeInfo(T)) {
.@"struct" => |s| s,
else => return false,
};

inline for (info.fields) |field| {
if (field.default_value == null)
return false;
}

return true;
}

test allFieldsHaveDefaults {
try std.testing.expect(!allFieldsHaveDefaults(u8));
try std.testing.expect(!allFieldsHaveDefaults([]const u8));
try std.testing.expect(allFieldsHaveDefaults(struct {}));
try std.testing.expect(allFieldsHaveDefaults(struct {
a: u8 = 0,
}));
try std.testing.expect(!allFieldsHaveDefaults(struct {
a: u8,
}));
try std.testing.expect(!allFieldsHaveDefaults(struct {
a: u8,
b: u8 = 0,
c: u8,
}));
try std.testing.expect(allFieldsHaveDefaults(struct {
a: u8 = 1,
b: u8 = 0,
c: u8 = 3,
}));
}

const std = @import("std");
Loading

0 comments on commit 36e0f9f

Please sign in to comment.