mirror of
https://github.com/swift-server/RediStack.git
synced 2026-06-02 07:37:33 +00:00
Split NIO ChannelHandler conformances into separate files from RESP Encoder/Decoder
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import NIO
|
||||
|
||||
extension RESPDecoder: ByteToMessageDecoder {
|
||||
/// `ByteToMessageDecoder.InboundOut`
|
||||
public typealias InboundOut = RESPValue
|
||||
|
||||
/// See `ByteToMessageDecoder.decode(ctx:buffer:)`
|
||||
public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
|
||||
var position = 0
|
||||
|
||||
switch try parse(at: &position, from: &buffer) {
|
||||
case .notYetParsed:
|
||||
return .needMoreData
|
||||
|
||||
case .parsed(let RESPValue):
|
||||
ctx.fireChannelRead(wrapInboundOut(RESPValue))
|
||||
buffer.moveReaderIndex(forwardBy: position)
|
||||
return .continue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import NIO
|
||||
|
||||
extension RESPEncoder: MessageToByteEncoder {
|
||||
/// See `MessageToByteEncoder.OutboundIn`
|
||||
public typealias OutboundIn = RESPValue
|
||||
|
||||
/// See `RESPEncoder.encode(ctx:data:out:)`
|
||||
public func encode(ctx: ChannelHandlerContext, data: RESPValue, out: inout ByteBuffer) throws {
|
||||
out.write(bytes: encode(data))
|
||||
}
|
||||
}
|
||||
-20
@@ -186,23 +186,3 @@ public final class RESPDecoder {
|
||||
return .parsed(.array(values))
|
||||
}
|
||||
}
|
||||
|
||||
extension RESPDecoder: ByteToMessageDecoder {
|
||||
/// `ByteToMessageDecoder.InboundOut`
|
||||
public typealias InboundOut = RESPValue
|
||||
|
||||
/// See `ByteToMessageDecoder.decode(ctx:buffer:)`
|
||||
public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
|
||||
var position = 0
|
||||
|
||||
switch try parse(at: &position, from: &buffer) {
|
||||
case .notYetParsed:
|
||||
return .needMoreData
|
||||
|
||||
case .parsed(let RESPValue):
|
||||
ctx.fireChannelRead(wrapInboundOut(RESPValue))
|
||||
buffer.moveReaderIndex(forwardBy: position)
|
||||
return .continue
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-14
@@ -1,12 +1,11 @@
|
||||
import Foundation
|
||||
import NIO
|
||||
|
||||
/// Translates `RedisValue` into raw bytes, formatted according to the Redis Serialization Protocol (RESP).
|
||||
///
|
||||
/// See: https://redis.io/topics/protocol
|
||||
public final class RESPEncoder {
|
||||
public init() { }
|
||||
|
||||
|
||||
/// Encodes the `RedisValue` to bytes, following the RESP specification.
|
||||
///
|
||||
/// See https://redis.io/topics/protocol
|
||||
@@ -35,15 +34,3 @@ public final class RESPEncoder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: MessageToByteEncoder
|
||||
|
||||
extension RESPEncoder: MessageToByteEncoder {
|
||||
/// See `MessageToByteEncoder.OutboundIn`
|
||||
public typealias OutboundIn = RESPValue
|
||||
|
||||
/// See `RESPEncoder.encode(ctx:data:out:)`
|
||||
public func encode(ctx: ChannelHandlerContext, data: RESPValue, out: inout ByteBuffer) throws {
|
||||
out.write(bytes: encode(data))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user