Skip to content

Commit

Permalink
Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
philippzagar committed Dec 6, 2023
1 parent 00203ad commit 9e0eb5c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
46 changes: 0 additions & 46 deletions Sources/Spezi/Dependencies/DependencyBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,6 @@
// SPDX-License-Identifier: MIT
//

/// A protocol enabling the implementation of a result builder to build a ``DependencyCollection``.
/// Enables the simple construction of a result builder accepting ``Module``s with additional type constraints (useful for DSL implementations).
///
/// Upon conformance, developers are required to implement a single result builder function transforming an arbitrary ``Module`` type constraint (M in the example below) to a ``DependencyCollection``.
/// All other result builder functions for constructing a ``DependencyCollection`` are provided as a default protocol implementation.
/// ```swift
/// static func buildExpression<M: Module>(_ expression: @escaping @autoclosure () -> M) -> DependencyCollection
/// ```
public protocol DependencyCollectionBuilder {}


/// Default protocol implementations of a result builder constructing a ``DependencyCollection``.
extension DependencyCollectionBuilder {
/// Build a block of ``DependencyCollection``s.
public static func buildBlock(_ components: DependencyCollection...) -> DependencyCollection {
buildArray(components)
}

/// Build the first block of an conditional ``DependencyCollection`` component.
public static func buildEither(first component: DependencyCollection) -> DependencyCollection {
component
}

/// Build the second block of an conditional ``DependencyCollection`` component.
public static func buildEither(second component: DependencyCollection) -> DependencyCollection {
component
}

/// Build an optional ``DependencyCollection`` component.
public static func buildOptional(_ component: DependencyCollection?) -> DependencyCollection {
component ?? DependencyCollection()
}

/// Build an ``DependencyCollection`` component with limited availability.
public static func buildLimitedAvailability(_ component: DependencyCollection) -> DependencyCollection {
component
}

/// Build an array of ``DependencyCollection`` components.
public static func buildArray(_ components: [DependencyCollection]) -> DependencyCollection {
DependencyCollection(components.reduce(into: []) { result, component in
result.append(contentsOf: component.entries)
})
}
}


/// A result builder to build a ``DependencyCollection``.
@resultBuilder
Expand Down
54 changes: 54 additions & 0 deletions Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//


/// A protocol enabling the implementation of a result builder to build a ``DependencyCollection``.
/// Enables the simple construction of a result builder accepting ``Module``s with additional type constraints (useful for DSL implementations).
///
/// Upon conformance, developers are required to implement a single result builder function transforming an arbitrary ``Module`` type constraint (M in the example below) to a ``DependencyCollection``.
/// All other result builder functions for constructing a ``DependencyCollection`` are provided as a default protocol implementation.
/// ```swift
/// static func buildExpression<M: Module>(_ expression: @escaping @autoclosure () -> M) -> DependencyCollection
/// ```
public protocol DependencyCollectionBuilder {}


/// Default protocol implementations of a result builder constructing a ``DependencyCollection``.
extension DependencyCollectionBuilder {
/// Build a block of ``DependencyCollection``s.
public static func buildBlock(_ components: DependencyCollection...) -> DependencyCollection {
buildArray(components)
}

/// Build the first block of an conditional ``DependencyCollection`` component.
public static func buildEither(first component: DependencyCollection) -> DependencyCollection {
component
}

Check warning on line 31 in Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift#L29-L31

Added lines #L29 - L31 were not covered by tests

/// Build the second block of an conditional ``DependencyCollection`` component.
public static func buildEither(second component: DependencyCollection) -> DependencyCollection {
component
}

Check warning on line 36 in Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift#L34-L36

Added lines #L34 - L36 were not covered by tests

/// Build an optional ``DependencyCollection`` component.
public static func buildOptional(_ component: DependencyCollection?) -> DependencyCollection {
component ?? DependencyCollection()
}

Check warning on line 41 in Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift#L39-L41

Added lines #L39 - L41 were not covered by tests

/// Build an ``DependencyCollection`` component with limited availability.
public static func buildLimitedAvailability(_ component: DependencyCollection) -> DependencyCollection {
component
}

Check warning on line 46 in Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Dependencies/DependencyCollectionBuilder.swift#L44-L46

Added lines #L44 - L46 were not covered by tests

/// Build an array of ``DependencyCollection`` components.
public static func buildArray(_ components: [DependencyCollection]) -> DependencyCollection {
DependencyCollection(components.reduce(into: []) { result, component in
result.append(contentsOf: component.entries)
})
}
}

0 comments on commit 9e0eb5c

Please sign in to comment.