forked from sam701/zig-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_parser.zig
More file actions
119 lines (109 loc) · 4 KB
/
value_parser.zig
File metadata and controls
119 lines (109 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const ValueParseError = error{
InvalidValue,
} || std.mem.Allocator.Error;
pub const ValueParser = *const fn (dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void;
pub const ValueData = struct {
value_size: usize,
value_parser: ValueParser,
is_bool: bool = false,
type_name: []const u8,
};
pub fn getValueData(comptime T: type) ValueData {
const ValueType = switch (@typeInfo(T)) {
.optional => |oinfo| oinfo.child,
else => T,
};
return switch (@typeInfo(ValueType)) {
.int => intData(ValueType, T),
.float => floatData(ValueType, T),
.bool => boolData(T),
.pointer => |pinfo| {
if (pinfo.size == .slice and pinfo.child == u8) {
return stringData(T);
}
},
.@"enum" => enumData(ValueType, T),
else => @compileError("unsupported value type"),
};
}
fn intData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = std.fmt.parseInt(ValueType, value, 10) catch return error.InvalidValue;
}
}.parser,
.type_name = "integer",
};
}
fn floatData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = std.fmt.parseFloat(ValueType, value) catch return error.InvalidValue;
}
}.parser,
.type_name = "float",
};
}
pub const str_true = "true";
pub const str_false = "false";
fn boolData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.is_bool = true,
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @ptrCast(@alignCast(dest));
if (std.mem.eql(u8, value, str_true)) {
dt.* = true;
} else if (std.mem.eql(u8, value, str_false)) {
dt.* = false;
} else return error.InvalidValue;
}
}.parser,
.type_name = "bool",
};
}
fn stringData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
const dt: *DestinationType = @ptrCast(@alignCast(dest));
const cpy = try alloc.dupe(u8, value);
dt.* = cpy;
}
}.parser,
.type_name = "string",
};
}
fn enumData(comptime ValueType: type, comptime DestinationType: type) ValueData {
const edata = @typeInfo(ValueType).@"enum";
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
inline for (edata.fields) |field| {
if (std.mem.eql(u8, field.name, value)) {
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = @field(ValueType, field.name);
return;
}
}
return error.InvalidValue;
}
}.parser,
.type_name = "enum",
};
}