Files
swift-nio-imap/Sources/NIOIMAP/Parser/Coders/ResponseDecoder.swift
T
David EvansandDavid Evans 9f871c5b70 Simplify body extension (#16)
* Condense body extension to be a single array

* Add tests for nested array condensing

* Finish parser support

* Resolve access warnings

Co-authored-by: David Evans <d.evans@apple.com>
2020-03-30 09:54:21 +01:00

50 lines
1.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
extension NIOIMAP {
public struct ResponseDecoder: ByteToMessageDecoder {
public typealias InboundOut = ResponseStream
var parser: ResponseParser
public init(bufferLimit: Int = 1_000) {
self.parser = ResponseParser(bufferLimit: bufferLimit)
}
public mutating func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
let save = buffer
do {
let result = try self.parser.parseResponseStream(buffer: &buffer)
context.fireChannelRead(self.wrapInboundOut(result))
return .continue
} catch NIOIMAP.ParsingError.incompleteMessage {
return .needMoreData
} catch {
throw IMAPDecoderError(parserError: error, buffer: save)
}
}
public mutating func decodeLast(context: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState {
while try self.decode(context: context, buffer: &buffer) != .needMoreData {}
return .needMoreData
}
}
}