Enum.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Sources/SwiftProtobuf/Enum.swift - Enum support
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Generated enums conform to SwiftProtobuf.Enum
  12. ///
  13. /// See ProtobufTypes and JSONTypes for extension
  14. /// methods to support binary and JSON coding.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. /// Generated enum types conform to this protocol.
  18. @preconcurrency
  19. public protocol Enum: RawRepresentable, Hashable, Sendable {
  20. /// Creates a new instance of the enum initialized to its default value.
  21. init()
  22. /// Creates a new instance of the enum from the given raw integer value.
  23. ///
  24. /// For proto2 enums, this initializer will fail if the raw value does not
  25. /// correspond to a valid enum value. For proto3 enums, this initializer never
  26. /// fails; unknown values are created as instances of the `UNRECOGNIZED` case.
  27. ///
  28. /// - Parameter rawValue: The raw integer value from which to create the enum
  29. /// value.
  30. init?(rawValue: Int)
  31. /// The raw integer value of the enum value.
  32. ///
  33. /// For a recognized enum case, this is the integer value of the case as
  34. /// defined in the .proto file. For `UNRECOGNIZED` cases in proto3, this is
  35. /// the value that was originally decoded.
  36. var rawValue: Int { get }
  37. }
  38. extension Enum {
  39. public func hash(into hasher: inout Hasher) {
  40. hasher.combine(rawValue)
  41. }
  42. /// Internal convenience property representing the name of the enum value (or
  43. /// `nil` if it is an `UNRECOGNIZED` value or doesn't provide names).
  44. ///
  45. /// Since the text format and JSON names are always identical, we don't need
  46. /// to distinguish them.
  47. internal var name: _NameMap.Name? {
  48. guard let nameProviding = Self.self as? any _ProtoNameProviding.Type else {
  49. return nil
  50. }
  51. return nameProviding._protobuf_nameMap.names(for: rawValue)?.proto
  52. }
  53. /// Internal convenience initializer that returns the enum value with the
  54. /// given name, if it provides names.
  55. ///
  56. /// Since the text format and JSON names are always identical, we don't need
  57. /// to distinguish them.
  58. ///
  59. /// - Parameter name: The name of the enum case.
  60. internal init?(name: String) {
  61. guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
  62. let number = nameProviding._protobuf_nameMap.number(forJSONName: name)
  63. else {
  64. return nil
  65. }
  66. self.init(rawValue: number)
  67. }
  68. /// Internal convenience initializer that returns the enum value with the
  69. /// given name, if it provides names.
  70. ///
  71. /// Since the text format and JSON names are always identical, we don't need
  72. /// to distinguish them.
  73. ///
  74. /// - Parameter name: Buffer holding the UTF-8 bytes of the desired name.
  75. internal init?(rawUTF8: UnsafeRawBufferPointer) {
  76. guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
  77. let number = nameProviding._protobuf_nameMap.number(forJSONName: rawUTF8)
  78. else {
  79. return nil
  80. }
  81. self.init(rawValue: number)
  82. }
  83. }