From 90ca7050852cb229b0039a6582efa19af46c5c34 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 17 Feb 2024 18:02:44 +0000 Subject: [PATCH] swift format --- .../HBPerformance/EmbeddedApplication.swift | 2 +- .../Sources/HBPerformance/RunBenchmark.swift | 2 +- .../Request+Persist+async.swift | 8 +-- .../AsyncAwaitSupport/Router+async.swift | 36 ++++++------ .../Codable/CodableProtocols.swift | 2 +- Sources/Hummingbird/Configuration.swift | 8 +-- .../Hummingbird/Extensions/Extensions.swift | 4 +- Sources/Hummingbird/HTTP/MediaType.swift | 4 +- Sources/Hummingbird/HTTP/URL.swift | 2 +- .../Middleware/TracingMiddleware.swift | 2 +- .../Hummingbird/Router/RouterBuilder.swift | 8 +-- Sources/Hummingbird/Router/RouterGroup.swift | 8 +-- .../Hummingbird/Router/RouterMethods.swift | 56 +++++++++---------- Sources/Hummingbird/Router/RouterPath.swift | 4 +- Sources/Hummingbird/Router/TrieRouter.swift | 2 +- Sources/Hummingbird/Server/Request.swift | 2 +- .../Hummingbird/Server/ResponsePatch.swift | 4 +- .../Storage/Application+Persist.swift | 4 +- .../Storage/MemoryPersistDriver.swift | 4 +- .../Hummingbird/Storage/PersistDriver.swift | 4 +- .../Hummingbird/Utils/FlatDictionary.swift | 4 +- Sources/Hummingbird/Utils/HBParser.swift | 4 +- .../Codable/JSON/JSONCoding.swift | 2 +- .../URLEncodedForm+Request.swift | 2 +- .../URLEncodedFormEncoder.swift | 8 +-- .../HummingbirdFoundation/Files/FileIO.swift | 2 +- .../Files/FileMiddleware.swift | 12 ++-- Sources/HummingbirdJobs/JobQueueWorker.swift | 2 +- .../HummingbirdXCT/HBXCTAsyncTesting.swift | 4 +- Sources/HummingbirdXCT/HBXCTEmbedded.swift | 2 +- .../URLEncodedForm/URLEncoderTests.swift | 2 +- Tests/HummingbirdTests/TestTracer.swift | 14 ++--- 32 files changed, 112 insertions(+), 112 deletions(-) diff --git a/Benchmarks/Sources/HBPerformance/EmbeddedApplication.swift b/Benchmarks/Sources/HBPerformance/EmbeddedApplication.swift index 177f9bff9..c3f039fa1 100644 --- a/Benchmarks/Sources/HBPerformance/EmbeddedApplication.swift +++ b/Benchmarks/Sources/HBPerformance/EmbeddedApplication.swift @@ -87,7 +87,7 @@ public struct HBEmbeddedApplication { // write request let requestHead = HTTPRequestHead(version: .init(major: 1, minor: 1), method: method, uri: uri, headers: headers) try writeInbound(.head(requestHead)) - if let body = body { + if let body { try self.writeInbound(.body(body)) } try self.writeInbound(.end(nil)) diff --git a/Benchmarks/Sources/HBPerformance/RunBenchmark.swift b/Benchmarks/Sources/HBPerformance/RunBenchmark.swift index 59334ecb9..516a58562 100644 --- a/Benchmarks/Sources/HBPerformance/RunBenchmark.swift +++ b/Benchmarks/Sources/HBPerformance/RunBenchmark.swift @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// import BenchmarkSupport -public func runBenchmark(benchmark: BenchmarkSupport.Benchmark, running: B) throws { +public func runBenchmark(benchmark: BenchmarkSupport.Benchmark, running: some BenchmarkWrapper) throws { try running.setUp() defer { running.tearDown() diff --git a/Sources/Hummingbird/AsyncAwaitSupport/Request+Persist+async.swift b/Sources/Hummingbird/AsyncAwaitSupport/Request+Persist+async.swift index 331bac5be..5a7e10f85 100644 --- a/Sources/Hummingbird/AsyncAwaitSupport/Request+Persist+async.swift +++ b/Sources/Hummingbird/AsyncAwaitSupport/Request+Persist+async.swift @@ -20,7 +20,7 @@ extension HBPersistDriver { /// - value: Codable value to store /// - expires: If non-nil defines time that value will expire /// - request: Request making this call - public func create(key: String, value: Object, expires: TimeAmount? = nil, request: HBRequest) async throws { + public func create(key: String, value: some Codable, expires: TimeAmount? = nil, request: HBRequest) async throws { try await self.create(key: key, value: value, expires: expires, request: request).get() } @@ -30,7 +30,7 @@ extension HBPersistDriver { /// - value: Codable value to store /// - expires: If non-nil defines time that value will expire /// - request: Request making this call - public func set(key: String, value: Object, expires: TimeAmount? = nil, request: HBRequest) async throws { + public func set(key: String, value: some Codable, expires: TimeAmount? = nil, request: HBRequest) async throws { try await self.set(key: key, value: value, expires: expires, request: request).get() } @@ -61,7 +61,7 @@ extension HBRequest.Persist { /// - key: key string /// - value: value /// - expires: time key/value pair will expire - public func create(key: String, value: Object, expires: TimeAmount? = nil) async throws { + public func create(key: String, value: some Codable, expires: TimeAmount? = nil) async throws { try await self.request.application.persist.driver.create(key: key, value: value, expires: expires, request: self.request) } @@ -70,7 +70,7 @@ extension HBRequest.Persist { /// - key: key string /// - value: value /// - expires: time key/value pair will expire - public func set(key: String, value: Object, expires: TimeAmount? = nil) async throws { + public func set(key: String, value: some Codable, expires: TimeAmount? = nil) async throws { try await self.request.application.persist.driver.set(key: key, value: value, expires: expires, request: self.request) } diff --git a/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift b/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift index d0d79a9b1..916e7d478 100644 --- a/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift +++ b/Sources/Hummingbird/AsyncAwaitSupport/Router+async.swift @@ -17,62 +17,62 @@ import NIOCore @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension HBRouterMethods { /// GET path for async closure returning type conforming to ResponseEncodable - @discardableResult public func get( + @discardableResult public func get( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .GET, options: options, use: handler) } /// PUT path for async closure returning type conforming to ResponseEncodable - @discardableResult public func put( + @discardableResult public func put( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .PUT, options: options, use: handler) } /// DELETE path for async closure returning type conforming to ResponseEncodable - @discardableResult public func delete( + @discardableResult public func delete( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .DELETE, options: options, use: handler) } /// HEAD path for async closure returning type conforming to ResponseEncodable - @discardableResult public func head( + @discardableResult public func head( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .HEAD, options: options, use: handler) } /// POST path for async closure returning type conforming to ResponseEncodable - @discardableResult public func post( + @discardableResult public func post( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .POST, options: options, use: handler) } /// PATCH path for async closure returning type conforming to ResponseEncodable - @discardableResult public func patch( + @discardableResult public func patch( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) async throws -> Output + use handler: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { return on(path, method: .PATCH, options: options, use: handler) } - public static func constructResponder( + public static func constructResponder( options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) async throws -> Output + use closure: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> HBResponder { return HBAsyncCallbackResponder { request in var request = request @@ -95,11 +95,11 @@ extension HBRouterMethods { @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension HBRouterBuilder { /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String, method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) async throws -> Output + use closure: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { let responder = Self.constructResponder(options: options, use: closure) add(path, method: method, responder: responder) @@ -110,11 +110,11 @@ extension HBRouterBuilder { @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension HBRouterGroup { /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String = "", method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) async throws -> Output + use closure: @escaping (HBRequest) async throws -> some HBResponseGenerator ) -> Self { let responder = Self.constructResponder(options: options, use: closure) let path = self.combinePaths(self.path, path) diff --git a/Sources/Hummingbird/Codable/CodableProtocols.swift b/Sources/Hummingbird/Codable/CodableProtocols.swift index d67951242..7b3319f0b 100644 --- a/Sources/Hummingbird/Codable/CodableProtocols.swift +++ b/Sources/Hummingbird/Codable/CodableProtocols.swift @@ -33,7 +33,7 @@ public protocol HBRequestDecoder { /// Default encoder. Outputs request with the swift string description of object struct NullEncoder: HBResponseEncoder { - func encode(_ value: T, from request: HBRequest) throws -> HBResponse { + func encode(_ value: some Encodable, from request: HBRequest) throws -> HBResponse { return HBResponse( status: .ok, headers: ["content-type": "text/plain; charset=utf-8"], diff --git a/Sources/Hummingbird/Configuration.swift b/Sources/Hummingbird/Configuration.swift index 4cab57349..dc03cfa68 100644 --- a/Sources/Hummingbird/Configuration.swift +++ b/Sources/Hummingbird/Configuration.swift @@ -110,7 +110,7 @@ extension HBApplication { self.threadPoolSize = threadPoolSize self.noHTTPServer = noHTTPServer - if let logLevel = logLevel { + if let logLevel { self.logLevel = logLevel } else if let logLevel = env.get("LOG_LEVEL") { self.logLevel = Logger.Level(rawValue: logLevel) ?? .info @@ -169,7 +169,7 @@ extension HBApplication { self.threadPoolSize = threadPoolSize self.noHTTPServer = noHTTPServer - if let logLevel = logLevel { + if let logLevel { self.logLevel = logLevel } else if let logLevel = env.get("LOG_LEVEL") { self.logLevel = Logger.Level(rawValue: logLevel) ?? .info @@ -223,7 +223,7 @@ extension HBApplication { self.threadPoolSize = threadPoolSize self.noHTTPServer = noHTTPServer - if let logLevel = logLevel { + if let logLevel { self.logLevel = logLevel } else if let logLevel = env.get("LOG_LEVEL") { self.logLevel = Logger.Level(rawValue: logLevel) ?? .info @@ -277,7 +277,7 @@ extension HBApplication { self.threadPoolSize = threadPoolSize self.noHTTPServer = noHTTPServer - if let logLevel = logLevel { + if let logLevel { self.logLevel = logLevel } else if let logLevel = env.get("LOG_LEVEL") { self.logLevel = Logger.Level(rawValue: logLevel) ?? .info diff --git a/Sources/Hummingbird/Extensions/Extensions.swift b/Sources/Hummingbird/Extensions/Extensions.swift index edecf159c..0c78945f0 100644 --- a/Sources/Hummingbird/Extensions/Extensions.swift +++ b/Sources/Hummingbird/Extensions/Extensions.swift @@ -59,7 +59,7 @@ public struct HBExtensions { /// Return if extension has been set @inlinable - public func exists(_ key: KeyPath) -> Bool { + public func exists(_ key: KeyPath) -> Bool { self.items[key.hashValue]?.value != nil } @@ -146,7 +146,7 @@ public struct HBSendableExtensions: Sendable { /// Return if extension has been set @inlinable - public func exists(_ key: KeyPath) -> Bool { + public func exists(_ key: KeyPath) -> Bool { self.items[key.hashValue]?.value != nil } diff --git a/Sources/Hummingbird/HTTP/MediaType.swift b/Sources/Hummingbird/HTTP/MediaType.swift index 18eabb0e9..a169eaeed 100644 --- a/Sources/Hummingbird/HTTP/MediaType.swift +++ b/Sources/Hummingbird/HTTP/MediaType.swift @@ -97,8 +97,8 @@ public struct HBMediaType: Sendable, CustomStringConvertible { break } } - if let category = category, - let subCategory = subCategory + if let category, + let subCategory { self.type = category self.subType = subCategory.lowercased() diff --git a/Sources/Hummingbird/HTTP/URL.swift b/Sources/Hummingbird/HTTP/URL.swift index bdcd9ea18..2fa10eb1d 100644 --- a/Sources/Hummingbird/HTTP/URL.swift +++ b/Sources/Hummingbird/HTTP/URL.swift @@ -139,7 +139,7 @@ extension HBParameters { /// Initialize parameters from parser struct /// - Parameter query: parser holding query strings init(fromQuery query: HBParser?) { - guard var query = query else { + guard var query else { self.parameters = .init() return } diff --git a/Sources/Hummingbird/Middleware/TracingMiddleware.swift b/Sources/Hummingbird/Middleware/TracingMiddleware.swift index 51d86acaf..4999a30a5 100644 --- a/Sources/Hummingbird/Middleware/TracingMiddleware.swift +++ b/Sources/Hummingbird/Middleware/TracingMiddleware.swift @@ -28,7 +28,7 @@ public struct HBTracingMiddleware: HBMiddleware { /// /// - Parameter recordingHeaders: A list of HTTP header names to be recorded as span attributes. By default, no headers /// are being recorded. - public init(recordingHeaders headerNamesToRecord: C) where C.Element == String { + public init(recordingHeaders headerNamesToRecord: some Collection) { self.headerNamesToRecord = Set(headerNamesToRecord.map(RecordingHeader.init)) } diff --git a/Sources/Hummingbird/Router/RouterBuilder.swift b/Sources/Hummingbird/Router/RouterBuilder.swift index e272fe8be..53c9c5a82 100644 --- a/Sources/Hummingbird/Router/RouterBuilder.swift +++ b/Sources/Hummingbird/Router/RouterBuilder.swift @@ -77,11 +77,11 @@ public final class HBRouterBuilder: HBRouterMethods { } /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String, method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) throws -> Output + use closure: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { let responder = Self.constructResponder(options: options, use: closure) self.add(path, method: method, responder: responder) @@ -89,11 +89,11 @@ public final class HBRouterBuilder: HBRouterMethods { } /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String, method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) -> EventLoopFuture + use closure: @escaping (HBRequest) -> EventLoopFuture ) -> Self { let responder = Self.constructResponder(options: options, use: closure) self.add(path, method: method, responder: responder) diff --git a/Sources/Hummingbird/Router/RouterGroup.swift b/Sources/Hummingbird/Router/RouterGroup.swift index 06c5a56de..6c7404921 100644 --- a/Sources/Hummingbird/Router/RouterGroup.swift +++ b/Sources/Hummingbird/Router/RouterGroup.swift @@ -58,11 +58,11 @@ public struct HBRouterGroup: HBRouterMethods { } /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String = "", method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) throws -> Output + use closure: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { let responder = Self.constructResponder(options: options, use: closure) let path = self.combinePaths(self.path, path) @@ -71,11 +71,11 @@ public struct HBRouterGroup: HBRouterMethods { } /// Add path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func on( + @discardableResult public func on( _ path: String = "", method: HTTPMethod, options: HBRouterMethodOptions = [], - use closure: @escaping (HBRequest) -> EventLoopFuture + use closure: @escaping (HBRequest) -> EventLoopFuture ) -> Self { let responder = Self.constructResponder(options: options, use: closure) let path = self.combinePaths(self.path, path) diff --git a/Sources/Hummingbird/Router/RouterMethods.swift b/Sources/Hummingbird/Router/RouterMethods.swift index 775365a2c..325f47873 100644 --- a/Sources/Hummingbird/Router/RouterMethods.swift +++ b/Sources/Hummingbird/Router/RouterMethods.swift @@ -61,118 +61,118 @@ public protocol HBRouterMethods { extension HBRouterMethods { /// GET path for closure returning type conforming to HBResponseGenerator - @discardableResult public func get( + @discardableResult public func get( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .GET, options: options, use: handler) } /// PUT path for closure returning type conforming to HBResponseGenerator - @discardableResult public func put( + @discardableResult public func put( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .PUT, options: options, use: handler) } /// POST path for closure returning type conforming to HBResponseGenerator - @discardableResult public func post( + @discardableResult public func post( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .POST, options: options, use: handler) } /// HEAD path for closure returning type conforming to HBResponseGenerator - @discardableResult public func head( + @discardableResult public func head( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .HEAD, options: options, use: handler) } /// DELETE path for closure returning type conforming to HBResponseGenerator - @discardableResult public func delete( + @discardableResult public func delete( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .DELETE, options: options, use: handler) } /// PATCH path for closure returning type conforming to HBResponseGenerator - @discardableResult public func patch( + @discardableResult public func patch( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) throws -> Output + use handler: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> Self { return on(path, method: .PATCH, options: options, use: handler) } /// GET path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func get( + @discardableResult public func get( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .GET, options: options, use: handler) } /// PUT path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func put( + @discardableResult public func put( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .PUT, options: options, use: handler) } /// DELETE path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func delete( + @discardableResult public func delete( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .DELETE, options: options, use: handler) } /// HEAD path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func head( + @discardableResult public func head( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .HEAD, options: options, use: handler) } /// POST path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func post( + @discardableResult public func post( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .POST, options: options, use: handler) } /// PATCH path for closure returning type conforming to ResponseFutureEncodable - @discardableResult public func patch( + @discardableResult public func patch( _ path: String = "", options: HBRouterMethodOptions = [], - use handler: @escaping (HBRequest) -> EventLoopFuture + use handler: @escaping (HBRequest) -> EventLoopFuture ) -> Self { return on(path, method: .PATCH, options: options, use: handler) } } extension HBRouterMethods { - public static func constructResponder( + public static func constructResponder( options: HBRouterMethodOptions, - use closure: @escaping (HBRequest) throws -> Output + use closure: @escaping (HBRequest) throws -> some HBResponseGenerator ) -> HBResponder { // generate response from request. Moved repeated code into internal function func _respond(request: HBRequest) throws -> HBResponse { @@ -219,9 +219,9 @@ extension HBRouterMethods { } } - public static func constructResponder( + public static func constructResponder( options: HBRouterMethodOptions, - use closure: @escaping (HBRequest) -> EventLoopFuture + use closure: @escaping (HBRequest) -> EventLoopFuture ) -> HBResponder { // generate response from request. Moved repeated code into internal function func _respond(request: HBRequest) -> EventLoopFuture { diff --git a/Sources/Hummingbird/Router/RouterPath.swift b/Sources/Hummingbird/Router/RouterPath.swift index ec3e42560..ff93315a2 100644 --- a/Sources/Hummingbird/Router/RouterPath.swift +++ b/Sources/Hummingbird/Router/RouterPath.swift @@ -25,7 +25,7 @@ struct RouterPath: ExpressibleByStringLiteral { case recursiveWildcard case null - static func ~= (lhs: Element, rhs: S) -> Bool { + static func ~= (lhs: Element, rhs: some StringProtocol) -> Bool { switch lhs { case .path(let lhs): return lhs == rhs @@ -48,7 +48,7 @@ struct RouterPath: ExpressibleByStringLiteral { } } - static func == (lhs: Element, rhs: S) -> Bool { + static func == (lhs: Element, rhs: some StringProtocol) -> Bool { switch lhs { case .path(let lhs): return lhs == rhs diff --git a/Sources/Hummingbird/Router/TrieRouter.swift b/Sources/Hummingbird/Router/TrieRouter.swift index f175edad6..dd968e3e0 100644 --- a/Sources/Hummingbird/Router/TrieRouter.swift +++ b/Sources/Hummingbird/Router/TrieRouter.swift @@ -97,7 +97,7 @@ struct RouterPathTrie { } } -extension Optional where Wrapped == HBParameters { +extension HBParameters? { mutating func set(_ s: Substring, value: Substring) { switch self { case .some(var parameters): diff --git a/Sources/Hummingbird/Server/Request.swift b/Sources/Hummingbird/Server/Request.swift index fcae3b2c6..4d05020eb 100644 --- a/Sources/Hummingbird/Server/Request.swift +++ b/Sources/Hummingbird/Server/Request.swift @@ -28,7 +28,7 @@ private extension CodingKey { } } -private extension Array where Element == CodingKey { +private extension [CodingKey] { /// returns a path key using a dot character as a separator var pathKeyValue: String { map(\.pathKeyValue).joined(separator: ".") diff --git a/Sources/Hummingbird/Server/ResponsePatch.swift b/Sources/Hummingbird/Server/ResponsePatch.swift index 41243583d..5ece74ae2 100644 --- a/Sources/Hummingbird/Server/ResponsePatch.swift +++ b/Sources/Hummingbird/Server/ResponsePatch.swift @@ -62,7 +62,7 @@ extension HBRequest { extension HBResponse { /// apply `HBRequest.ResponsePatch` to `HBResponse` mutating func apply(patch: HBRequest.ResponsePatch?) -> Self { - guard let patch = patch else { return self } + guard let patch else { return self } if let status = patch.status { self.status = status } @@ -108,7 +108,7 @@ public struct HTTPHeadersPatch: ExpressibleByDictionaryLiteral { /// the header should be an ASCII string. For future-proofing with HTTP/2 lowercase header /// names are strongly recommended. @inlinable - public mutating func add(contentsOf other: S) where S.Element == (String, String) { + public mutating func add(contentsOf other: some Sequence<(String, String)>) { self.addHeaders.add(contentsOf: other) } diff --git a/Sources/Hummingbird/Storage/Application+Persist.swift b/Sources/Hummingbird/Storage/Application+Persist.swift index a832f234a..2e514610b 100644 --- a/Sources/Hummingbird/Storage/Application+Persist.swift +++ b/Sources/Hummingbird/Storage/Application+Persist.swift @@ -54,7 +54,7 @@ extension HBRequest { /// - value: value /// - expires: time key/value pair will expire /// - Returns: EventLoopFuture for when value has been set - public func create(key: String, value: Object, expires: TimeAmount? = nil) -> EventLoopFuture { + public func create(key: String, value: some Codable, expires: TimeAmount? = nil) -> EventLoopFuture { return self.request.application.persist.driver.create(key: key, value: value, expires: expires, request: self.request) } @@ -64,7 +64,7 @@ extension HBRequest { /// - value: value /// - expires: time key/value pair will expire /// - Returns: EventLoopFuture for when value has been set - public func set(key: String, value: Object, expires: TimeAmount? = nil) -> EventLoopFuture { + public func set(key: String, value: some Codable, expires: TimeAmount? = nil) -> EventLoopFuture { return self.request.application.persist.driver.set(key: key, value: value, expires: expires, request: self.request) } diff --git a/Sources/Hummingbird/Storage/MemoryPersistDriver.swift b/Sources/Hummingbird/Storage/MemoryPersistDriver.swift index b685e9d8f..fa44626af 100644 --- a/Sources/Hummingbird/Storage/MemoryPersistDriver.swift +++ b/Sources/Hummingbird/Storage/MemoryPersistDriver.swift @@ -33,14 +33,14 @@ public final class HBMemoryPersistDriver: HBPersistDriver { self.task?.cancel() } - public func create(key: String, value: Object, expires: TimeAmount?, request: HBRequest) -> EventLoopFuture { + public func create(key: String, value: some Codable, expires: TimeAmount?, request: HBRequest) -> EventLoopFuture { return self.eventLoop.submit { guard self.values[key] == nil else { throw HBPersistError.duplicate } self.values[key] = .init(value: value, expires: expires) } } - public func set(key: String, value: Object, expires: TimeAmount?, request: HBRequest) -> EventLoopFuture { + public func set(key: String, value: some Codable, expires: TimeAmount?, request: HBRequest) -> EventLoopFuture { return self.eventLoop.submit { self.values[key] = .init(value: value, expires: expires) } diff --git a/Sources/Hummingbird/Storage/PersistDriver.swift b/Sources/Hummingbird/Storage/PersistDriver.swift index 0031be5fc..63bfcc645 100644 --- a/Sources/Hummingbird/Storage/PersistDriver.swift +++ b/Sources/Hummingbird/Storage/PersistDriver.swift @@ -56,7 +56,7 @@ extension HBPersistDriver { /// - key: Key to store value against /// - value: Codable value to store /// - request: Request making this call - func create(key: String, value: Object, request: HBRequest) -> EventLoopFuture { + func create(key: String, value: some Codable, request: HBRequest) -> EventLoopFuture { self.create(key: key, value: value, expires: nil, request: request) } @@ -66,7 +66,7 @@ extension HBPersistDriver { /// - value: Codable value to store /// - expires: If non-nil defines time that value will expire /// - request: Request making this call - func set(key: String, value: Object, request: HBRequest) -> EventLoopFuture { + func set(key: String, value: some Codable, request: HBRequest) -> EventLoopFuture { self.set(key: key, value: value, expires: nil, request: request) } } diff --git a/Sources/Hummingbird/Utils/FlatDictionary.swift b/Sources/Hummingbird/Utils/FlatDictionary.swift index d6da0dced..78296b5aa 100644 --- a/Sources/Hummingbird/Utils/FlatDictionary.swift +++ b/Sources/Hummingbird/Utils/FlatDictionary.swift @@ -75,13 +75,13 @@ public struct FlatDictionary: Collection, ExpressibleByDic set { let hashKey = Self.hashKey(key) if let index = hashKeys.firstIndex(of: hashKey) { - if let newValue = newValue { + if let newValue { self.elements[index].value = newValue } else { self.elements.remove(at: index) self.hashKeys.remove(at: index) } - } else if let newValue = newValue { + } else if let newValue { self.elements.append((key: key, value: newValue)) self.hashKeys.append(hashKey) } diff --git a/Sources/Hummingbird/Utils/HBParser.swift b/Sources/Hummingbird/Utils/HBParser.swift index cbe196198..557d7c1ce 100644 --- a/Sources/Hummingbird/Utils/HBParser.swift +++ b/Sources/Hummingbird/Utils/HBParser.swift @@ -28,7 +28,7 @@ public struct HBParser: Sendable { /// Create a Parser object /// - Parameter string: UTF8 data to parse - public init?(_ utf8Data: Bytes, validateUTF8: Bool = true) where Bytes.Element == UInt8 { + public init?(_ utf8Data: some Collection, validateUTF8: Bool = true) { if let buffer = utf8Data as? [UInt8] { self.buffer = buffer } else { @@ -663,7 +663,7 @@ extension Unicode.Scalar { } } -extension Set where Element == Unicode.Scalar { +extension Set { public init(_ string: String) { self = Set(string.unicodeScalars) } diff --git a/Sources/HummingbirdFoundation/Codable/JSON/JSONCoding.swift b/Sources/HummingbirdFoundation/Codable/JSON/JSONCoding.swift index 9939f5fcc..a48116908 100644 --- a/Sources/HummingbirdFoundation/Codable/JSON/JSONCoding.swift +++ b/Sources/HummingbirdFoundation/Codable/JSON/JSONCoding.swift @@ -22,7 +22,7 @@ extension JSONEncoder: HBResponseEncoder { /// - Parameters: /// - value: Value to encode /// - request: Request used to generate response - public func encode(_ value: T, from request: HBRequest) throws -> HBResponse { + public func encode(_ value: some Encodable, from request: HBRequest) throws -> HBResponse { var buffer = request.allocator.buffer(capacity: 0) let data = try self.encode(value) buffer.writeBytes(data) diff --git a/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedForm+Request.swift b/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedForm+Request.swift index ba4c5e98d..d3282201b 100644 --- a/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedForm+Request.swift +++ b/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedForm+Request.swift @@ -19,7 +19,7 @@ extension URLEncodedFormEncoder: HBResponseEncoder { /// - Parameters: /// - value: Value to encode /// - request: Request used to generate response - public func encode(_ value: T, from request: HBRequest) throws -> HBResponse { + public func encode(_ value: some Encodable, from request: HBRequest) throws -> HBResponse { var buffer = request.allocator.buffer(capacity: 0) let string = try self.encode(value) buffer.writeString(string) diff --git a/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedFormEncoder.swift b/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedFormEncoder.swift index 5dbeda34c..d87c23d9f 100644 --- a/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedFormEncoder.swift +++ b/Sources/HummingbirdFoundation/Codable/URLEncodedForm/URLEncodedFormEncoder.swift @@ -75,7 +75,7 @@ public struct URLEncodedFormEncoder { /// - Parameters: /// - value: Value to encode /// - Returns: URL encoded form data - public func encode(_ value: T) throws -> String { + public func encode(_ value: some Encodable) throws -> String { let encoder = _URLEncodedFormEncoder(options: options) try value.encode(to: encoder) guard let result = encoder.result else { @@ -158,7 +158,7 @@ private class _URLEncodedFormEncoder: Encoder { mutating func encode(_ value: UInt32, forKey key: Key) throws { self.encode(value, key: key.stringValue) } mutating func encode(_ value: UInt64, forKey key: Key) throws { self.encode(value, key: key.stringValue) } - mutating func encode(_ value: T, forKey key: Key) throws { + mutating func encode(_ value: some Encodable, forKey key: Key) throws { self.encoder.codingPath.append(key) defer { self.encoder.codingPath.removeLast() } @@ -238,7 +238,7 @@ private class _URLEncodedFormEncoder: Encoder { mutating func encode(_ value: UInt32) throws { self.encodeResult(value) } mutating func encode(_ value: UInt64) throws { self.encodeResult(value) } - mutating func encode(_ value: T) throws { + mutating func encode(_ value: some Encodable) throws { self.count += 1 self.encoder.codingPath.append(URLEncodedForm.Key(index: self.count)) @@ -304,7 +304,7 @@ extension _URLEncodedFormEncoder: SingleValueEncodingContainer { func encode(_ value: UInt32) throws { self.encodeResult(value) } func encode(_ value: UInt64) throws { self.encodeResult(value) } - func encode(_ value: T) throws { + func encode(_ value: some Encodable) throws { try value.encode(to: self) } diff --git a/Sources/HummingbirdFoundation/Files/FileIO.swift b/Sources/HummingbirdFoundation/Files/FileIO.swift index e70d65948..a6a43acdf 100644 --- a/Sources/HummingbirdFoundation/Files/FileIO.swift +++ b/Sources/HummingbirdFoundation/Files/FileIO.swift @@ -111,7 +111,7 @@ public struct HBFileIO { let futureResult: EventLoopFuture switch contents { case .byteBuffer(let buffer): - guard let buffer = buffer else { return context.eventLoop.makeSucceededVoidFuture() } + guard let buffer else { return context.eventLoop.makeSucceededVoidFuture() } futureResult = self.writeFile(buffer: buffer, handle: handle, on: context.eventLoop) case .stream(let streamer): futureResult = self.writeFile(stream: streamer, handle: handle, on: context.eventLoop) diff --git a/Sources/HummingbirdFoundation/Files/FileMiddleware.swift b/Sources/HummingbirdFoundation/Files/FileMiddleware.swift index b70c4c36b..4d4bea1d3 100644 --- a/Sources/HummingbirdFoundation/Files/FileMiddleware.swift +++ b/Sources/HummingbirdFoundation/Files/FileMiddleware.swift @@ -114,12 +114,12 @@ public struct HBFileMiddleware: HBMiddleware { var headers = HTTPHeaders() // content-length - if let contentSize = contentSize { + if let contentSize { headers.add(name: "content-length", value: String(describing: contentSize)) } // modified-date var modificationDateString: String? - if let modificationDate = modificationDate { + if let modificationDate { modificationDateString = HBDateCache.rfc1123Formatter.string(from: modificationDate) headers.add(name: "modified-date", value: modificationDateString!) } @@ -150,7 +150,7 @@ public struct HBFileMiddleware: HBMiddleware { } // verify if-modified-since else if let ifModifiedSince = request.headers["if-modified-since"].first, - let modificationDate = modificationDate + let modificationDate { if let ifModifiedSinceDate = HBDateCache.rfc1123Formatter.date(from: ifModifiedSince) { // round modification date of file down to seconds for comparison @@ -170,7 +170,7 @@ public struct HBFileMiddleware: HBMiddleware { if let ifRange = request.headers["if-range"].first, ifRange != headers["eTag"].first, ifRange != headers["modified-date"].first { // do nothing and drop down to returning full file } else { - if let contentSize = contentSize { + if let contentSize { let lowerBound = max(range.lowerBound, 0) let upperBound = min(range.upperBound, contentSize - 1) headers.replaceOrAdd(name: "content-range", value: "bytes \(lowerBound)-\(upperBound)/\(contentSize)") @@ -188,7 +188,7 @@ public struct HBFileMiddleware: HBMiddleware { case .loadFile(let headers, let range): switch request.method { case .GET: - if let range = range { + if let range { return self.fileIO.loadFile(path: fullPath.relativePath, range: range, context: request.context, logger: request.logger) .map { body, _ in return HBResponse(status: .partialContent, headers: headers, body: body) @@ -276,7 +276,7 @@ extension HBFileMiddleware { } } -extension Sequence where Element == UInt8 { +extension Sequence { /// return a hexEncoded string buffer from an array of bytes func hexDigest() -> String { return self.map { String(format: "%02x", $0) }.joined(separator: "") diff --git a/Sources/HummingbirdJobs/JobQueueWorker.swift b/Sources/HummingbirdJobs/JobQueueWorker.swift index e242d0be0..a688115d6 100644 --- a/Sources/HummingbirdJobs/JobQueueWorker.swift +++ b/Sources/HummingbirdJobs/JobQueueWorker.swift @@ -94,7 +94,7 @@ class HBJobQueueWorker { } return self.queue.pop(on: eventLoop) .map { value in - if let value = value { + if let value { promise.succeed(value) task.cancel() } diff --git a/Sources/HummingbirdXCT/HBXCTAsyncTesting.swift b/Sources/HummingbirdXCT/HBXCTAsyncTesting.swift index 42fa65769..3d4e8d173 100644 --- a/Sources/HummingbirdXCT/HBXCTAsyncTesting.swift +++ b/Sources/HummingbirdXCT/HBXCTAsyncTesting.swift @@ -59,7 +59,7 @@ struct HBXCTAsyncTesting: HBXCT { // shutdown eventloop try await withCheckedThrowingContinuation { (cont: CheckedContinuation) in self.asyncTestingEventLoop.shutdownGracefully { error in - if let error = error { + if let error { cont.resume(throwing: error) } else { cont.resume() @@ -95,7 +95,7 @@ struct HBXCTAsyncTesting: HBXCT { // write request let requestHead = HTTPRequestHead(version: .init(major: 1, minor: 1), method: method, uri: uri, headers: headers) try await writeInbound(.head(requestHead)) - if let body = body { + if let body { try await self.writeInbound(.body(body)) } try await self.writeInbound(.end(nil)) diff --git a/Sources/HummingbirdXCT/HBXCTEmbedded.swift b/Sources/HummingbirdXCT/HBXCTEmbedded.swift index 0d5795d42..2d3249ebe 100644 --- a/Sources/HummingbirdXCT/HBXCTEmbedded.swift +++ b/Sources/HummingbirdXCT/HBXCTEmbedded.swift @@ -53,7 +53,7 @@ struct HBXCTEmbedded: HBXCT { // write request let requestHead = HTTPRequestHead(version: .init(major: 1, minor: 1), method: method, uri: uri, headers: headers) try writeInbound(.head(requestHead)) - if let body = body { + if let body { try self.writeInbound(.body(body)) } try self.writeInbound(.end(nil)) diff --git a/Tests/HummingbirdFoundationTests/URLEncodedForm/URLEncoderTests.swift b/Tests/HummingbirdFoundationTests/URLEncodedForm/URLEncoderTests.swift index ed35c6bd0..4a76fa115 100644 --- a/Tests/HummingbirdFoundationTests/URLEncodedForm/URLEncoderTests.swift +++ b/Tests/HummingbirdFoundationTests/URLEncodedForm/URLEncoderTests.swift @@ -26,7 +26,7 @@ class URLEncodedFormEncoderTests: XCTestCase { XCTAssertEqual(lhs, rhs) } - func testForm(_ value: Input, query: String, encoder: URLEncodedFormEncoder = .init()) { + func testForm(_ value: some Encodable, query: String, encoder: URLEncodedFormEncoder = .init()) { do { let query2 = try encoder.encode(value) Self.XCTAssertEncodedEqual(query2, query) diff --git a/Tests/HummingbirdTests/TestTracer.swift b/Tests/HummingbirdTests/TestTracer.swift index cacd79d2c..da859a0a2 100644 --- a/Tests/HummingbirdTests/TestTracer.swift +++ b/Tests/HummingbirdTests/TestTracer.swift @@ -36,11 +36,11 @@ final class TestTracer: Tracer { private(set) var spans = [TestSpan]() var onEndSpan: (TestSpan) -> Void = { _ in } - func startSpan( + func startSpan( _ operationName: String, context: @autoclosure () -> ServiceContext, ofKind kind: SpanKind, - at instant: @autoclosure () -> Instant, + at instant: @autoclosure () -> some TracerInstant, function: String, file fileID: String, line: UInt @@ -137,9 +137,9 @@ final class TestSpan: Span { let onEnd: (TestSpan) -> Void - init( + init( operationName: String, - at instant: Instant, + at instant: some TracerInstant, context: ServiceContext, kind: SpanKind, onEnd: @escaping (TestSpan) -> Void @@ -164,15 +164,15 @@ final class TestSpan: Span { self.events.append(event) } - func recordError( + func recordError( _ error: Error, attributes: SpanAttributes, - at instant: @autoclosure () -> Instant + at instant: @autoclosure () -> some TracerInstant ) { self.recordedErrors.append((error, attributes)) } - func end(at instant: @autoclosure () -> Instant) { + func end(at instant: @autoclosure () -> some TracerInstant) { self.endTime = instant().millisecondsSinceEpoch self.onEnd(self) }