From 576003fb702a34ed84a6ad5d5ab1a39e05c5be73 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 14 May 2024 15:56:13 +0100 Subject: [PATCH] Structify errors --- Sources/ExtrasBase64/Base32.swift | 13 +++++++++++-- Sources/ExtrasBase64/Base64.swift | 22 +++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Sources/ExtrasBase64/Base32.swift b/Sources/ExtrasBase64/Base32.swift index d6c1982..0cbaf0e 100644 --- a/Sources/ExtrasBase64/Base32.swift +++ b/Sources/ExtrasBase64/Base32.swift @@ -30,8 +30,17 @@ public enum Base32 { public static let allowNullCharacters = DecodingOptions(rawValue: UInt(1 << 0)) } - public enum DecodingError: Swift.Error, Equatable { - case invalidCharacter + public struct DecodingError: Swift.Error, Equatable { + enum _Internal { + case invalidCharacter + } + + fileprivate let value: _Internal + init(_ value: _Internal) { + self.value = value + } + + public static var invalidCharacter: Self { .init(.invalidCharacter) } } /// Base32 Encode a buffer to an array of bytes diff --git a/Sources/ExtrasBase64/Base64.swift b/Sources/ExtrasBase64/Base64.swift index ba2a557..b75328b 100644 --- a/Sources/ExtrasBase64/Base64.swift +++ b/Sources/ExtrasBase64/Base64.swift @@ -334,11 +334,23 @@ extension Base64 { public static let omitPaddingCharacter = DecodingOptions(rawValue: UInt(1 << 1)) } - public enum DecodingError: Error, Equatable { - case invalidLength - case invalidCharacter(UInt8) - case unexpectedPaddingCharacter - case unexpectedEnd + public struct DecodingError: Error, Equatable { + fileprivate enum _Internal: Error, Equatable { + case invalidLength + case invalidCharacter(UInt8) + case unexpectedPaddingCharacter + case unexpectedEnd + } + + fileprivate let value: _Internal + fileprivate init(_ value: _Internal) { + self.value = value + } + + public static var invalidLength: Self { .init(.invalidLength) } + public static func invalidCharacter(_ character: UInt8) -> Self { .init(.invalidCharacter(character)) } + public static var unexpectedPaddingCharacter: Self { .init(.unexpectedPaddingCharacter) } + public static var unexpectedEnd: Self { .init(.unexpectedEnd) } } @inlinable