1
- #if canImport(FoundationEssentials)
2
- import FoundationEssentials
3
- #elseif canImport(Foundation)
4
- import Foundation
5
- #endif
6
-
7
1
// MARK: - Error Types
8
2
9
3
/// Errors that can occur during CBOR encoding and decoding.
10
4
///
11
5
/// These errors provide detailed information about what went wrong during
12
6
/// CBOR processing operations, helping developers diagnose and fix issues
13
7
/// in their CBOR data or usage of the CBOR API.
14
- public enum CBORError : Error {
8
+ public enum CBORError : Error , Equatable , Sendable {
15
9
/// The input data is not valid CBOR.
16
10
///
17
11
/// This error occurs when the decoder encounters data that doesn't conform to
18
12
/// the CBOR specification (RFC 8949). This could be due to corrupted data,
19
13
/// incomplete data, or data encoded with a different format entirely.
20
14
case invalidCBOR
21
15
22
- /// Expected a specific type but found another.
23
- ///
24
- /// This error occurs when trying to decode a CBOR value as a specific type,
25
- /// but the actual type of the value doesn't match the expected type.
26
- /// - Parameters:
27
- /// - expected: The type that was expected (e.g., "String", "Int", "Array")
28
- /// - actual: The actual type that was found in the CBOR data
29
- case typeMismatch( expected: String , actual: String )
30
-
31
- /// Array index out of bounds.
32
- ///
33
- /// This error occurs when attempting to access an element in a CBOR array
34
- /// using an index that is outside the valid range for the array.
35
- /// - Parameters:
36
- /// - index: The requested index that was attempted to be accessed
37
- /// - count: The actual number of elements in the array (valid indices are 0..<count)
38
- case outOfBounds( index: Int , count: Int )
39
-
40
- /// Required key missing from map.
41
- ///
42
- /// This error occurs when trying to decode a CBOR map into a Swift struct or class,
43
- /// but a required key is not present in the map.
44
- /// - Parameter key: The name of the missing key
45
- case missingKey( String )
46
-
47
- /// Value conversion failed.
48
- ///
49
- /// This error occurs when a CBOR value cannot be converted to the requested Swift type,
50
- /// even though the CBOR type is compatible with the requested type.
51
- /// - Parameter message: A description of what went wrong during the conversion
52
- case valueConversionFailed( String )
53
-
54
16
/// Invalid UTF-8 string data.
55
17
///
56
18
/// This error occurs when decoding a CBOR text string that contains invalid UTF-8 sequences.
57
19
/// All CBOR text strings must contain valid UTF-8 data according to the specification.
58
20
case invalidUTF8
59
21
60
- /// Integer overflow during encoding/decoding.
61
- ///
62
- /// This error occurs when a CBOR integer value is too large to fit into the
63
- /// corresponding Swift integer type (e.g., trying to decode a UInt64.max into an Int).
64
- case integerOverflow
65
-
66
- /// Tag value is not supported.
67
- ///
68
- /// This error occurs when the decoder encounters a CBOR tag that is not supported
69
- /// by the current implementation.
70
- /// - Parameter tag: The unsupported tag number
71
- case unsupportedTag( UInt64 )
72
-
73
22
/// Reached end of data while decoding.
74
23
///
75
24
/// This error occurs when the decoder unexpectedly reaches the end of the input data
@@ -105,26 +54,15 @@ public enum CBORError: Error {
105
54
case extraDataFound
106
55
}
107
56
57
+ @_unavailableInEmbedded
108
58
extension CBORError : CustomStringConvertible {
109
59
/// A human-readable description of the error.
110
60
public var description : String {
111
61
switch self {
112
62
case . invalidCBOR:
113
63
return " Invalid CBOR data: The input does not conform to the CBOR specification (RFC 8949) "
114
- case . typeMismatch( let expected, let actual) :
115
- return " Type mismatch: expected \( expected) , found \( actual) "
116
- case . outOfBounds( let index, let count) :
117
- return " Array index out of bounds: attempted to access index \( index) , but array only contains \( count) elements (valid indices are 0..< \( count) ) "
118
- case . missingKey( let key) :
119
- return " Missing key: required key ' \( key) ' was not found in the CBOR map "
120
- case . valueConversionFailed( let message) :
121
- return " Value conversion failed: \( message) "
122
64
case . invalidUTF8:
123
65
return " Invalid UTF-8 data: the CBOR text string contains invalid UTF-8 sequences "
124
- case . integerOverflow:
125
- return " Integer overflow: the CBOR integer value is too large for the target Swift integer type "
126
- case . unsupportedTag( let tag) :
127
- return " Unsupported tag: tag \( tag) is not supported by this implementation "
128
66
case . prematureEnd:
129
67
return " Unexpected end of data: reached the end of input before completing the CBOR value "
130
68
case . invalidInitialByte( let byte) :
@@ -139,6 +77,13 @@ extension CBORError: CustomStringConvertible {
139
77
}
140
78
}
141
79
80
+ #if canImport(FoundationEssentials)
81
+ import FoundationEssentials
82
+ #elseif canImport(Foundation)
83
+ import Foundation
84
+ #endif
85
+
86
+ @_unavailableInEmbedded
142
87
extension CBORError : LocalizedError {
143
88
public var errorDescription : String ? {
144
89
return description
0 commit comments