SPB Git

Repository Public

OpenEnum.swift 5.4 KB 133 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Sources/ClaudeRemoteModels/OpenEnum.swift10// Module           : ClaudeRemoteModels11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990/// A string enum of the wire protocol that tolerates values this build does not know yet.91///92/// Every server enum is decoded through this protocol: an unknown raw value becomes93/// `.unknown(raw)` instead of failing the whole payload, and it is encoded back unchanged94/// (so a `PUT /settings` never destroys a value introduced by a newer server).95///96/// Deliberately NOT `RawRepresentable`: that conformance would replace the synthesized `==` and97/// `hash(into:)` by the standard library's raw-value based ones, which would recurse through98/// `rawValue` below. `rawValue` and `init(rawValue:)` are provided as plain members instead.99public protocol OpenStringEnum: Codable, Hashable, Sendable, CaseIterable, CustomStringConvertible {100    /// Known cases and their wire values.101    static var known: [(Self, String)] { get }102    static func unknown(_ raw: String) -> Self103    /// The raw value when this is the `unknown` case.104    var unknownRawValue: String? { get }105}106107extension OpenStringEnum {108    /// Never fails: an unknown raw value becomes `.unknown(raw)`.109    public init(_ raw: String) {110        self = Self.known.first { $0.1 == raw }?.0 ?? .unknown(raw)111    }112113    public init(rawValue: String) { self.init(rawValue) }114115    public var rawValue: String {116        if let raw = unknownRawValue { return raw }117        return Self.known.first { $0.0 == self }?.1 ?? ""118    }119120    public static var allCases: [Self] { known.map(\.0) }121    public var isKnown: Bool { unknownRawValue == nil }122    public var description: String { rawValue }123124    public init(from decoder: Decoder) throws {125        self.init(try decoder.singleValueContainer().decode(String.self))126    }127128    public func encode(to encoder: Encoder) throws {129        var c = encoder.singleValueContainer()130        try c.encode(rawValue)131    }132}133
Files main
Menu

Theme