Closed joelreymont closed 3 weeks ago
toStruct
only works for array/tuple to struct conversion. You should be able to get a tuple by doing this:
const parser4 = mecha.combine(.{parser3}).map(toStruct(Defined));
I tried that but I still get the same error with this
const Defined = struct {
id: []const u8,
};
const parser4 = comptime combine(.{parser3}).map(toStruct(Defined));
const result = try parser4.parse(allocator, "foo");
try testing.expectEqualStrings("foo", result.value.id);
I tried that but I still get the same error with this
const Defined = struct { id: []const u8, }; const parser4 = comptime combine(.{parser3}).map(toStruct(Defined)); const result = try parser4.parse(allocator, "foo"); try testing.expectEqualStrings("foo", result.value.id);
Aah right, just had a look and for 1 parser, combine does not return a tuple. It would make sense for toStruct
be able to convert T
-> struct { field: T }
but it currently isn't supported
Is there a specific reason why combine
does not return a tuple for 1 parser?
Should this be considered an enhancement?
Is there a specific reason why
combine
does not return a tuple for 1 parser?Should this be considered an enhancement?
The logic was that you often will do something like this combine(.{ whitespace, token })
where whitespace
returns void
. The void
will then be excluded and only the result of token
remains. In that case, it is often not desired that the result of token
is wrapped in a tuple.
Would the same result be achieved by wrapping result of token in a tuple and providing an unwrap
combinator that can be used for 1-item tuples if desired?
As it stands, I'm forced to map using a function that takes a string and returns my structure which seems less elegant than an using an unwrap
combinator.
I mentioned earlier this:
It would make sense for toStruct be able to convert T -> struct { field: T } but it currently isn't supported
Wouldn't that work for your usecase?
It would work. I looked at toStruct
, though, tried to figure out how to specify what field
is and couldn't figure out a good approach.
On a second thought, I guess the name of the field doesn't matter, just as it doesn't matter when overlaying a tuple over structure fields.
PR #72 should fix this.
Extending the
asStr
test...gives me the error below.
What is the correct way of creating a structure where a field is of type string?