diff --git a/Sources/NIORedis/ChannelHandlers/RESPDecoder+ByteToMessage.swift b/Sources/NIORedis/ChannelHandlers/RESPDecoder+ByteToMessage.swift new file mode 100644 index 0000000..51df498 --- /dev/null +++ b/Sources/NIORedis/ChannelHandlers/RESPDecoder+ByteToMessage.swift @@ -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 + } + } +} diff --git a/Sources/NIORedis/ChannelHandlers/RESPEncoder+MessageToByte.swift b/Sources/NIORedis/ChannelHandlers/RESPEncoder+MessageToByte.swift new file mode 100644 index 0000000..cbab14c --- /dev/null +++ b/Sources/NIORedis/ChannelHandlers/RESPEncoder+MessageToByte.swift @@ -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)) + } +} diff --git a/Sources/NIORedis/ChannelHandlers/RESPDecoder.swift b/Sources/NIORedis/RESP/RESPDecoder.swift similarity index 92% rename from Sources/NIORedis/ChannelHandlers/RESPDecoder.swift rename to Sources/NIORedis/RESP/RESPDecoder.swift index 8b4bce1..261446a 100644 --- a/Sources/NIORedis/ChannelHandlers/RESPDecoder.swift +++ b/Sources/NIORedis/RESP/RESPDecoder.swift @@ -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 - } - } -} diff --git a/Sources/NIORedis/ChannelHandlers/RESPEncoder.swift b/Sources/NIORedis/RESP/RESPEncoder.swift similarity index 76% rename from Sources/NIORedis/ChannelHandlers/RESPEncoder.swift rename to Sources/NIORedis/RESP/RESPEncoder.swift index 2b0d1cb..8c51e50 100644 --- a/Sources/NIORedis/ChannelHandlers/RESPEncoder.swift +++ b/Sources/NIORedis/RESP/RESPEncoder.swift @@ -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)) - } -}