Split NIO ChannelHandler conformances into separate files from RESP Encoder/Decoder

This commit is contained in:
Nathan Harris
2019-02-11 12:33:22 -08:00
parent 5bdb92967b
commit dd58fb2cfe
4 changed files with 33 additions and 34 deletions
@@ -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))
}
}
@@ -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,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))
}
}