Skip to content

Commit

Permalink
[pkl.experimental.net] Address style issues
Browse files Browse the repository at this point in the history
* Use `_` for unused parameter
* Add empty line between declared properties
  • Loading branch information
The Pkl Team (automation) authored and bioball committed Apr 30, 2024
1 parent d40e586 commit a86888f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion packages/pkl.experimental.net/net.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import "./net.pkl"

// language=RegExp
const hidden hex: String = "[0-9a-fA-F]"

// language=RegExp
const hidden decByte: String = #"(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})"#

// language=RegExp
const hidden ipv4String = #"(\#(decByte)\.){3}\#(decByte)"#

// language=RegExp
const hidden ipv6String = #"""
(?x:
Expand All @@ -52,6 +55,7 @@ const hidden ipv6String = #"""
| \#(nonGlobalIPv6String)%[0-9]+ # Scoped address
)
"""#

// language=RegExp
const hidden nonGlobalIPv6String = #"""
(?xi-U:
Expand Down Expand Up @@ -83,18 +87,23 @@ typealias MACAddressString = String(matches(Regex(#"(\#(net.hex){1,2}[\.:-]){5}(

/// A string that contains either an IPv4 or IPv6 address.
typealias IPAddressString = IPv4AddressString|IPv6AddressString

/// A string that contains either an IPv4 or IPv6 address and port.
typealias IPAddressPortString = IPv4AddressPortString|IPv6AddressPortString

/// A string that contains either an IPv4 or IPv6 CIDR range.
typealias IPCIDRString = IPv4CIDRString|IPv6CIDRString

/// An IPv4 or IPv6 address.
typealias IPAddress = IPv4Address|IPv6Address

/// An IPv4 or IPv6 network.
typealias IPNetwork = IPv4Network|IPv6Network

/// A string that contains an IPv4 address.
// language=RegExp
typealias IPv4AddressString = String(matches(Regex(net.ipv4String)))

/// A string that contains an IPv6 address.
///
/// IPv6 unicast and multicast addresses of non-global scope except for the unspecified address and the loopback address
Expand All @@ -106,13 +115,15 @@ typealias IPv6AddressString = String(matches(Regex(net.ipv6String)))
/// A string that contains an IPv4 address and port.
// language=RegExp
typealias IPv4AddressPortString = String(matches(Regex(#"\#(net.ipv4String):[0-9]{1,5}"#)))

/// A string that contains an IPv6 address and port.
// language=RegExp
typealias IPv6AddressPortString = String(matches(Regex(#"\[\#(net.ipv6String)\]:[0-9]{1,5}"#)))

/// A string that contains an IPv4 address.
// language=RegExp
typealias IPv4CIDRString = String(matches(Regex(#"\#(net.ipv4String)/[0-9]{1,2}"#)))

/// A string that contains an IPv6 address.
// language=RegExp
typealias IPv6CIDRString = String(matches(Regex(#"\#(net.ipv6String)/[0-9]{1,3}"#)))
Expand All @@ -135,12 +146,14 @@ function IPv4Address(ip: IPv4AddressString): IPv4Address = new {
/// An IPv4 address.
class IPv4Address {
repr: UInt32

const hidden bitWidth: UInt = 32

local self = this

/// maskHi generates a mask of 1s in the top [prefix] bits of a [UInt32].
function maskHi(prefix: UInt(isBetween(0, bitWidth))): UInt32 = mask32Hi(prefix)

/// maskLo generates a mask of 1s in the bottom [suffix] bits of a [UInt32].
function maskLo(suffix: UInt(isBetween(0, bitWidth))): UInt32 = mask32Lo(suffix)

Expand Down Expand Up @@ -168,7 +181,7 @@ function IPv6Address(ip: IPv6AddressString): IPv6Address =
let (scope = idx.ifNonNull((it) -> ip.drop(it+1).toInt()))
let (ip = idx.ifNonNull((it) -> ip.take(it)) ?? ip)
let (ipIdx = ip.indexOfOrNull(Regex(":\(ipv4String)$")))
let (ipv4 = ipIdx.ifNonNull((it) -> ip.drop(ipIdx+1).split(".").map((s) -> s.toInt())))
let (ipv4 = ipIdx.ifNonNull((_) -> ip.drop(ipIdx+1).split(".").map((s) -> s.toInt())))
let (ip = ipIdx.ifNonNull((it) -> ip.replaceRange(it, ip.length, ":0:0")) ?? ip)
let (_ip = expandIPv6AddressString(ip).toLowerCase().split(":"))
new {
Expand All @@ -185,22 +198,29 @@ function IPv6Address(ip: IPv6AddressString): IPv6Address =
/// An IPv6 address.
class IPv6Address {
repr: u128.UInt128

/// The scope ID associated with this address, or [null] if the scope is unknown or not applicable.
///
/// Only addresses of [non-global scope][isNonGlobalScope] are allowed to have a scope ID.
scopeId: UInt(isNonGlobalScope)?

const hidden bitWidth: UInt = 128

/// Tells if this address is the unspecified address (`::`).
fixed hidden isUnspecified: Boolean = repr == u128.zero

/// Tells if this address is the loopback address (`::1`).
fixed hidden isLoopback: Boolean = repr == u128.one

/// Tells if this address is an IPv4-mapped address (`::ffff:0:0/96`).
fixed hidden isIPv4Mapped: Boolean =
repr.hihi == 0 && repr.hilo == 0 && repr.lohi == 0xffff

/// Tells if this address is an IPv4-embedded address using the Well-Known Prefix (`64:ff9b::/96`) described by
/// [RFC 6052 §2.1](https://tools.ietf.org/html/rfc6052#section-2.1).
fixed hidden isIPv4Embedded: Boolean =
repr.hihi == 0x64ff9b && repr.hilo == 0 && repr.lohi == 0

/// Tells if this address has non-global unicast or multicast scope.
///
/// Returns [true] for link-local unicast and for multicast with "scop" field less than global. Returns [false] for
Expand All @@ -218,6 +238,7 @@ class IPv6Address {
else if (prefix <= 64) u128.UInt128(math.maxUInt32, mask32Hi(prefix - 32), 0, 0)
else if (prefix <= 96) u128.UInt128(math.maxUInt32, math.maxUInt32, mask32Hi(prefix - 64), 0)
else u128.UInt128(math.maxUInt32, math.maxUInt32, math.maxUInt32, mask32Hi(prefix - 96))

/// maskLo generates a mask of 1s in the bottom [suffix] bits of a [u128.UInt128].
function maskLo(suffix: UInt(isBetween(0, bitWidth))): u128.UInt128 =
if (suffix <= 32) u128.UInt128(0, 0, 0, mask32Lo(suffix))
Expand Down Expand Up @@ -292,16 +313,20 @@ function IPv4Network(cidr: IPv4CIDRString): IPv4Network = new {
class IPv4Network {
/// The base address of this network
base: IPv4Address

/// The CIDR prefix of this network
prefix: UInt(isBetween(0, bitWidth))

fixed hidden bitWidth = base.bitWidth

const hidden reverseBitResolution: UInt = 8

local self = this

/// The first address in this network.
/// Will be equivalent to [lastAddress] when [prefix] is equal to [bitWidth].
fixed firstAddress: IPv4Address = new { repr = base.repr.and(base.maskHi(prefix)) }

/// The last address in this network.
/// Will be equivalent to [firstAddress] when [prefix] is equal to [bitWidth].
fixed lastAddress: IPv4Address = new { repr = base.repr.or(base.maskLo(bitWidth - prefix)) }
Expand All @@ -311,6 +336,7 @@ class IPv4Network {

/// Return true if this network contains [ip].
function contains(ip: IPv4Address): Boolean = firstAddress.repr <= ip.repr && ip.repr <= lastAddress.repr

/// Generate the name of the reverse DNS zone for this network.
function reverse(): String = base.toString().split(".").take(prefix ~/ reverseBitResolution).reverse().join(".") + ".in-addr.arpa"

Expand Down Expand Up @@ -343,16 +369,20 @@ function IPv6Network(cidr: IPv6CIDRString): IPv6Network = new {
class IPv6Network {
/// The base address of this network
base: IPv6Address

/// The CIDR prefix of this network
prefix: UInt(isBetween(0, bitWidth))

fixed hidden bitWidth = base.bitWidth

const hidden reverseBitResolution: UInt = 4

local self = this

/// The first address in this network.
/// Will be equivalent to [lastAddress] when [prefix] is equal to [bitWidth].
fixed firstAddress: IPv6Address = (base) { repr = base.repr.and(base.maskHi(prefix)) }

/// The last address in this network.
/// Will be equivalent to [firstAddress] when [prefix] is equal to [bitWidth].
fixed lastAddress: IPv6Address = (base) { repr = base.repr.or(base.maskLo(bitWidth - prefix)) }
Expand All @@ -363,6 +393,7 @@ class IPv6Network {
/// If either is [null] then scope ID is ignored.
function contains(ip: IPv6Address): Boolean = firstAddress.repr.le(ip.repr) && ip.repr.le(lastAddress.repr) &&
(base.scopeId == ip.scopeId || base.scopeId == null || ip.scopeId == null)

/// Generate the name of the reverse DNS zone for this network.
function reverse(): String = (base) { scopeId = null }.toExpandedString().replaceAll(":", "").chars.take(prefix ~/ reverseBitResolution).reverse().join(".") + ".ip6.arpa"

Expand Down

0 comments on commit a86888f

Please sign in to comment.