Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsers: Actually add some documentation to these functions #104

Merged
merged 2 commits into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions clap/parsers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const default = .{
.f64 = float(f64),
};

/// A parser that does nothing.
pub fn string(in: []const u8) error{}![]const u8 {
return in;
}
Expand All @@ -28,10 +29,12 @@ test "string" {
try testing.expectEqualStrings("aa", try string("aa"));
}

pub fn int(comptime T: type, comptime radix: u8) fn ([]const u8) fmt.ParseIntError!T {
/// A parser that uses `std.fmt.parseInt` to parse the string into an integer value.
/// See `std.fmt.parseInt` documentation for more information.
pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) fmt.ParseIntError!T {
return struct {
fn parse(in: []const u8) fmt.ParseIntError!T {
return fmt.parseInt(T, in, radix);
return fmt.parseInt(T, in, base);
}
}.parse;
}
Expand All @@ -40,10 +43,19 @@ test "int" {
try testing.expectEqual(@as(u8, 0), try int(u8, 10)("0"));
try testing.expectEqual(@as(u8, 1), try int(u8, 10)("1"));
try testing.expectEqual(@as(u8, 10), try int(u8, 10)("10"));
try testing.expectEqual(@as(u8, 0b10), try int(u8, 2)("10"));
try testing.expectEqual(@as(u8, 0x10), try int(u8, 0)("0x10"));
try testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10"));
try testing.expectEqual(@as(u16, 0), try int(u16, 10)("0"));
try testing.expectEqual(@as(u16, 1), try int(u16, 10)("1"));
try testing.expectEqual(@as(u16, 10), try int(u16, 10)("10"));
try testing.expectEqual(@as(u16, 0b10), try int(u16, 2)("10"));
try testing.expectEqual(@as(u16, 0x10), try int(u16, 0)("0x10"));
try testing.expectEqual(@as(u16, 0b10), try int(u16, 0)("0b10"));
}

/// A parser that uses `std.fmt.parseFloat` to parse the string into an float value.
/// See `std.fmt.parseFloat` documentation for more information.
pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T {
return struct {
fn parse(in: []const u8) fmt.ParseFloatError!T {
Expand All @@ -60,6 +72,9 @@ pub const EnumError = error{
NameNotPartOfEnum,
};

/// A parser that uses `std.meta.stringToEnum` to parse the string into an enum value. On `null`,
/// this function returns the error `NameNotPartOfEnum`.
/// See `std.meta.stringToEnum` documentation for more information.
pub fn enumeration(comptime T: type) fn ([]const u8) EnumError!T {
return struct {
fn parse(in: []const u8) EnumError!T {
Expand Down