Add shims/net for forced IP address initialization

This commit is contained in:
Zahary Karadjov 2020-06-05 17:05:22 +03:00
parent 86ac01122c
commit c0f884b81b
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 33 additions and 0 deletions

33
stew/shims/net.nim Normal file
View File

@ -0,0 +1,33 @@
import std/net as stdNet
export stdNet
type
ValidIpAddress* = distinct IpAddress
{.push raises: [Defect].}
proc ipv4*(address: array[4, byte]): ValidIpAddress =
ValidIpAddress IpAddress(family: IPv4, address_v4: address)
template ipv4*(a, b, c, d: byte): ValidIpAddress =
ipv4([a, b, c, d])
proc ipv6*(address: array[16, byte]): ValidIpAddress =
ValidIpAddress IpAddress(family: IPv6, address_v6: address)
template family*(a: ValidIpAddress): IpAddressFamily =
IpAddress(a).family
template address_v4*(a: ValidIpAddress): array[4, byte] =
IpAddress(a).address_v4
template address_v6*(a: ValidIpAddress): array[16, byte] =
IpAddress(a).address_v6
template `$`*(a: ValidIpAddress): string =
$ IpAddress(a)
func init*(T: type ValidIpAddress, str: string): T
{.raises: [ValueError].} =
ValidIpAddress stdNet.parseIpAddress(str)