xref: /aosp_15_r20/external/flatbuffers/swift/Sources/FlatBuffers/FlatbuffersErrors.swift (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker  * Copyright 2021 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker  *
4*890232f2SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker  *
8*890232f2SAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker  *
10*890232f2SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker  * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker  */
16*890232f2SAndroid Build Coastguard Worker 
17*890232f2SAndroid Build Coastguard Worker #if !os(WASI)
18*890232f2SAndroid Build Coastguard Worker import Foundation
19*890232f2SAndroid Build Coastguard Worker #else
20*890232f2SAndroid Build Coastguard Worker import SwiftOverlayShims
21*890232f2SAndroid Build Coastguard Worker #endif
22*890232f2SAndroid Build Coastguard Worker 
23*890232f2SAndroid Build Coastguard Worker /// Collection of thrown from the Flatbuffer verifier
24*890232f2SAndroid Build Coastguard Worker public enum FlatbuffersErrors: Error, Equatable {
25*890232f2SAndroid Build Coastguard Worker 
26*890232f2SAndroid Build Coastguard Worker   /// Thrown when verifying a file id that doesnt match buffer id
27*890232f2SAndroid Build Coastguard Worker   case bufferIdDidntMatchPassedId
28*890232f2SAndroid Build Coastguard Worker   /// Prefixed size doesnt match the current (readable) buffer size
29*890232f2SAndroid Build Coastguard Worker   case prefixedSizeNotEqualToBufferSize
30*890232f2SAndroid Build Coastguard Worker   /// Thrown when buffer is bigger than the allowed 2GiB
31*890232f2SAndroid Build Coastguard Worker   case exceedsMaxSizeAllowed
32*890232f2SAndroid Build Coastguard Worker   /// Thrown when there is an missaligned pointer at position
33*890232f2SAndroid Build Coastguard Worker   /// of type
34*890232f2SAndroid Build Coastguard Worker   case missAlignedPointer(position: Int, type: String)
35*890232f2SAndroid Build Coastguard Worker   /// Thrown when trying to read a value that goes out of the
36*890232f2SAndroid Build Coastguard Worker   /// current buffer bounds
37*890232f2SAndroid Build Coastguard Worker   case outOfBounds(position: UInt, end: Int)
38*890232f2SAndroid Build Coastguard Worker   /// Thrown when the signed offset is out of the bounds of the
39*890232f2SAndroid Build Coastguard Worker   /// current buffer
40*890232f2SAndroid Build Coastguard Worker   case signedOffsetOutOfBounds(offset: Int, position: Int)
41*890232f2SAndroid Build Coastguard Worker   /// Thrown when a required field doesnt exist within the buffer
42*890232f2SAndroid Build Coastguard Worker   case requiredFieldDoesntExist(position: VOffset, name: String)
43*890232f2SAndroid Build Coastguard Worker   /// Thrown when a string is missing its NULL Terminator `\0`,
44*890232f2SAndroid Build Coastguard Worker   /// this can be disabled in the `VerifierOptions`
45*890232f2SAndroid Build Coastguard Worker   case missingNullTerminator(position: Int, str: String?)
46*890232f2SAndroid Build Coastguard Worker   /// Thrown when the verifier has reached the maximum tables allowed,
47*890232f2SAndroid Build Coastguard Worker   /// this can be disabled in the `VerifierOptions`
48*890232f2SAndroid Build Coastguard Worker   case maximumTables
49*890232f2SAndroid Build Coastguard Worker   /// Thrown when the verifier has reached the maximum depth allowed,
50*890232f2SAndroid Build Coastguard Worker   /// this can be disabled in the `VerifierOptions`
51*890232f2SAndroid Build Coastguard Worker   case maximumDepth
52*890232f2SAndroid Build Coastguard Worker   /// Thrown when the verifier is presented with an unknown union case
53*890232f2SAndroid Build Coastguard Worker   case unknownUnionCase
54*890232f2SAndroid Build Coastguard Worker   /// thrown when a value for a union is not found within the buffer
55*890232f2SAndroid Build Coastguard Worker   case valueNotFound(key: Int?, keyName: String, field: Int?, fieldName: String)
56*890232f2SAndroid Build Coastguard Worker   /// thrown when the size of the keys vector doesnt match fields vector
57*890232f2SAndroid Build Coastguard Worker   case unionVectorSize(
58*890232f2SAndroid Build Coastguard Worker     keyVectorSize: Int,
59*890232f2SAndroid Build Coastguard Worker     fieldVectorSize: Int,
60*890232f2SAndroid Build Coastguard Worker     unionKeyName: String,
61*890232f2SAndroid Build Coastguard Worker     fieldName: String)
62*890232f2SAndroid Build Coastguard Worker   case apparentSizeTooLarge
63*890232f2SAndroid Build Coastguard Worker 
64*890232f2SAndroid Build Coastguard Worker }
65*890232f2SAndroid Build Coastguard Worker 
66*890232f2SAndroid Build Coastguard Worker #if !os(WASI)
67*890232f2SAndroid Build Coastguard Worker 
68*890232f2SAndroid Build Coastguard Worker extension FlatbuffersErrors {
69*890232f2SAndroid Build Coastguard Worker     public static func == (
70*890232f2SAndroid Build Coastguard Worker       lhs: FlatbuffersErrors,
71*890232f2SAndroid Build Coastguard Worker       rhs: FlatbuffersErrors) -> Bool
72*890232f2SAndroid Build Coastguard Worker     {
73*890232f2SAndroid Build Coastguard Worker       lhs.localizedDescription == rhs.localizedDescription
74*890232f2SAndroid Build Coastguard Worker     }
75*890232f2SAndroid Build Coastguard Worker }
76*890232f2SAndroid Build Coastguard Worker 
77*890232f2SAndroid Build Coastguard Worker #endif
78