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

Fixes sam701/zig-cli#47 #48

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
refs/
tryout/
zig-cache/
zig-out/
zig-out/
.zig-cache/

22 changes: 14 additions & 8 deletions src/value_parser.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");
const Allocator = std.mem.Allocator;

pub const ValueParseError = error{
InvalidValue,
};
pub const ValueParser = *const fn (dest: *anyopaque, value: []const u8) ValueParseError!void;
} || 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,
Expand Down Expand Up @@ -35,7 +36,8 @@ fn intData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @alignCast(@ptrCast(dest));
dt.* = std.fmt.parseInt(ValueType, value, 10) catch return error.InvalidValue;
}
Expand All @@ -48,7 +50,8 @@ fn floatData(comptime ValueType: type, comptime DestinationType: type) ValueData
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
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;
}
Expand All @@ -65,7 +68,8 @@ fn boolData(comptime DestinationType: type) ValueData {
.value_size = @sizeOf(DestinationType),
.is_bool = true,
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
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)) {
Expand All @@ -83,9 +87,10 @@ fn stringData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = value;
const cpy = try alloc.dupe(u8, value);
Dinastyh marked this conversation as resolved.
Show resolved Hide resolved
dt.* = cpy;
}
}.parser,
.type_name = "string",
Expand All @@ -97,7 +102,8 @@ fn enumData(comptime ValueType: type, comptime DestinationType: type) ValueData
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
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));
Expand Down
4 changes: 2 additions & 2 deletions src/value_ref.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub const ValueRef = struct {
self.element_count += 1;
switch (self.value_type) {
.single => {
return self.value_data.value_parser(self.dest, value);
return self.value_data.value_parser(self.dest, value, alloc);
},
.multi => |*list| {
if (list.list_ptr == null) {
list.list_ptr = try list.vtable.createList(alloc);
}
const value_ptr = try list.vtable.addOne(list.list_ptr.?, alloc);
try self.value_data.value_parser(value_ptr, value);
try self.value_data.value_parser(value_ptr, value, alloc);
},
}
}
Expand Down