Hey everyone! I’m a bit stuck with this one, and I sincerely hope that someone will shed some light on where exactly I’m wrong.
Basically, I’m trying to write a function that should break up a given string into an array of 4 u8 integers (IPv4 address parsing). It goes like this -
const std = @import("std");
pub const NetworkAddressError = error {
IPV4ParseError,
};
pub fn ipV4StringToSlice(ip_addr: []const u8) ![4]u8 {
const heap = std.heap.page_allocator;
const buffer = try heap.alloc(u8, 32);
defer heap.free(buffer);
var fba = std.heap.FixedBufferAllocator.init(buffer);
const alloc = fba.allocator();
// Using ArrayList because we do not know the values at the compile time;
// We get them from elsewhere (i.e. CLI args or environment variables)
var address = try std.ArrayList(u8).initCapacity(alloc, 4);
defer address.deinit(alloc);
var iter = std.mem.splitScalar(u8, ip_addr, '.');
while(iter.next()) |val| {
const octet = try std.fmt.parseInt(u8, val, 10);
if (address.append(alloc, octet)) |_| {
return address.items;
} else |err| {
std.debug.print("Error: invalid octet number;\n{any}\n", .{ err });
return NetworkAddressError.IPV4ParseError;
}
}
}
Unfortunately no matter what I try, I get the following compilation error - error: expected type '@typeInfo(@typeInfo(@TypeOf(misc.ipV4StringToSlice)).@"fn".return_type.?).error_union.error_set![4]u8', found '[]u8'. I have tried handling error in all the possible ways I could imagine, but to zero success.
So my question is - what is the correct way of handling errors in these particular situations (using while loops with iterators)? Is it possible at all or my function logic is flawed at root? I would be immensely grateful for any insight you might share!