-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,70 @@ | ||
const std = @import("std"); | ||
|
||
// const Parser = *const fn (dest: *anyopaque, value: []const u8) anyerror!void; | ||
pub fn ValueParser(comptime T: type) type { | ||
// TODO: the parse function might need an allocator, e.g. to copy a string or allocate the destination type if it is a pointer. | ||
return *const fn (dest: *T, value: []const u8) anyerror!void; | ||
} | ||
pub const ValueParser = *const fn (dest: *anyopaque, value: []const u8) anyerror!void; | ||
|
||
pub const ValueData = struct { | ||
value_size: usize, | ||
value_parser: ValueParser, | ||
}; | ||
|
||
pub fn get(comptime T: type) ValueParser(T) { | ||
pub fn getValueData(comptime T: type) ValueData { | ||
return switch (@typeInfo(T)) { | ||
.Int => intParser(T), | ||
.Float => floatParser(T), | ||
.Bool => boolParser(T), | ||
.Int => intData(T), | ||
.Float => floatData(T), | ||
.Bool => boolData(T), | ||
.Pointer => |pinfo| { | ||
if (pinfo.size == .Slice and pinfo.child == u8) { | ||
stringParser(T); | ||
return stringData(T); | ||
} | ||
}, | ||
else => @compileError("unsupported value type"), | ||
}; | ||
} | ||
|
||
fn intParser(comptime T: type) ValueParser(T) { | ||
return struct { | ||
fn parser(dest: *T, value: []const u8) anyerror!void { | ||
dest.* = try std.fmt.parseInt(T, value, 10); | ||
} | ||
}.parser; | ||
fn intData(comptime T: type) ValueData { | ||
return .{ | ||
.value_size = @sizeOf(T), | ||
.value_parser = struct { | ||
fn parser(dest: *anyopaque, value: []const u8) anyerror!void { | ||
const dt: *T = @ptrCast(@alignCast(dest)); | ||
dt.* = try std.fmt.parseInt(T, value, 10); | ||
} | ||
}.parser, | ||
}; | ||
} | ||
|
||
fn floatParser(comptime T: type) ValueParser(T) { | ||
return struct { | ||
fn parser(dest: *T, value: []const u8) anyerror!void { | ||
dest.* = try std.fmt.parseFloat(T, value); | ||
} | ||
}.parser; | ||
fn floatData(comptime T: type) ValueData { | ||
return .{ | ||
.value_size = @sizeOf(T), | ||
.value_parser = struct { | ||
fn parser(dest: *anyopaque, value: []const u8) anyerror!void { | ||
const dt: *T = @ptrCast(@alignCast(dest)); | ||
dt.* = try std.fmt.parseFloat(T, value); | ||
} | ||
}.parser, | ||
}; | ||
} | ||
|
||
fn boolParser(comptime T: type) ValueParser(T) { | ||
return struct { | ||
fn parser(dest: *T, value: []const u8) anyerror!void { | ||
dest.* = std.mem.eql(u8, value, "true"); | ||
} | ||
}.parser; | ||
fn boolData(comptime T: type) ValueData { | ||
return .{ | ||
.value_size = @sizeOf(T), | ||
.value_parser = struct { | ||
fn parser(dest: *anyopaque, value: []const u8) anyerror!void { | ||
const dt: *T = @ptrCast(@alignCast(dest)); | ||
dt.* = std.mem.eql(u8, value, "true"); | ||
} | ||
}.parser, | ||
}; | ||
} | ||
|
||
fn stringParser(comptime T: type) ValueParser(T) { | ||
return struct { | ||
fn parser(dest: *T, value: []const u8) anyerror!void { | ||
dest.* = value; | ||
} | ||
}.parser; | ||
fn stringData(comptime T: type) ValueData { | ||
return .{ | ||
.value_size = @sizeOf(T), | ||
.value_parser = struct { | ||
fn parser(dest: *anyopaque, value: []const u8) anyerror!void { | ||
const dt: *T = @ptrCast(@alignCast(dest)); | ||
dt.* = value; | ||
} | ||
}.parser, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters