Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassRegistry/RegistryError.swift8// Module : SPGlassRegistry9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Errors raised while reading, validating or building a registry. Each has a stable machine-readable code.44public enum RegistryError: Error, Equatable, Sendable, CustomStringConvertible {45 case invalidIdentifier(String)46 case invalidVersion(String)47 case unsafePath(String, reason: String)48 case pathCollision(String, String)49 case tooLarge(what: String, bytes: Int, limit: Int)50 case tooDeep(what: String, limit: Int)51 case malformed(what: String, detail: String)52 case unsupportedSchema(what: String, version: Int)53 case invalidManifest(id: String, detail: String)54 case hashMismatch(path: String, expected: String, actual: String)55 case sizeMismatch(path: String, expected: Int, actual: Int)56 case unknownItem(String)57 case unknownVersion(id: String, version: String)58 case dependencyCycle([String])59 case signatureMissing60 case signatureInvalid61 case invalidKey(String)62 case immutableVersion(id: String, version: String)6364 public var code: String {65 switch self {66 case .invalidIdentifier: "invalid_identifier"67 case .invalidVersion: "invalid_version"68 case .unsafePath: "unsafe_path"69 case .pathCollision: "path_collision"70 case .tooLarge: "too_large"71 case .tooDeep: "too_deep"72 case .malformed: "malformed"73 case .unsupportedSchema: "unsupported_schema"74 case .invalidManifest: "invalid_manifest"75 case .hashMismatch: "hash_mismatch"76 case .sizeMismatch: "size_mismatch"77 case .unknownItem: "unknown_item"78 case .unknownVersion: "unknown_version"79 case .dependencyCycle: "dependency_cycle"80 case .signatureMissing: "signature_missing"81 case .signatureInvalid: "signature_invalid"82 case .invalidKey: "invalid_key"83 case .immutableVersion: "immutable_version"84 }85 }8687 public var description: String {88 switch self {89 case .invalidIdentifier(let id): "\"\(Self.clip(id))\" is not a valid identifier (lower-case letters, digits and single hyphens, at most 64 characters)."90 case .invalidVersion(let version): "\"\(Self.clip(version))\" is not a semantic version (MAJOR.MINOR.PATCH)."91 case .unsafePath(let path, let reason): "Unsafe path \"\(Self.clip(path))\": \(reason)."92 case .pathCollision(let a, let b): "Paths \"\(Self.clip(a))\" and \"\(Self.clip(b))\" collide."93 case .tooLarge(let what, let bytes, let limit): "\(what) is \(bytes) bytes; the limit is \(limit)."94 case .tooDeep(let what, let limit): "\(what) nests more than \(limit) levels."95 case .malformed(let what, let detail): "\(what) cannot be read: \(detail)"96 case .unsupportedSchema(let what, let version): "\(what) uses schema version \(version), which this version does not support."97 case .invalidManifest(let id, let detail): "Manifest \"\(Self.clip(id))\" is invalid: \(detail)"98 case .hashMismatch(let path, let expected, let actual): "Checksum mismatch for \"\(Self.clip(path))\": expected \(expected.prefix(12))…, got \(actual.prefix(12))…."99 case .sizeMismatch(let path, let expected, let actual): "Size mismatch for \"\(Self.clip(path))\": expected \(expected) bytes, got \(actual)."100 case .unknownItem(let id): "\"\(Self.clip(id))\" is not in the registry."101 case .unknownVersion(let id, let version): "Version \(Self.clip(version)) of \"\(Self.clip(id))\" is not in the registry."102 case .dependencyCycle(let cycle): "Dependency cycle: " + cycle.map(Self.clip).joined(separator: " → ") + "."103 case .signatureMissing: "The registry index is not signed, but a public key is configured."104 case .signatureInvalid: "The signature of the registry index does not match the configured public key."105 case .invalidKey(let detail): "Invalid key: \(detail)"106 case .immutableVersion(let id, let version): "Version \(version) of \"\(id)\" is already published with different content. Published versions are immutable: release a new version."107 }108 }109110 /// Keeps untrusted text short and on one line before it reaches a terminal or a log.111 static func clip(_ text: String) -> String {112 String(text.unicodeScalars.filter { $0.value >= 0x20 && $0.value != 0x7F && !(0x80...0x9F).contains($0.value) }.prefix(120))113 }114}115