SkillAgentSearch skills...

Bijection

generate roundtripping logic for RawRepresentable, LosslessStringConvertible, and more!

Install / Use

/learn @ordo-one/Bijection
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<div align="center">

🥨   bijection   🥨

generate roundtripping logic for RawRepresentable, LosslessStringConvertible, and more!

documentation · license

</div>

Requirements

@Bijection is a Swift macro that generates an initializer from a switch-case mapping of an enum’s cases to set of corresponding values. It is useful for generating roundtripping logic for things like binary encodings and string representations, in situations where relying on native raw value-backed enums is insufficient, experiences poor performance due to lack of inlining, or would interfere with other compiler features, such as synthesized Comparable.

The @Bijection library requires Swift 6.1 or later.

<!-- DO NOT EDIT BELOW! AUTOSYNC CONTENT [STATUS TABLE] -->

| Platform | Status | | -------- | ------| | 💬 Documentation | Status | | 🐧 Linux | Status | | 🍏 Darwin | Status | | 🍏 Darwin (iOS) | Status | | 🍏 Darwin (tvOS) | Status | | 🍏 Darwin (visionOS) | Status | | 🍏 Darwin (watchOS) | Status |

<!-- DO NOT EDIT ABOVE! AUTOSYNC CONTENT [STATUS TABLE] -->

Check deployment minimums

Examples

Generate a plain, unlabeled initializer:

enum Enum: CaseIterable, Equatable {
    case a, b, c

    @Bijection
    var value: Unicode.Scalar {
        switch self {
        case .a: "a"
        case .b: "b"
        case .c: "c"
        }
    }
}

/* --- EXPANDS TO --- */
extension Enum {
    init?(_ $value: borrowing Unicode.Scalar) {
        switch $value {
        case "a":
            self = .a
        case "b":
            self = .b
        case "c":
            self = .c
        default:
            return nil
        }
    }
}

Generate an initializer with a custom argument label:

extension Enum {
    @Bijection(label: "index")
    var index: Int {
        switch self {
        case .a: 1
        case .b: 2
        case .c: 3
        }
    }
}

/* --- EXPANDS TO --- */
extension Enum {
    init?(index $value: borrowing Int) {
        switch $value {
        case 1:
            self = .a
        case 2:
            self = .b
        case 3:
            self = .c
        default:
            return nil
        }
    }
}

Generate an initializer from a getter in a property with multiple accessors:

extension Enum: LosslessStringConvertible {
    @Bijection
    var description: String {
        get {
            switch self {
            case .a: "A"
            case .b: "B"
            case .c: "C"
            }
        }
        set(value) {
            if let value: Self = .init(value) {
                self = value
            }
        }
    }
}
/* --- EXPANDS TO --- */
extension Enum {
    init?(_ $value: borrowing String) {
        switch $value {
        case "A":
            self = .a
        case "B":
            self = .b
        case "C":
            self = .c
        default:
            return nil
        }
    }
}

The @Bijection macro will mirror the access control (and other modifiers, such as nonisolated) of the property it is applied to. It will also copy the following attributes, if present:

  1. @available
  2. @backDeployed
  3. @inlinable
  4. @inline
  5. @usableFromInline
View on GitHub
GitHub Stars12
CategoryDevelopment
Updated1d ago
Forks0

Languages

Swift

Security Score

95/100

Audited on Apr 3, 2026

No findings