Coverage Report

Created: 2025-09-04 06:32

/src/grpc-swift/Sources/GRPC/Interceptor/MessageParts.swift
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020, 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
import NIOHPACK
17
18
public enum GRPCClientRequestPart<Request> {
19
  /// User provided metadata sent at the start of the request stream.
20
  case metadata(HPACKHeaders)
21
22
  /// A message to send to the server.
23
  case message(Request, MessageMetadata)
24
25
  /// The end the request stream.
26
  case end
27
}
28
29
public enum GRPCClientResponsePart<Response> {
30
  /// The metadata returned by the server at the start of the RPC.
31
  case metadata(HPACKHeaders)
32
33
  /// A response message from the server.
34
  case message(Response)
35
36
  /// The end of response stream sent by the server.
37
  case end(GRPCStatus, HPACKHeaders)
38
}
39
40
public enum GRPCServerRequestPart<Request> {
41
  /// Metadata received from the client at the start of the RPC.
42
  case metadata(HPACKHeaders)
43
44
  /// A request message sent by the client.
45
  case message(Request)
46
47
  /// The end the request stream.
48
  case end
49
}
50
51
public enum GRPCServerResponsePart<Response> {
52
  /// The metadata to send to the client at the start of the response stream.
53
  case metadata(HPACKHeaders)
54
55
  /// A response message sent by the server.
56
  case message(Response, MessageMetadata)
57
58
  /// The end of response stream sent by the server.
59
  case end(GRPCStatus, HPACKHeaders)
60
}
61
62
/// Metadata associated with a request or response message.
63
public struct MessageMetadata: Equatable, Sendable {
64
  /// Whether the message should be compressed. If compression has not been enabled on the RPC
65
  /// then this setting is ignored.
66
  public var compress: Bool
67
68
  /// Whether the underlying transported should be 'flushed' after writing this message. If a batch
69
  /// of messages is to be sent then flushing only after the last message may improve
70
  /// performance.
71
  public var flush: Bool
72
73
109M
  public init(compress: Bool, flush: Bool) {
74
109M
    self.compress = compress
75
109M
    self.flush = flush
76
109M
  }
77
}
78
79
extension GRPCClientResponsePart {
80
  @inlinable
81
0
  internal var isEnd: Bool {
82
0
    switch self {
83
0
    case .end:
84
0
      return true
85
0
    case .metadata, .message:
86
0
      return false
87
0
    }
88
0
  }
89
}
90
91
extension GRPCServerResponsePart {
92
  @inlinable
93
5.87M
  internal var isEnd: Bool {
94
5.87M
    switch self {
95
5.87M
    case .end:
96
517k
      return true
97
5.87M
    case .metadata, .message:
98
5.35M
      return false
99
5.87M
    }
100
5.87M
  }
101
}
102
103
extension GRPCClientRequestPart: Sendable where Request: Sendable {}
104
extension GRPCClientResponsePart: Sendable where Response: Sendable {}
105
extension GRPCServerRequestPart: Sendable where Request: Sendable {}
106
extension GRPCServerResponsePart: Sendable where Response: Sendable {}