mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-29 05:24:23 +00:00
* Support encoding append commands * Yay it works * Cleanup 1 * Cleanup 2 * Fix sanity * Fix command decoder test * Fix parsing partial commands sync literals * Fix parser stress tests * Fix tests * Remove unused code * Code refactor and cleanup * Fix sanity Co-authored-by: David Evans <d.evans@apple.com>
60 lines
1.9 KiB
Swift
60 lines
1.9 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 struct NIO.ByteBuffer
|
|
|
|
public enum AppendCommand: Equatable {
|
|
case start(tag: String, appendingTo: MailboxName)
|
|
case beginMessage(messsage: AppendMessage)
|
|
case messageBytes(ByteBuffer)
|
|
case endMessage
|
|
case finish
|
|
}
|
|
|
|
public enum CommandStream: Equatable {
|
|
case idleDone
|
|
case command(TaggedCommand)
|
|
case append(AppendCommand)
|
|
}
|
|
|
|
extension CommandEncodeBuffer {
|
|
@discardableResult public mutating func writeCommandStream(_ stream: CommandStream) throws -> Int {
|
|
switch stream {
|
|
case .idleDone:
|
|
return self.buffer.writeString("DONE\r\n")
|
|
case .command(let command):
|
|
return try self.buffer.writeCommand(command)
|
|
case .append(let command):
|
|
return try self.writeAppendCommand(command)
|
|
}
|
|
}
|
|
|
|
@discardableResult mutating func writeAppendCommand(_ command: AppendCommand) throws -> Int {
|
|
switch command {
|
|
case .start(tag: let tag, appendingTo: let mailbox):
|
|
return
|
|
self.buffer.writeString("\(tag) APPEND ") +
|
|
self.buffer.writeMailbox(mailbox)
|
|
case .beginMessage(messsage: let messsage):
|
|
return self.buffer.writeAppendMessage(messsage)
|
|
case .messageBytes(var bytes):
|
|
return self.buffer.writeBuffer(&bytes)
|
|
case .endMessage:
|
|
return 0
|
|
case .finish:
|
|
return self.buffer.writeString("\r\n")
|
|
}
|
|
}
|
|
}
|