-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathValkeyCommandEncoder.swift
More file actions
69 lines (57 loc) · 1.73 KB
/
ValkeyCommandEncoder.swift
File metadata and controls
69 lines (57 loc) · 1.73 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
//
// 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
/// A type that encodes Valkey commands into a buffer.
public struct ValkeyCommandEncoder {
@usableFromInline
package var buffer: ByteBuffer
@inlinable
package init() {
self.buffer = .init()
}
@inlinable
mutating func encodeIdentifier(_ identifier: RESPTypeIdentifier) {
self.buffer.writeInteger(identifier.rawValue)
}
@inlinable
mutating func encodeBulkString(_ string: String) {
encodeIdentifier(.bulkString)
buffer.writeString(String(string.utf8.count))
buffer.writeStaticString("\r\n")
buffer.writeString(string)
buffer.writeStaticString("\r\n")
}
@inlinable
mutating func encodeBulkString(_ string: Substring) {
encodeIdentifier(.bulkString)
buffer.writeString(String(string.utf8.count))
buffer.writeStaticString("\r\n")
buffer.writeSubstring(string)
buffer.writeStaticString("\r\n")
}
@inlinable
mutating func encodeBulkString<Bytes: Collection>(_ string: Bytes) where Bytes.Element == UInt8 {
encodeIdentifier(.bulkString)
buffer.writeString(String(string.count))
buffer.writeStaticString("\r\n")
buffer.writeBytes(string)
buffer.writeStaticString("\r\n")
}
@inlinable
var writerIndex: Int {
buffer.writerIndex
}
@inlinable
mutating func moveWriterIndex(to index: Int) {
buffer.moveWriterIndex(to: index)
}
@inlinable
package mutating func reset() {
self.buffer.clear()
}
}