-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRESPRenderableHelpers.swift
More file actions
185 lines (170 loc) · 5.17 KB
/
RESPRenderableHelpers.swift
File metadata and controls
185 lines (170 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// This source file is part of the valkey-swift project
// Copyright (c) 2025 the valkey-swift project authors
//
// See LICENSE.txt for license information
// SPDX-License-Identifier: Apache-2.0
//
import NIOCore
/// Render token if boolean is true
@usableFromInline
package struct RESPPureToken: RESPRenderable {
@usableFromInline
let token: String?
@inlinable
package init(_ token: String, _ value: Bool) {
if value {
self.token = token
} else {
self.token = nil
}
}
@inlinable
package var respEntries: Int {
self.token != nil ? 1 : 0
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
self.token.encode(into: &commandEncoder)
}
}
/// Render token and value if value is not nil
@usableFromInline
package struct RESPWithToken<Value: RESPRenderable>: RESPRenderable {
@usableFromInline
let value: Value?
@usableFromInline
let token: String
@inlinable
package init(_ token: String, _ value: Value?) {
self.value = value
self.token = token
}
@inlinable
package var respEntries: Int {
guard let value = self.value, value.respEntries > 0 else { return 0 }
return value.respEntries + 1
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
guard let value, value.respEntries > 0 else {
return
}
self.token.encode(into: &commandEncoder)
value.encode(into: &commandEncoder)
}
}
/// Render array with token preceding each item
@usableFromInline
package struct RESPArrayWithToken<Element: RESPRenderable>: RESPRenderable {
@usableFromInline
let array: [Element]
@usableFromInline
let token: String
@inlinable
package init(_ token: String, _ array: [Element]) {
self.array = array
self.token = token
}
@inlinable
package var respEntries: Int {
self.array.respEntries + self.array.count
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
for element in self.array {
token.encode(into: &commandEncoder)
element.encode(into: &commandEncoder)
}
}
}
/// Render array item count and then array
@usableFromInline
package struct RESPArrayWithCount<Element: RESPRenderable>: RESPRenderable {
@usableFromInline
let array: [Element]
@inlinable
package init(_ array: [Element]) {
self.array = array
}
@inlinable
package var respEntries: Int {
self.array.respEntries + 1
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
self.array.count.encode(into: &commandEncoder)
self.array.encode(into: &commandEncoder)
}
}
/// Render number of resp parameters array generates and then array
@usableFromInline
package struct RESPArrayWithParameterCount<Element: RESPRenderable>: RESPRenderable {
@usableFromInline
let array: [Element]
@inlinable
package init(_ array: [Element]) {
self.array = array
}
@inlinable
package var respEntries: Int {
self.array.respEntries + 1
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
self.array.respEntries.encode(into: &commandEncoder)
self.array.encode(into: &commandEncoder)
}
}
/// Render token, count of items in array and array, but only if array is non-empty
@usableFromInline
package struct RESPArrayWithTokenAndCount<Element: RESPRenderable>: RESPRenderable {
@usableFromInline
let array: [Element]
@usableFromInline
let token: String
@inlinable
package init(_ token: String, _ array: [Element]) {
self.token = token
self.array = array
}
@inlinable
package var respEntries: Int {
guard self.array.count > 0 else { return 0 }
return self.array.respEntries + 2
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
guard self.array.count > 0 else { return }
self.token.encode(into: &commandEncoder)
self.array.count.encode(into: &commandEncoder)
self.array.encode(into: &commandEncoder)
}
}
/// Render token, count of parameters generated by array and array, but only if array is non-empty
@usableFromInline
package struct RESPArrayWithTokenAndParameterCount<Element: RESPRenderable>: RESPRenderable {
@usableFromInline
let array: [Element]
@usableFromInline
let token: String
@inlinable
package init(_ token: String, _ array: [Element]) {
self.token = token
self.array = array
}
@inlinable
package var respEntries: Int {
let respEntries = self.array.respEntries
guard respEntries > 0 else { return 0 }
return respEntries + 2
}
@inlinable
package func encode(into commandEncoder: inout ValkeyCommandEncoder) {
let respEntries = self.array.respEntries
guard respEntries > 0 else { return }
self.token.encode(into: &commandEncoder)
respEntries.encode(into: &commandEncoder)
self.array.encode(into: &commandEncoder)
}
}