<commit_before>//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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
//
//===----------------------------------------------------------------------===//

import NIO

/// HTTP/2 error codes that may be found in frames at the HTTP/2 level.
public enum HTTP2ErrorCode {
    /// The associated condition is not a result of an error. For example,
    /// a GOAWAY might include this code to indicate graceful shutdown of
    /// a connection.
    case noError

    /// The endpoint detected an unspecific protocol error. This error is
    /// for use when a more specific error code is not available.
    case protocolError

    /// The endpoint encountered an unexpected internal error.
    case internalError

    /// The endpoint detected that its peer violated the flow-control
    /// protocol.
    case flowControlError

    /// The endpoint sent a SETTINGS frame but did not receive a
    /// response in a timely manner.
    case settingsTimeout

    /// The endpoint received a frame after a stream was half-closed.
    case streamClosed

    /// The endpoint received a frame with an invalid size.
    case frameSizeError

    /// The endpoint refused the stream prior to performing any
    /// application processing.
    case refusedStream

    /// Used by the endpoint to indicate that the stream is no
    /// longer needed.
    case cancel

    /// The endpoint is unable to maintain the header compression
    /// context for the connection.
    case compressionError

    /// The connection established in response to a CONNECT request
    /// was reset or abnormally closed.
    case connectError

    /// The endpoint detected that its peer is exhibiting a behavior
    /// that might be generating excessive load.
    case enhanceYourCalm

    /// The underlying transport has properties that do not meet
    /// minimum security requirements.
    case inadequateSecurity

    /// The endpoint requires that HTTP/1.1 be used instead of HTTP/2.
    case http11Required

    /// The error code is not known to NIO.
    case unknown(UInt32)
}

public extension HTTP2ErrorCode {
    /// Create a `HTTP2ErrorCode` from the 32-bit integer it corresponds to.
    public init(_ networkInteger: UInt32) {
        switch networkInteger {
        case 0x0:
            self = .noError
        case 0x1:
            self = .protocolError
        case 0x2:
            self = .internalError
        case 0x3:
            self = .flowControlError
        case 0x4:
            self = .settingsTimeout
        case 0x5:
            self = .streamClosed
        case 0x6:
            self = .frameSizeError
        case 0x7:
            self = .refusedStream
        case 0x8:
            self = .cancel
        case 0x9:
            self = .compressionError
        case 0xa:
            self = .connectError
        case 0xb:
            self = .enhanceYourCalm
        case 0xc:
            self = .inadequateSecurity
        case 0xd:
            self = .http11Required
        default:
            self = .unknown(networkInteger)
        }
    }

    /// Create a `HTTP2ErrorCode` from the integer it corresponds to.
    public init(_ userInteger: Int) {
        self.init(UInt32(userInteger))
    }
}

public extension UInt32 {
    /// Create a 32-bit integer corresponding to the given `HTTP2ErrorCode`.
    public init(http2ErrorCode code: HTTP2ErrorCode) {
        switch code {
        case .noError:
            self = 0x0
        case .protocolError:
            self = 0x1
        case .internalError:
            self = 0x2
        case .flowControlError:
            self = 0x3
        case .settingsTimeout:
            self = 0x4
        case .streamClosed:
            self = 0x5
        case .frameSizeError:
            self = 0x6
        case .refusedStream:
            self = 0x7
        case .cancel:
            self = 0x8
        case .compressionError:
            self = 0x9
        case .connectError:
            self = 0xa
        case .enhanceYourCalm:
            self = 0xb
        case .inadequateSecurity:
            self = 0xc
        case .http11Required:
            self = 0xd
        case .unknown(let val):
            self = val
        }
    }
}

public extension Int {
    /// Create an integer corresponding to the given `HTTP2ErrorCode`.
    public init(http2ErrorCode code: HTTP2ErrorCode) {
        self = Int(UInt32(http2ErrorCode: code))
    }
}

public extension ByteBuffer {
    /// Serializes a `HTTP2ErrorCode` into a `ByteBuffer` in the appropriate endianness
    /// for use in HTTP/2.
    ///
    /// - parameters:
    ///     - code: The `HTTP2ErrorCode` to serialize.
    /// - returns: The number of bytes written.
    public mutating func write(http2ErrorCode code: HTTP2ErrorCode) -> Int {
        return self.write(integer: UInt32(http2ErrorCode: code))
    }
}<commit_msg>Refactor error code to struct instead of enum<commit_after>//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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
//
//===----------------------------------------------------------------------===//

