Improved the initializers for the DocCMiddleware type in the library target.

This commit is contained in:
2025-09-25 02:41:52 +02:00
parent 36f14cfb51
commit edeaf219a0
2 changed files with 69 additions and 29 deletions
@@ -10,20 +10,21 @@
//
// ===----------------------------------------------------------------------===
import Hummingbird
import protocol Hummingbird.FileProvider
import struct Hummingbird.LocalFileSystem
import struct Logging.Logger
/// A middleware that proxies requests to `DocC` documentation containers within a hosting app.
public struct DocCMiddleware {
public struct DocCMiddleware<FileSystemProvider: FileProvider> {
// MARK: Properties
/// A type that contains the parameters to configure the middleware.
let configuration: Configuration
/// A protocol that defines file system interactions.
let fileProvider: any FileProvider
/// A type that conforms to a protocol that defines file system interactions.
let fileProvider: FileSystemProvider
/// A type that interacts with the logging system.
let logger: Logger
@@ -33,27 +34,44 @@ public struct DocCMiddleware {
// MARK: Initializers
/// Initializes this middleware with the root path to the `DocC` documentation containers in the file system.
/// Initializes this middleware.
/// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
init(
public init(
configuration: Configuration,
logger: Logger,
fileProvider: (any FileProvider)? = nil
) {
self.configuration = configuration
self.fileProvider = if let fileProvider {
fileProvider
} else {
LocalFileSystem(
logger: Logger
) where FileSystemProvider == LocalFileSystem {
self.init(
configuration: configuration,
fileProvider: LocalFileSystem(
rootFolder: configuration.folderRoot,
threadPool: configuration.threadPool,
logger: logger
)
}
),
logger: logger,
)
}
/// Initializes this middleware with a concrete file provider type.
/// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions.
/// - logger: A type that interacts with the logging system.
init(
configuration: Configuration,
fileProvider: FileSystemProvider,
logger: Logger,
) {
self.logger = logger
self.configuration = configuration
self.fileProvider = fileProvider
self.prepareURIPath = .init(uriRoot: configuration.uriRoot)
self.redirectURI = .init(logger: logger)
self.serveURI = .init(
fileProvider: fileProvider,
logger: logger
)
}
}
@@ -72,22 +72,48 @@ private extension DocCMiddlewareTests {
// MARK: Functions
/// Asserts the initialization of a `DocCMiddleware` type.
/// Asserts the public initializer.
/// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
func assertInit(
configuration: DocCMiddleware.Configuration,
logger: Logger = .test(),
fileProvider: (any FileProvider)? = nil
configuration: DocCMiddleware<LocalFileSystem>.Configuration,
logger: Logger = .test()
) {
// GIVEN
// WHEN
let middleware = DocCMiddleware(
configuration: configuration,
logger: logger,
fileProvider: fileProvider
logger: logger
)
// THEN
#expect(middleware.configuration.folderRoot == configuration.folderRoot)
#expect(middleware.configuration.uriRoot == configuration.uriRoot)
#expect(middleware.configuration.threadPool === configuration.threadPool)
#expect(middleware.logger.label == logger.label)
#expect(middleware.logger.logLevel == logger.logLevel)
#expect(type(of:middleware.fileProvider) == LocalFileSystem.self)
}
/// Asserts the internal initializer with a concrete file provider type.
/// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
func assertInit<FileSystemProvider: FileProvider>(
configuration: DocCMiddleware<FileSystemProvider>.Configuration,
logger: Logger = .test(),
fileProvider: FileSystemProvider
) {
// GIVEN
// WHEN
let middleware = DocCMiddleware(
configuration: configuration,
fileProvider: fileProvider,
logger: logger
)
// THEN
@@ -98,11 +124,7 @@ private extension DocCMiddlewareTests {
#expect(middleware.logger.label == logger.label)
#expect(middleware.logger.logLevel == logger.logLevel)
if let fileProvider {
#expect(type(of:middleware.fileProvider) == type(of: fileProvider))
} else {
#expect(middleware.fileProvider is LocalFileSystem)
}
#expect(type(of:middleware.fileProvider) == FileSystemProvider.self)
}
}