CompressionAlgorithm.swift 3.5 KB

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