Guest User

Untitled

a guest
Nov 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import Foundation
  2. enum Either<T> {
  3. case success(T)
  4. case error(Swift.Error)
  5.  
  6. var successResult: T? {
  7. switch self {
  8. case .success(let result):
  9. return result
  10. case .error(_):
  11. return nil
  12. }
  13. }
  14. }
  15.  
  16. protocol UseCaseProtocol {
  17. associatedtype ReturnType
  18. associatedtype Param
  19. func execute(_ params: Param, completion: @escaping ((Either<ReturnType>) -> Void))
  20. }
  21.  
  22. struct UseCase<P, R> {
  23. typealias ReturnType = R
  24. typealias Param = P
  25.  
  26. init<U>(_ useCase: U) where U: UseCaseProtocol, U.ReturnType == ReturnType, U.Param == Param {
  27. _execute = useCase.execute
  28. }
  29.  
  30. func execute(_ params: P, completion: @escaping ((Either<R>) -> Void)) {
  31. _execute(params, completion)
  32. }
  33.  
  34. let _execute: (P, @escaping (Either<R>) -> Void) -> Void
  35. }
Add Comment
Please, Sign In to add comment