CompressionAlgorithm.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2024, gRPC Authors 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. /// Message compression algorithms.
  17. public struct CompressionAlgorithm: Hashable, Sendable {
  18. @_spi(Package)
  19. public enum Value: UInt8, Hashable, Sendable, CaseIterable {
  20. case none = 0
  21. case deflate
  22. case gzip
  23. }
  24. @_spi(Package)
  25. public let value: Value
  26. fileprivate init(_ algorithm: Value) {
  27. self.value = algorithm
  28. }
  29. /// No compression, sometimes referred to as 'identity' compression.
  30. public static var none: Self {
  31. Self(.none)
  32. }
  33. /// The 'deflate' compression algorithm.
  34. public static var deflate: Self {
  35. Self(.deflate)
  36. }
  37. /// The 'gzip' compression algorithm.
  38. public static var gzip: Self {
  39. Self(.gzip)
  40. }
  41. }
  42. /// A set of compression algorithms.
  43. public struct CompressionAlgorithmSet: OptionSet, Hashable, Sendable {
  44. public var rawValue: UInt32
  45. public init(rawValue: UInt32) {
  46. self.rawValue = rawValue
  47. }
  48. private init(value: CompressionAlgorithm.Value) {
  49. self.rawValue = 1 << value.rawValue
  50. }
  51. /// No compression, sometimes referred to as 'identity' compression.
  52. public static var none: Self {
  53. return Self(value: .none)
  54. }
  55. /// The 'deflate' compression algorithm.
  56. public static var deflate: Self {
  57. return Self(value: .deflate)
  58. }
  59. /// The 'gzip' compression algorithm.
  60. public static var gzip: Self {
  61. return Self(value: .gzip)
  62. }
  63. /// All compression algorithms.
  64. public static var all: Self {
  65. return [.gzip, .deflate, .none]
  66. }
  67. /// Returns whether a given algorithm is present in the set.
  68. ///
  69. /// - Parameter algorithm: The algorithm to check.
  70. public func contains(_ algorithm: CompressionAlgorithm) -> Bool {
  71. return self.contains(CompressionAlgorithmSet(value: algorithm.value))
  72. }
  73. }
  74. extension CompressionAlgorithmSet {
  75. /// A sequence of ``CompressionAlgorithm`` values present in the set.
  76. public var elements: Elements {
  77. Elements(algorithmSet: self)
  78. }
  79. /// A sequence of ``CompressionAlgorithm`` values present in a ``CompressionAlgorithmSet``.
  80. public struct Elements: Sequence {
  81. public typealias Element = CompressionAlgorithm
  82. private let algorithmSet: CompressionAlgorithmSet
  83. init(algorithmSet: CompressionAlgorithmSet) {
  84. self.algorithmSet = algorithmSet
  85. }
  86. public func makeIterator() -> Iterator {
  87. return Iterator(algorithmSet: self.algorithmSet)
  88. }
  89. public struct Iterator: IteratorProtocol {
  90. private let algorithmSet: CompressionAlgorithmSet
  91. private var iterator: IndexingIterator<[CompressionAlgorithm.Value]>
  92. init(algorithmSet: CompressionAlgorithmSet) {
  93. self.algorithmSet = algorithmSet
  94. self.iterator = CompressionAlgorithm.Value.allCases.makeIterator()
  95. }
  96. public mutating func next() -> CompressionAlgorithm? {
  97. while let value = self.iterator.next() {
  98. if self.algorithmSet.contains(CompressionAlgorithmSet(value: value)) {
  99. return CompressionAlgorithm(value)
  100. }
  101. }
  102. return nil
  103. }
  104. }
  105. }
  106. }