Repository: apple/swift-nio
File: Sources/NIOCore/IPProtocol.swift (Swift)

Code before:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// In the Internet Protocol version 4 (IPv4) [RFC791] there is a field
/// called "Protocol" to identify the next level protocol.  This is an 8
/// bit field.  In Internet Protocol version 6 (IPv6) [RFC8200], this field
/// is called the "Next Header" field.
public struct NIOIPProtocol: RawRepresentable, Hashable {
    public typealias RawValue = UInt8
    public var rawValue: RawValue
    
    @inlinable
    public init(rawValue: RawValue) {
        self.rawValue = rawValue
    }
}

// Generated with:
//
// import Foundation
//
// let protocols = try String(contentsOf: URL(filePath: "/etc/protocols"))
//
// let regex = #/^([^#].+)\t+(\d+)\t+(([\w\d\-\+\./]+)\t*)+#*\ *(.+)?$/#.anchorsMatchLineEndings()
// for match in protocols.matches(of: regex) {
//     let (_, name, number, _, _, description) = match.output
//
//     guard let number = UInt8(number) else { continue }
//
//     let identifier = name
//         // hyphens to camel case
//         .replacing(#/-([a-z1-9])/#) { match in
//             match.output.1.uppercased()

Review comment (suggestion):
For the sake of convenience it might be nice to add a `CustomStringConvertible` implementation that switches over the named `static let`s and prints those names as well as the raw number.

Code after:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// In the Internet Protocol version 4 (IPv4) [RFC791] there is a field
/// called "Protocol" to identify the next level protocol.  This is an 8
/// bit field.  In Internet Protocol version 6 (IPv6) [RFC8200], this field
/// is called the "Next Header" field.
public struct NIOIPProtocol: RawRepresentable, Hashable {
    public typealias RawValue = UInt8
    public var rawValue: RawValue
    
    @inlinable
    public init(rawValue: RawValue) {
        self.rawValue = rawValue
    }
}

extension NIOIPProtocol {
    /// - precondition: `rawValue` must fit into an `UInt8`
    public init(_ rawValue: Int) {
        self.init(rawValue: UInt8(rawValue))
    }
}

// Subset of https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml with an RFC
extension NIOIPProtocol {
    /// IPv6 Hop-by-Hop Option - [RFC8200]
    public static let hopopt = Self(rawValue: 0)
    /// Internet Control Message - [RFC792]
    public static let icmp = Self(rawValue: 1)
    /// Internet Group Management - [RFC1112]
    public static let igmp = Self(rawValue: 2)
    /// Gateway-to-Gateway - [RFC823]