Contents

edonv/orother

`OrOther` is a macro that adds a "blank" `.other(_:)` case to any enum. All that's needed is to create an empty enum, add a `private` nested enum called `Options` with an explicit raw value type, add the explicit cases you want, then tack `@OrOther` onto the primary enum.

Example

Usage

@OrOther
enum EnumTest: Codable {
    private enum Options: String {
        case a
        case b
        case c, d, e, f
    }
}

Synthesized Output

enum EnumTest {
    private enum Options: String { ... }
    
    typealias RawValue = String

    case a, b, c, d, e, f, other(String)

    var rawValue: RawValue {
        switch self {
        case .a:
            return Options.a.rawValue
        case .b:
            return Options.b.rawValue
        case .c:
            return Options.c.rawValue
        case .d:
            return Options.d.rawValue
        case .e:
            return Options.e.rawValue
        case .f:
            return Options.f.rawValue
        case .other(let string):
            return string
        }
    }

    init(rawValue: RawValue) {
        if let this = Options(rawValue: rawValue) {
            switch this {
            case .a:
                self = .a
            case .b:
                self = .b
            case .c:
                self = .c
            case .d:
                self = .d
            case .e:
                self = .e
            case .f:
                self = .f
            }
        } else {
            self = .other(rawValue)
        }
    }
}

Supported Protocols

The following protocols can be explicitly added to the primary enum, and @OrOther its conformance will be automatically synthesized:

  • Equatable
  • Hashable
  • Encodable / Decodable / Codable
  • CaseIterable

Package Metadata

Repository: edonv/orother

Default branch: main

README: README.md