|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +public extension AsyncSequence { |
| 14 | + |
| 15 | + /// Converts any failure into a new error. |
| 16 | + /// |
| 17 | + /// - Parameter transform: A closure that takes the failure as a parameter and returns a new error. |
| 18 | + /// - Returns: An asynchronous sequence that maps the error thrown into the one produced by the transform closure. |
| 19 | + /// |
| 20 | + /// Use the ``mapError(_:)`` operator when you need to replace one error type with another. |
| 21 | + func mapError<ErrorType>(transform: @Sendable @escaping (Error) -> ErrorType) -> AsyncMapErrorSequence<Self, ErrorType> { |
| 22 | + .init(base: self, transform: transform) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// An asynchronous sequence that converts any failure into a new error. |
| 27 | +public struct AsyncMapErrorSequence<Base: AsyncSequence, ErrorType: Error>: AsyncSequence, Sendable where Base: Sendable { |
| 28 | + |
| 29 | + public typealias AsyncIterator = Iterator |
| 30 | + public typealias Element = Base.Element |
| 31 | + |
| 32 | + private let base: Base |
| 33 | + private let transform: @Sendable (Error) -> ErrorType |
| 34 | + |
| 35 | + init( |
| 36 | + base: Base, |
| 37 | + transform: @Sendable @escaping (Error) -> ErrorType |
| 38 | + ) { |
| 39 | + self.base = base |
| 40 | + self.transform = transform |
| 41 | + } |
| 42 | + |
| 43 | + public func makeAsyncIterator() -> Iterator { |
| 44 | + Iterator( |
| 45 | + base: base.makeAsyncIterator(), |
| 46 | + transform: transform |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public extension AsyncMapErrorSequence { |
| 52 | + |
| 53 | + /// The iterator that produces elements of the map sequence. |
| 54 | + struct Iterator: AsyncIteratorProtocol { |
| 55 | + |
| 56 | + public typealias Element = Base.Element |
| 57 | + |
| 58 | + private var base: Base.AsyncIterator |
| 59 | + |
| 60 | + private let transform: @Sendable (Error) -> ErrorType |
| 61 | + |
| 62 | + public init( |
| 63 | + base: Base.AsyncIterator, |
| 64 | + transform: @Sendable @escaping (Error) -> ErrorType |
| 65 | + ) { |
| 66 | + self.base = base |
| 67 | + self.transform = transform |
| 68 | + } |
| 69 | + |
| 70 | + public mutating func next() async throws -> Element? { |
| 71 | + do { |
| 72 | + return try await base.next() |
| 73 | + } catch { |
| 74 | + throw transform(error) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments