• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import Foundation
18 
19 /// Enum is a protocol that all flatbuffers enums should conform to
20 /// Since it allows us to get the actual `ByteSize` and `Value` from
21 /// a swift enum.
22 public protocol Enum {
23   /// associatedtype that the type of the enum should conform to
24   associatedtype T: Scalar & Verifiable
25   /// Size of the current associatedtype in the enum
26   static var byteSize: Int { get }
27   /// The current value the enum hosts
28   var value: T { get }
29 }
30 
31 extension Enum where Self: Verifiable {
32 
33   /// Verifies that the current value is which the bounds of the buffer, and if
34   /// the current `Value` is aligned properly
35   /// - Parameters:
36   ///   - verifier: Verifier that hosts the buffer
37   ///   - position: Current position within the buffer
38   ///   - type: The type of the object to be verified
39   /// - Throws: Errors coming from `inBuffer` function
40   public static func verify<T>(
41     _ verifier: inout Verifier,
42     at position: Int,
43     of type: T.Type) throws where T: Verifiable
44   {
45     try verifier.inBuffer(position: position, of: type.self)
46   }
47 
48 }
49 
50 /// UnionEnum is a Protocol that allows us to create Union type of enums
51 /// and their value initializers. Since an `init` was required by
52 /// the verifier
53 public protocol UnionEnum: Enum {
54   init?(value: T) throws
55 }
56