Files
swift-nio-imap/Sources/NIOIMAPCore/Grammar/LastCommandSet.swift
T
David Evans 6191061fb4 Create generic type to wrap a set or IMAP "last result" (#463)
* Define last command set type

* Replace SequenceSet

* Docs

* Soundness

* Update dates

* Rename last command set

* Cleanup

* Update docs
2021-03-04 14:45:12 +00:00

42 lines
1.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
/// Defines an interface for a type to be able to serialize itself to an `EncodeBuffer`.
/// Avoid conforming to this protocol, and use the standard `EncodeBuffer.write` functions.
public protocol _IMAPEncodable: ExpressibleByArrayLiteral, Hashable {
/// Writes the set to an `inout EncodeBuffer`.
func writeIntoBuffer(_ buffer: inout EncodeBuffer) -> Int
}
/// Provides support for using either the result of the last command (`.lastCommand`) or
/// a concrete set type.
public enum LastCommandSet<SetType: _IMAPEncodable>: Hashable {
/// A specific set that will be sent to the IMAP server. E.g. `1, 2:5, 10:*`
case set(SetType)
/// Tells the server to use the result of the last command, described in RFC 5182.
case lastCommand
}
extension EncodeBuffer {
@discardableResult mutating func writeLastCommandSet<T>(_ set: LastCommandSet<T>) -> Int {
switch set {
case .lastCommand:
return self.writeString("$")
case .set(let set):
return set.writeIntoBuffer(&self)
}
}
}