import NIO

/// An HTTP/2 error code.
public struct HTTP2ErrorCode {
    /// The underlying network representation of the error code.
    public var networkCode: Int {
        get {
            return Int(self._networkCode)
        }
        set {
            self._networkCode = UInt32(newValue)
        }
    }

    /// The underlying network representation of the error code.
    fileprivate var _networkCode: UInt32

    /// Create a HTTP/2 error code from the given network value.
    public init(networkCode: Int) {
        self._networkCode = UInt32(networkCode)
    }

    /// Create a `HTTP2ErrorCode` from the 32-bit integer it corresponds to.
    internal init(_ networkInteger: UInt32) {
        self._networkCode = networkInteger
    }

    /// The associated condition is not a result of an error. For example,
    /// a GOAWAY might include this code to indicate graceful shutdown of
    /// a connection.
    public static let noError = HTTP2ErrorCode(networkCode: 0x0)

    /// The endpoint detected an unspecific protocol error. This error is
    /// for use when a more specific error code is not available.
    public static let protocolError = HTTP2ErrorCode(networkCode: 0x01)

    /// The endpoint encountered an unexpected internal error.
    public static let internalError = HTTP2ErrorCode(networkCode: 0x02)

    /// The endpoint detected that its peer violated the flow-control
    /// protocol.
    public static let flowControlError = HTTP2ErrorCode(networkCode: 0x03)

    /// The endpoint sent a SETTINGS frame but did not receive a
    /// response in a timely manner.
    public static let settingsTimeout = HTTP2ErrorCode(networkCode: 0x04)

    /// The endpoint received a frame after a stream was half-closed.
    public static let streamClosed = HTTP2ErrorCode(networkCode: 0x05)

    /// The endpoint received a frame with an invalid size.
    public static let frameSizeError = HTTP2ErrorCode(networkCode: 0x06)

    /// The endpoint refused the stream prior to performing any
    /// application processing.
    public static let refusedStream = HTTP2ErrorCode(networkCode: 0x07)

    /// Used by the endpoint to indicate that the stream is no
    /// longer needed.
    public static let cancel = HTTP2ErrorCode(networkCode: 0x08)

    /// The endpoint is unable to maintain the header compression
    /// context for the connection.
    public static let compressionError = HTTP2ErrorCode(networkCode: 0x09)

    /// The connection established in response to a CONNECT request
    /// was reset or abnormally closed.
    public static let connectError = HTTP2ErrorCode(networkCode: 0x0a)

    /// The endpoint detected that its peer is exhibiting a behavior
    /// that might be generating excessive load.
    public static let enhanceYourCalm = HTTP2ErrorCode(networkCode: 0x0b)

    /// The underlying transport has properties that do not meet
    /// minimum security requirements.
    public static let inadequateSecurity = HTTP2ErrorCode(networkCode: 0x0c)

    /// The endpoint requires that HTTP/1.1 be used instead of HTTP/2.
    public static let http11Required = HTTP2ErrorCode(networkCode: 0x0d)
}

extension HTTP2ErrorCode: Equatable {
    public static func ==(lhs: HTTP2ErrorCode, rhs: HTTP2ErrorCode) -> Bool {
        return lhs._networkCode == rhs._networkCode
    }
}

extension HTTP2ErrorCode: Hashable {
    public var hashValue: Int {
        return self._networkCode.hashValue
    }
}

public extension UInt32 {
    /// Create a 32-bit integer corresponding to the given `HTTP2ErrorCode`.
    public init(http2ErrorCode code: HTTP2ErrorCode) {
        self = code._networkCode
    }
}

public extension Int {
    /// Create an integer corresponding to the given `HTTP2ErrorCode`.
    public init(http2ErrorCode code: HTTP2ErrorCode) {
        self = code.networkCode
    }
}

public extension ByteBuffer {
    /// Serializes a `HTTP2ErrorCode` into a `ByteBuffer` in the appropriate endianness
    /// for use in HTTP/2.
    ///
    /// - parameters:
    ///     - code: The `HTTP2ErrorCode` to serialize.
    /// - returns: The number of bytes written.
    public mutating func write(http2ErrorCode code: HTTP2ErrorCode) -> Int {
        return self.write(integer: UInt32(http2ErrorCode: code))
    }
}
{'fraction_non_alphanumeric': 0.10317301935741369, 'fraction_numerical': 0.020754340451007783, 'mean_word_length': 2.0735970561177552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7470438', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2541, 'n_words': 1004}