Review generated order-state update

from Enums and pattern matching
Swift 6.3.3 advanced 6 min 4 issues to find

Review this generated order-state update against its boundary contract.

Apply a server status to an order. Unknown status strings must be preserved, paid requires a nonempty receipt, failure reasons are sensitive, and dispatch should make the complete state mapping obvious.

swift
enum ServerStatus: String {
    case pending
    case paid
    case failed
}

enum OrderState {
    case pending
    case paid(receipt: String)
    case failed(reason: String)
    case unknown(String)
}

func apply(statusText: String, receipt: String?, reason: String?,
           to state: inout OrderState) {
    let status = ServerStatus(rawValue: statusText)!
    if case .paid = status {
        state = .paid(receipt: receipt ?? "")
    }
    if case .failed = status {
        print("failure reason:", reason ?? "none")
        state = .failed(reason: reason ?? "unknown")
    }
    if case .pending = status { state = .pending }
}

generated code is illustrative, not from any one model

Open in playground
Report an error