1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// InlineContextTransformer.swift
|
|
4 |
// https://github.com/ekazaev/route-composer
|
|
5 |
//
|
|
6 |
// Created by Eugene Kazaev in 2018-2022.
|
|
7 |
// Distributed under the MIT license.
|
|
8 |
//
|
|
9 |
// Become a sponsor:
|
|
10 |
// https://github.com/sponsors/ekazaev
|
|
11 |
//
|
|
12 |
|
|
13 |
import Foundation
|
|
14 |
|
|
15 |
/// `InlineContextTransformer`
|
|
16 |
///
|
|
17 |
/// **NB:** It may be used for the purpose of configuration testing, but then replaced with a strongly typed
|
|
18 |
/// `ContextTransformer` instance.
|
|
19 |
public final class InlineContextTransformer<SourceContext, TargetContext>: ContextTransformer {
|
|
20 |
|
|
21 |
// MARK: Properties
|
|
22 |
|
|
23 |
private let transformationBlock: (SourceContext) throws -> TargetContext
|
|
24 |
|
|
25 |
// MARK: Methods
|
|
26 |
|
|
27 |
/// Constructor
|
|
28 |
///
|
|
29 |
/// - Parameter transformationBlock: the block to be called when it requested to transform the context.
|
|
30 |
public init(_ transformationBlock: @escaping (SourceContext) throws -> TargetContext) {
|
9x |
31 |
self.transformationBlock = transformationBlock
|
9x |
32 |
}
|
9x |
33 |
|
|
34 |
public func transform(_ context: SourceContext) throws -> TargetContext {
|
15x |
35 |
return try transformationBlock(context)
|
15x |
36 |
}
|
15x |
37 |
}
|
|