diff options
| author | 2024-10-22 17:46:47 +0200 | |
|---|---|---|
| committer | 2024-10-22 17:48:34 +0200 | |
| commit | e73b56aa4bcb7e53144ef96ee978f2a19b32669d (patch) | |
| tree | 0ab5e3d426e25d2ad9d2e0cd0015fc010d9ea182 /clap/parsers.zig | |
| parent | feat: Add `terminating_positional` to `clap.ParseOptions` (diff) | |
| download | zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.gz zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.tar.xz zig-clap-e73b56aa4bcb7e53144ef96ee978f2a19b32669d.zip | |
refactor: Always access using full namespace
This is my new preferred style of programming Zig :)
Diffstat (limited to 'clap/parsers.zig')
| -rw-r--r-- | clap/parsers.zig | 55 |
1 files changed, 26 insertions, 29 deletions
diff --git a/clap/parsers.zig b/clap/parsers.zig index 8abdf57..2bf8976 100644 --- a/clap/parsers.zig +++ b/clap/parsers.zig | |||
| @@ -1,8 +1,3 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const fmt = std.fmt; | ||
| 4 | const testing = std.testing; | ||
| 5 | |||
| 6 | pub const default = .{ | 1 | pub const default = .{ |
| 7 | .string = string, | 2 | .string = string, |
| 8 | .str = string, | 3 | .str = string, |
| @@ -26,46 +21,46 @@ pub fn string(in: []const u8) error{}![]const u8 { | |||
| 26 | } | 21 | } |
| 27 | 22 | ||
| 28 | test "string" { | 23 | test "string" { |
| 29 | try testing.expectEqualStrings("aa", try string("aa")); | 24 | try std.testing.expectEqualStrings("aa", try string("aa")); |
| 30 | } | 25 | } |
| 31 | 26 | ||
| 32 | /// A parser that uses `std.fmt.parseInt` to parse the string into an integer value. | 27 | /// A parser that uses `std.fmt.parseInt` to parse the string into an integer value. |
| 33 | /// See `std.fmt.parseInt` documentation for more information. | 28 | /// See `std.fmt.parseInt` documentation for more information. |
| 34 | pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) fmt.ParseIntError!T { | 29 | pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) std.fmt.ParseIntError!T { |
| 35 | return struct { | 30 | return struct { |
| 36 | fn parse(in: []const u8) fmt.ParseIntError!T { | 31 | fn parse(in: []const u8) std.fmt.ParseIntError!T { |
| 37 | return fmt.parseInt(T, in, base); | 32 | return std.fmt.parseInt(T, in, base); |
| 38 | } | 33 | } |
| 39 | }.parse; | 34 | }.parse; |
| 40 | } | 35 | } |
| 41 | 36 | ||
| 42 | test "int" { | 37 | test "int" { |
| 43 | try testing.expectEqual(@as(u8, 0), try int(u8, 10)("0")); | 38 | try std.testing.expectEqual(@as(u8, 0), try int(u8, 10)("0")); |
| 44 | try testing.expectEqual(@as(u8, 1), try int(u8, 10)("1")); | 39 | try std.testing.expectEqual(@as(u8, 1), try int(u8, 10)("1")); |
| 45 | try testing.expectEqual(@as(u8, 10), try int(u8, 10)("10")); | 40 | try std.testing.expectEqual(@as(u8, 10), try int(u8, 10)("10")); |
| 46 | try testing.expectEqual(@as(u8, 0b10), try int(u8, 2)("10")); | 41 | try std.testing.expectEqual(@as(u8, 0b10), try int(u8, 2)("10")); |
| 47 | try testing.expectEqual(@as(u8, 0x10), try int(u8, 0)("0x10")); | 42 | try std.testing.expectEqual(@as(u8, 0x10), try int(u8, 0)("0x10")); |
| 48 | try testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10")); | 43 | try std.testing.expectEqual(@as(u8, 0b10), try int(u8, 0)("0b10")); |
| 49 | try testing.expectEqual(@as(u16, 0), try int(u16, 10)("0")); | 44 | try std.testing.expectEqual(@as(u16, 0), try int(u16, 10)("0")); |
| 50 | try testing.expectEqual(@as(u16, 1), try int(u16, 10)("1")); | 45 | try std.testing.expectEqual(@as(u16, 1), try int(u16, 10)("1")); |
| 51 | try testing.expectEqual(@as(u16, 10), try int(u16, 10)("10")); | 46 | try std.testing.expectEqual(@as(u16, 10), try int(u16, 10)("10")); |
| 52 | try testing.expectEqual(@as(u16, 0b10), try int(u16, 2)("10")); | 47 | try std.testing.expectEqual(@as(u16, 0b10), try int(u16, 2)("10")); |
| 53 | try testing.expectEqual(@as(u16, 0x10), try int(u16, 0)("0x10")); | 48 | try std.testing.expectEqual(@as(u16, 0x10), try int(u16, 0)("0x10")); |
| 54 | try testing.expectEqual(@as(u16, 0b10), try int(u16, 0)("0b10")); | 49 | try std.testing.expectEqual(@as(u16, 0b10), try int(u16, 0)("0b10")); |
| 55 | } | 50 | } |
| 56 | 51 | ||
| 57 | /// A parser that uses `std.fmt.parseFloat` to parse the string into an float value. | 52 | /// A parser that uses `std.fmt.parseFloat` to parse the string into an float value. |
| 58 | /// See `std.fmt.parseFloat` documentation for more information. | 53 | /// See `std.fmt.parseFloat` documentation for more information. |
| 59 | pub fn float(comptime T: type) fn ([]const u8) fmt.ParseFloatError!T { | 54 | pub fn float(comptime T: type) fn ([]const u8) std.fmt.ParseFloatError!T { |
| 60 | return struct { | 55 | return struct { |
| 61 | fn parse(in: []const u8) fmt.ParseFloatError!T { | 56 | fn parse(in: []const u8) std.fmt.ParseFloatError!T { |
| 62 | return fmt.parseFloat(T, in); | 57 | return std.fmt.parseFloat(T, in); |
| 63 | } | 58 | } |
| 64 | }.parse; | 59 | }.parse; |
| 65 | } | 60 | } |
| 66 | 61 | ||
| 67 | test "float" { | 62 | test "float" { |
| 68 | try testing.expectEqual(@as(f32, 0), try float(f32)("0")); | 63 | try std.testing.expectEqual(@as(f32, 0), try float(f32)("0")); |
| 69 | } | 64 | } |
| 70 | 65 | ||
| 71 | pub const EnumError = error{ | 66 | pub const EnumError = error{ |
| @@ -85,10 +80,10 @@ pub fn enumeration(comptime T: type) fn ([]const u8) EnumError!T { | |||
| 85 | 80 | ||
| 86 | test "enumeration" { | 81 | test "enumeration" { |
| 87 | const E = enum { a, b, c }; | 82 | const E = enum { a, b, c }; |
| 88 | try testing.expectEqual(E.a, try enumeration(E)("a")); | 83 | try std.testing.expectEqual(E.a, try enumeration(E)("a")); |
| 89 | try testing.expectEqual(E.b, try enumeration(E)("b")); | 84 | try std.testing.expectEqual(E.b, try enumeration(E)("b")); |
| 90 | try testing.expectEqual(E.c, try enumeration(E)("c")); | 85 | try std.testing.expectEqual(E.c, try enumeration(E)("c")); |
| 91 | try testing.expectError(EnumError.NameNotPartOfEnum, enumeration(E)("d")); | 86 | try std.testing.expectError(EnumError.NameNotPartOfEnum, enumeration(E)("d")); |
| 92 | } | 87 | } |
| 93 | 88 | ||
| 94 | fn ReturnType(comptime P: type) type { | 89 | fn ReturnType(comptime P: type) type { |
| @@ -98,3 +93,5 @@ fn ReturnType(comptime P: type) type { | |||
| 98 | pub fn Result(comptime P: type) type { | 93 | pub fn Result(comptime P: type) type { |
| 99 | return @typeInfo(ReturnType(P)).error_union.payload; | 94 | return @typeInfo(ReturnType(P)).error_union.payload; |
| 100 | } | 95 | } |
| 96 | |||
| 97 | const std = @import("std"); | ||