| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// ContextTransformerBox.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 |
final class ContextTransformerBox<T: ContextTransformer>: AnyContextTransformer {
|
|
| 16 |
|
|
| 17 |
let transformer: T
|
|
| 18 |
|
|
| 19 |
init(_ transformer: T) {
|
17x |
| 20 |
self.transformer = transformer
|
17x |
| 21 |
}
|
17x |
| 22 |
|
|
| 23 |
func transform<Context>(_ context: AnyContext) throws -> Context {
|
42x |
| 24 |
let typedContext: T.SourceContext = try context.value()
|
42x |
| 25 |
guard let transformedContext = try transformer.transform(typedContext) as? Context else {
|
42x |
| 26 |
throw RoutingError.typeMismatch(type: type(of: T.TargetContext.self),
|
! |
| 27 |
expectedType: Context.self,
|
! |
| 28 |
.init("Failed to transform \(String(describing: T.TargetContext.self)) to \(String(describing: Context.self))."))
|
! |
| 29 |
}
|
42x |
| 30 |
return transformedContext
|
42x |
| 31 |
}
|
42x |
| 32 |
|
|
| 33 |
}
|
|