mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
* Export invalidFmtErr To allow consistent use of "invalid format string" compile error response for badly formatted format strings. See https://github.com/ziglang/zig/pull/13489#issuecomment-1311759340. * Replace format compile errors with invalidFmtErr - Provides more consistent compile errors. - Gives user info about the type of the badly formated value. * Rename invalidFmtErr as invalidFmtError For consistency. Zig seems to use “Error” more often than “Err”. * std: add invalid format string checks to remaining custom formatters * pass reference-trace to comp when building build file; fix checkobjectstep
57 lines
1.8 KiB
Zig
57 lines
1.8 KiB
Zig
const std = @import("../../std.zig");
|
|
|
|
const fmt = std.fmt;
|
|
|
|
const IPv4 = std.x.os.IPv4;
|
|
const IPv6 = std.x.os.IPv6;
|
|
const Socket = std.x.os.Socket;
|
|
|
|
/// A generic IP abstraction.
|
|
const ip = @This();
|
|
|
|
/// A union of all eligible types of IP addresses.
|
|
pub const Address = union(enum) {
|
|
ipv4: IPv4.Address,
|
|
ipv6: IPv6.Address,
|
|
|
|
/// Instantiate a new address with a IPv4 host and port.
|
|
pub fn initIPv4(host: IPv4, port: u16) Address {
|
|
return .{ .ipv4 = .{ .host = host, .port = port } };
|
|
}
|
|
|
|
/// Instantiate a new address with a IPv6 host and port.
|
|
pub fn initIPv6(host: IPv6, port: u16) Address {
|
|
return .{ .ipv6 = .{ .host = host, .port = port } };
|
|
}
|
|
|
|
/// Re-interpret a generic socket address into an IP address.
|
|
pub fn from(address: Socket.Address) ip.Address {
|
|
return switch (address) {
|
|
.ipv4 => |ipv4_address| .{ .ipv4 = ipv4_address },
|
|
.ipv6 => |ipv6_address| .{ .ipv6 = ipv6_address },
|
|
};
|
|
}
|
|
|
|
/// Re-interpret an IP address into a generic socket address.
|
|
pub fn into(self: ip.Address) Socket.Address {
|
|
return switch (self) {
|
|
.ipv4 => |ipv4_address| .{ .ipv4 = ipv4_address },
|
|
.ipv6 => |ipv6_address| .{ .ipv6 = ipv6_address },
|
|
};
|
|
}
|
|
|
|
/// Implements the `std.fmt.format` API.
|
|
pub fn format(
|
|
self: ip.Address,
|
|
comptime layout: []const u8,
|
|
opts: fmt.FormatOptions,
|
|
writer: anytype,
|
|
) !void {
|
|
if (layout.len != 0) std.fmt.invalidFmtError(layout, self);
|
|
_ = opts;
|
|
switch (self) {
|
|
.ipv4 => |address| try fmt.format(writer, "{}:{}", .{ address.host, address.port }),
|
|
.ipv6 => |address| try fmt.format(writer, "{}:{}", .{ address.host, address.port }),
|
|
}
|
|
}
|
|
};
|