mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-30 05:28:46 +00:00
Make writeIfExists / write(if:) free functions
As EncodeBuffer methods, `self.write(if:) { self.write… }` borrows the
buffer while the closure mutates it, forcing a copy and a CoW on the
first write inside the closure.
Hand edit: Sources/NIOIMAPCore/ByteBuffer+WriteLiteral.swift, which
defines the free functions. Call sites converted with:
git ls-files -z 'Sources/*.swift' 'Tests/*.swift' | xargs -0 perl -pi -e \
's/\b(?:self\.buffer|self|buffer)\.(writeIfExists\(|write\(if:)/$1/g'
This commit is contained in:
42 files changed
+119
-113
No files matched your search
@@ -197,24 +197,24 @@ extension EncodeBuffer {
|
||||
// TODO: This should probably check
|
||||
// collection.count != 0
|
||||
// such that an empty collection gets encoded as "()".
|
||||
self.write(if: collection.count > 0) {
|
||||
write(if: collection.count > 0) {
|
||||
self.writeString(prefix)
|
||||
}
|
||||
+ self.write(if: parenthesis) { () -> Int in
|
||||
+ write(if: parenthesis) { () -> Int in
|
||||
self.writeString("(")
|
||||
}
|
||||
+ collection.enumerated().reduce(0) { (size, row) in
|
||||
let (i, element) = row
|
||||
return
|
||||
size + writer(element, &self)
|
||||
+ self.write(if: i < collection.count - 1) { () -> Int in
|
||||
+ write(if: i < collection.count - 1) { () -> Int in
|
||||
self.writeString(separator)
|
||||
}
|
||||
}
|
||||
+ self.write(if: parenthesis) { () -> Int in
|
||||
+ write(if: parenthesis) { () -> Int in
|
||||
self.writeString(")")
|
||||
}
|
||||
+ self.write(if: collection.count > 0) {
|
||||
+ write(if: collection.count > 0) {
|
||||
self.writeString(suffix)
|
||||
}
|
||||
}
|
||||
@@ -239,52 +239,58 @@ extension EncodeBuffer {
|
||||
// TODO: This should probably check
|
||||
// collection.count != 0
|
||||
// such that an empty collection gets encoded as "()".
|
||||
self.write(if: values.count > 0) {
|
||||
write(if: values.count > 0) {
|
||||
self.writeString(prefix)
|
||||
}
|
||||
+ self.write(if: parenthesis) { () -> Int in
|
||||
+ write(if: parenthesis) { () -> Int in
|
||||
self.writeString("(")
|
||||
}
|
||||
+ values.enumerated().reduce(0) { (size, row) in
|
||||
let (i, element) = row
|
||||
return
|
||||
size + writer(.init(key: element.0, value: element.1), &self)
|
||||
+ self.write(if: i < values.count - 1) { () -> Int in
|
||||
+ write(if: i < values.count - 1) { () -> Int in
|
||||
self.writeString(separator)
|
||||
}
|
||||
}
|
||||
+ self.write(if: parenthesis) { () -> Int in
|
||||
+ write(if: parenthesis) { () -> Int in
|
||||
self.writeString(")")
|
||||
}
|
||||
+ self.write(if: values.count > 0) {
|
||||
+ write(if: values.count > 0) {
|
||||
self.writeString(suffix)
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes to self using a closure if the given `value` is non-`nil`. This allows for chaining together writes
|
||||
/// when attempting to perform composite writes and return the total number of bytes written.
|
||||
/// - parameters:
|
||||
/// - value: The optional field to evaluate.
|
||||
/// - writer: The closure to invoke if `value` is non-`nil`. If `value` exists then it is passed as
|
||||
/// the only argument to this closure.
|
||||
/// - returns: The number of bytes written.
|
||||
@discardableResult func writeIfExists<T>(_ value: T?, _ writer: (inout T) -> Int) -> Int {
|
||||
guard var value = value else {
|
||||
return 0
|
||||
}
|
||||
return writer(&value)
|
||||
}
|
||||
|
||||
/// Writes to self using a closure if a condition is met. This allows for chaining together writes
|
||||
/// when attempting to perform composite writes and return the total number of bytes written.
|
||||
/// - parameters:
|
||||
/// - condition: The condition to evaluate, if `true` then `writer` will be invoked.
|
||||
/// - writer: The closure to invoke if `condition` is met.
|
||||
/// - returns: The number of bytes written.
|
||||
@discardableResult func write(if condition: Bool, _ writer: () -> Int) -> Int {
|
||||
guard condition else {
|
||||
return 0
|
||||
}
|
||||
return writer()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Conditional writes
|
||||
|
||||
// Free functions, not `EncodeBuffer` methods: a method call `self.write(if: …) { self.write… }`
|
||||
// borrows `self` while the closure mutates it, so the compiler copies the buffer and the first write
|
||||
// inside the closure pays for copy-on-write.
|
||||
|
||||
/// Writes using a closure if the given `value` is non-`nil`. This allows for chaining together writes
|
||||
/// when attempting to perform composite writes and return the total number of bytes written.
|
||||
/// - parameters:
|
||||
/// - value: The optional field to evaluate.
|
||||
/// - writer: The closure to invoke if `value` is non-`nil`. If `value` exists then it is passed as
|
||||
/// the only argument to this closure.
|
||||
/// - returns: The number of bytes written.
|
||||
@discardableResult func writeIfExists<T>(_ value: T?, _ writer: (inout T) -> Int) -> Int {
|
||||
guard var value = value else {
|
||||
return 0
|
||||
}
|
||||
return writer(&value)
|
||||
}
|
||||
|
||||
/// Writes using a closure if a condition is met. This allows for chaining together writes
|
||||
/// when attempting to perform composite writes and return the total number of bytes written.
|
||||
/// - parameters:
|
||||
/// - condition: The condition to evaluate, if `true` then `writer` will be invoked.
|
||||
/// - writer: The closure to invoke if `condition` is met.
|
||||
/// - returns: The number of bytes written.
|
||||
@discardableResult func write(if condition: Bool, _ writer: () -> Int) -> Int {
|
||||
guard condition else {
|
||||
return 0
|
||||
}
|
||||
return writer()
|
||||
}
|
||||
@@ -89,10 +89,10 @@ public struct AppendOptions: Hashable, Sendable {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeAppendOptions(_ options: AppendOptions) -> Int {
|
||||
self.write(if: options.flagList.count >= 1) {
|
||||
write(if: options.flagList.count >= 1) {
|
||||
self.writeSpace() + self.writeFlags(options.flagList)
|
||||
}
|
||||
+ self.writeIfExists(options.internalDate) { (internalDate) -> Int in
|
||||
+ writeIfExists(options.internalDate) { (internalDate) -> Int in
|
||||
self.writeSpace() + self.writeInternalDate(internalDate)
|
||||
}
|
||||
+ self.writeOrderedDictionary(options.extensions, prefix: " ", parenthesis: false) { (ext, self) -> Int in
|
||||
|
||||
@@ -94,7 +94,7 @@ public struct AuthenticatedURLRump: Hashable, Sendable {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeAuthenticatedURLRump(_ data: AuthenticatedURLRump) -> Int {
|
||||
self.writeIfExists(data.expire) { expire in
|
||||
writeIfExists(data.expire) { expire in
|
||||
self.writeExpire(expire)
|
||||
} + self.writeString(";URLAUTH=") + self.writeAccess(data.access)
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ extension BodyStructure {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeBodyFieldLanguageLocation(_ langLoc: BodyStructure.LanguageLocation) -> Int {
|
||||
self.writeSpace() + self.writeBodyLanguages(langLoc.languages)
|
||||
+ self.writeIfExists(langLoc.location) { (location) -> Int in
|
||||
+ writeIfExists(langLoc.location) { (location) -> Int in
|
||||
self.writeBodyLocationAndExtensions(location)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ extension EncodeBuffer {
|
||||
_ locationExtension: BodyStructure.LocationAndExtensions
|
||||
) -> Int {
|
||||
self.writeSpace() + self.writeNString(locationExtension.location)
|
||||
+ self.write(if: !locationExtension.extensions.isEmpty) {
|
||||
+ write(if: !locationExtension.extensions.isEmpty) {
|
||||
self.writeSpace() + self.writeBodyExtensions(locationExtension.extensions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,14 +103,14 @@ extension EncodeBuffer {
|
||||
part.parts.reduce(into: 0) { (result, body) in
|
||||
result += self.writeBody(body)
|
||||
} + self.writeSpace() + self.writeMediaSubtype(part.mediaSubtype)
|
||||
+ self.writeIfExists(part.extension) { (ext) -> Int in
|
||||
+ writeIfExists(part.extension) { (ext) -> Int in
|
||||
self.writeSpace() + self.writeBodyExtensionMultipart(ext)
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult mutating func writeBodyExtensionMultipart(_ ext: BodyStructure.Multipart.Extension) -> Int {
|
||||
self.writeBodyParameterPairs(ext.parameters)
|
||||
+ self.writeIfExists(ext.dispositionAndLanguage) { (dspLanguage) -> Int in
|
||||
+ writeIfExists(ext.dispositionAndLanguage) { (dspLanguage) -> Int in
|
||||
self.writeBodyDispositionAndLanguage(dspLanguage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ extension EncodeBuffer {
|
||||
|
||||
@discardableResult mutating func writeBodyExtensionSinglePart(_ ext: BodyStructure.Singlepart.Extension) -> Int {
|
||||
self.writeNString(ext.digest)
|
||||
+ self.writeIfExists(ext.dispositionAndLanguage) { (dsp) -> Int in
|
||||
+ writeIfExists(ext.dispositionAndLanguage) { (dsp) -> Int in
|
||||
self.writeBodyDispositionAndLanguage(dsp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ extension EncodeBuffer {
|
||||
|
||||
@discardableResult mutating func writeByteRange(_ data: ByteRange) -> Int {
|
||||
self.writeString("\(data.offset)")
|
||||
+ self.writeIfExists(data.length) { length in
|
||||
+ writeIfExists(data.length) { length in
|
||||
self.writeString(".\(length)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -797,7 +797,7 @@ extension CommandEncodeBuffer {
|
||||
entries: [MetadataEntryName]
|
||||
) -> Int {
|
||||
self.buffer.writeString("GETMETADATA")
|
||||
+ self.buffer.write(if: options.count >= 1) {
|
||||
+ write(if: options.count >= 1) {
|
||||
buffer.writeSpace() + buffer.writeMetadataOptions(options)
|
||||
} + self.buffer.writeSpace() + self.buffer.writeMailbox(mailbox) + self.buffer.writeSpace()
|
||||
+ self.buffer.writeEntries(entries)
|
||||
@@ -825,7 +825,7 @@ extension CommandEncodeBuffer {
|
||||
|
||||
private mutating func writeCommandKind_create(mailbox: MailboxName, parameters: [CreateParameter]) -> Int {
|
||||
self.buffer.writeString("CREATE ") + self.buffer.writeMailbox(mailbox)
|
||||
+ self.buffer.write(if: parameters.count > 0) {
|
||||
+ write(if: parameters.count > 0) {
|
||||
self.buffer.writeSpace()
|
||||
+ self.buffer.writeArray(parameters, separator: "", parenthesis: true) { (param, buffer) -> Int in
|
||||
buffer.writeCreateParameter(param)
|
||||
@@ -849,11 +849,11 @@ extension CommandEncodeBuffer {
|
||||
returnOptions: [ReturnOption]
|
||||
) -> Int {
|
||||
self.buffer.writeString("LIST")
|
||||
+ self.buffer.writeIfExists(selectOptions) { (options) -> Int in
|
||||
+ writeIfExists(selectOptions) { (options) -> Int in
|
||||
self.buffer.writeSpace() + self.buffer.writeListSelectOptions(options)
|
||||
} + self.buffer.writeSpace() + self.buffer.writeMailbox(mailbox) + self.buffer.writeSpace()
|
||||
+ self.buffer.writeMailboxPatterns(mailboxPatterns)
|
||||
+ self.buffer.write(if: returnOptions.count >= 1) {
|
||||
+ write(if: returnOptions.count >= 1) {
|
||||
self.buffer.writeSpace() + self.buffer.writeListReturnOptions(returnOptions)
|
||||
}
|
||||
}
|
||||
@@ -865,14 +865,14 @@ extension CommandEncodeBuffer {
|
||||
returnOptions: [ReturnOption]
|
||||
) -> Int {
|
||||
self.buffer.writeString("LIST")
|
||||
+ self.buffer.write(if: selectOptions.count >= 1) {
|
||||
+ write(if: selectOptions.count >= 1) {
|
||||
self.buffer.writeSpace()
|
||||
+ self.buffer.writeArray(selectOptions) { element, buffer in
|
||||
buffer.writeListSelectIndependentOption(element)
|
||||
}
|
||||
} + self.buffer.writeSpace() + self.buffer.writeMailbox(mailbox) + self.buffer.writeSpace()
|
||||
+ self.buffer.writeMailboxPatterns(mailboxPatterns)
|
||||
+ self.buffer.write(if: returnOptions.count >= 1) {
|
||||
+ write(if: returnOptions.count >= 1) {
|
||||
self.buffer.writeSpace() + self.buffer.writeListReturnOptions(returnOptions)
|
||||
}
|
||||
}
|
||||
@@ -889,7 +889,7 @@ extension CommandEncodeBuffer {
|
||||
) -> Int {
|
||||
self.buffer.writeString("RENAME ") + self.buffer.writeMailbox(from) + self.buffer.writeSpace()
|
||||
+ self.buffer.writeMailbox(to)
|
||||
+ self.buffer.writeIfExists(parameters) { (params) -> Int in
|
||||
+ writeIfExists(parameters) { (params) -> Int in
|
||||
self.buffer.writeParameters(params)
|
||||
}
|
||||
}
|
||||
@@ -918,7 +918,7 @@ extension CommandEncodeBuffer {
|
||||
|
||||
private mutating func writeCommandKind_uidBatches(batchSize: Int, batchRange: ClosedRange<Int>?) -> Int {
|
||||
self.buffer.writeString("UIDBATCHES \(batchSize)")
|
||||
+ self.buffer.writeIfExists(batchRange) {
|
||||
+ writeIfExists(batchRange) {
|
||||
let range =
|
||||
UnknownMessageIdentifier(exactly: $0.lowerBound)!...UnknownMessageIdentifier(
|
||||
exactly: $0.upperBound
|
||||
@@ -942,7 +942,7 @@ extension CommandEncodeBuffer {
|
||||
initialResponse: InitialResponse?
|
||||
) -> Int {
|
||||
self.buffer.writeString("AUTHENTICATE ") + self.buffer.writeAuthenticationMechanism(mechanism)
|
||||
+ self.buffer.writeIfExists(initialResponse) { resp in
|
||||
+ writeIfExists(initialResponse) { resp in
|
||||
var c = self.buffer.writeSpace()
|
||||
if self.buffer.loggingMode {
|
||||
c += self.buffer.writeString("∅")
|
||||
@@ -1031,7 +1031,7 @@ extension CommandEncodeBuffer {
|
||||
data: StoreData
|
||||
) -> Int {
|
||||
self.buffer.writeString("STORE ") + self.buffer.writeLastCommandSet(set)
|
||||
+ self.buffer.write(if: modifiers.count >= 1) {
|
||||
+ write(if: modifiers.count >= 1) {
|
||||
self.buffer.writeSpace()
|
||||
+ self.buffer.writeArray(modifiers) { (element, buffer) -> Int in
|
||||
buffer.writeStoreModifier(element)
|
||||
@@ -1045,7 +1045,7 @@ extension CommandEncodeBuffer {
|
||||
data: StoreData
|
||||
) -> Int {
|
||||
self.buffer.writeString("UID STORE ") + self.buffer.writeLastCommandSet(set)
|
||||
+ self.buffer.write(if: modifiers.count >= 1) {
|
||||
+ write(if: modifiers.count >= 1) {
|
||||
self.buffer.writeStoreModifiers(modifiers)
|
||||
} + self.buffer.writeSpace() + self.buffer.writeStoreData(data)
|
||||
}
|
||||
@@ -1056,11 +1056,11 @@ extension CommandEncodeBuffer {
|
||||
returnOptions: [SearchReturnOption] = []
|
||||
) -> Int {
|
||||
self.buffer.writeString("SEARCH")
|
||||
+ self.buffer.writeIfExists(returnOptions) { (options) -> Int in
|
||||
+ writeIfExists(returnOptions) { (options) -> Int in
|
||||
self.buffer.writeSearchReturnOptions(options)
|
||||
} + self.buffer.writeSpace()
|
||||
+ self.buffer.write(if: key.usesString && options.useSearchCharset) {
|
||||
self.buffer.writeIfExists(charset) { (charset) -> Int in
|
||||
+ write(if: key.usesString && options.useSearchCharset) {
|
||||
writeIfExists(charset) { (charset) -> Int in
|
||||
self.buffer.writeString("CHARSET ") + self.buffer.writeCharacterSetName(charset)
|
||||
+ self.buffer.writeSpace()
|
||||
}
|
||||
@@ -1083,7 +1083,7 @@ extension CommandEncodeBuffer {
|
||||
returnOptions: [SearchReturnOption] = []
|
||||
) -> Int {
|
||||
self.buffer.writeString("SORT")
|
||||
+ self.buffer.writeIfExists(returnOptions) { (options) -> Int in
|
||||
+ writeIfExists(returnOptions) { (options) -> Int in
|
||||
self.buffer.writeSearchReturnOptions(options)
|
||||
} + self.buffer.writeSpace()
|
||||
+ self.buffer.writeSortCriteria(criteria)
|
||||
|
||||
@@ -343,11 +343,11 @@ extension CommandEncodeBuffer {
|
||||
self.encodedAtLeastOneCatenateElement = true
|
||||
}
|
||||
|
||||
return self.buffer.write(if: self.encodedAtLeastOneCatenateElement) { self.buffer.writeSpace() }
|
||||
return write(if: self.encodedAtLeastOneCatenateElement) { self.buffer.writeSpace() }
|
||||
+ self.buffer.writeString("URL ") + self.buffer.writeIMAPString(url)
|
||||
case .catenateData(.begin(let size)):
|
||||
var written =
|
||||
self.buffer.write(if: self.encodedAtLeastOneCatenateElement) { self.buffer.writeSpace() }
|
||||
write(if: self.encodedAtLeastOneCatenateElement) { self.buffer.writeSpace() }
|
||||
+ self.buffer.writeString("TEXT ")
|
||||
|
||||
if self.options.useNonSynchronizingLiteralPlus {
|
||||
|
||||
@@ -93,13 +93,13 @@ public struct ExtendedSearchOptions: Hashable, Sendable {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeExtendedSearchOptions(_ options: ExtendedSearchOptions) -> Int {
|
||||
self.writeIfExists(options.sourceOptions) { (options) -> Int in
|
||||
writeIfExists(options.sourceOptions) { (options) -> Int in
|
||||
self.writeSpace() + self.writeExtendedSearchSourceOptions(options)
|
||||
}
|
||||
+ self.writeIfExists(options.returnOptions) { (options) -> Int in
|
||||
+ writeIfExists(options.returnOptions) { (options) -> Int in
|
||||
self.writeSearchReturnOptions(options)
|
||||
} + self.writeSpace()
|
||||
+ self.writeIfExists(options.charset) { (charset) -> Int in
|
||||
+ writeIfExists(options.charset) { (charset) -> Int in
|
||||
self.writeString("CHARSET ") + self.writeCharacterSetName(charset) + self.writeSpace()
|
||||
} + self.writeSearchKey(options.key)
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ extension EncodeBuffer {
|
||||
+ self.writeArray(options.sourceMailbox, parenthesis: false) { (filter, buffer) -> Int in
|
||||
buffer.writeMailboxFilter(filter)
|
||||
}
|
||||
+ self.writeIfExists(options.scopeOptions) { scopeOptions in
|
||||
+ writeIfExists(options.scopeOptions) { scopeOptions in
|
||||
self.writeString(" (") + self.writeExtendedSearchScopeOptions(scopeOptions) + self.writeString(")")
|
||||
} + self.writeString(")")
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public struct EncodedSearchQuery: Hashable, Sendable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeEncodedSearchQuery(_ ref: EncodedSearchQuery) -> Int {
|
||||
self.writeEncodedMailboxUIDValidity(ref.mailboxUIDValidity)
|
||||
+ self.writeIfExists(ref.encodedSearch) { search in
|
||||
+ writeIfExists(ref.encodedSearch) { search in
|
||||
self.writeString("?") + self.writeEncodedSearch(search)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +392,7 @@ extension EncodeBuffer {
|
||||
partial: ClosedRange<UInt32>?
|
||||
) -> Int {
|
||||
self.writeString(peek ? "BODY.PEEK" : "BODY") + self.writeSection(section)
|
||||
+ self.writeIfExists(partial) { (partial) -> Int in
|
||||
+ writeIfExists(partial) { (partial) -> Int in
|
||||
self.writeByteRange(partial)
|
||||
}
|
||||
}
|
||||
@@ -407,10 +407,10 @@ extension EncodeBuffer {
|
||||
partial: ClosedRange<UInt32>?
|
||||
) -> Int {
|
||||
self.writeString("BINARY")
|
||||
+ self.write(if: peek) {
|
||||
+ write(if: peek) {
|
||||
self.writeString(".PEEK")
|
||||
} + self.writeSectionBinary(section)
|
||||
+ self.writeIfExists(partial) { (partial) -> Int in
|
||||
+ writeIfExists(partial) { (partial) -> Int in
|
||||
self.writeByteRange(partial)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ extension EncodeBuffer {
|
||||
let minute = self.padInteger(data.minute, minimum: 2)
|
||||
let second = self.padInteger(data.second, minimum: 2)
|
||||
return self.writeString("\(hour):\(minute):\(second)")
|
||||
+ self.writeIfExists(data.fraction) { fraction in
|
||||
+ writeIfExists(data.fraction) { fraction in
|
||||
self.writeString(".\(fraction.stringValue)")
|
||||
} + self.writeTimeOffset(data.offset)
|
||||
}
|
||||
|
||||
@@ -276,10 +276,10 @@ extension IMAPServer.Host: ExpressibleByStringLiteral {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeIMAPServer(_ server: IMAPServer) -> Int {
|
||||
self.writeIfExists(server.userAuthenticationMechanism) { authMechanism in
|
||||
writeIfExists(server.userAuthenticationMechanism) { authMechanism in
|
||||
self.writeUserAuthenticationMechanism(authMechanism) + self.writeString("@")
|
||||
} + self.writeString(server.host.stringValue)
|
||||
+ self.writeIfExists(server.port) { port in
|
||||
+ writeIfExists(server.port) { port in
|
||||
self.writeString(":\(port)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public struct IMAPURL: Hashable, Sendable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeIMAPURL(_ url: IMAPURL) -> Int {
|
||||
self.writeString("imap://") + self.writeIMAPServer(url.server) + self.writeString("/")
|
||||
+ self.writeIfExists(url.command) { command in
|
||||
+ writeIfExists(url.command) { command in
|
||||
self.writeURLCommand(command)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import struct NIO.ByteBuffer
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeListReturnOptions(_ options: [ReturnOption]) -> Int {
|
||||
self.writeString("RETURN ") + self.writeString("(")
|
||||
+ self.write(if: options.count >= 1) {
|
||||
+ write(if: options.count >= 1) {
|
||||
self.writeArray(options, parenthesis: false) { (option, self) in
|
||||
self.writeReturnOption(option)
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ extension EncodeBuffer {
|
||||
|
||||
@discardableResult mutating func writeListSelectOptions(_ options: ListSelectOptions?) -> Int {
|
||||
self.writeString("(")
|
||||
+ self.writeIfExists(options) { (optionsData) -> Int in
|
||||
+ writeIfExists(options) { (optionsData) -> Int in
|
||||
self.writeArray(optionsData.options, separator: "", parenthesis: false) { (option, self) -> Int in
|
||||
self.writeListSelectOption(option) + self.writeSpace()
|
||||
} + self.writeListSelectBaseOption(optionsData.baseOption)
|
||||
|
||||
@@ -218,7 +218,7 @@ extension EncodeBuffer {
|
||||
+ self.writeArray(list, prefix: " ", separator: " ", parenthesis: false) { (id, buffer) -> Int in
|
||||
buffer.writeMessageIdentifier(id)
|
||||
}
|
||||
+ self.writeIfExists(modificationSequence) { value -> Int in
|
||||
+ writeIfExists(modificationSequence) { value -> Int in
|
||||
self.writeString(" (MODSEQ ") + self.writeModificationSequenceValue(value) + self.writeString(")")
|
||||
}
|
||||
}
|
||||
@@ -231,7 +231,7 @@ extension EncodeBuffer {
|
||||
+ self.writeArray(list, prefix: " ", separator: " ", parenthesis: false) { (id, buffer) -> Int in
|
||||
buffer.writeMessageIdentifier(id)
|
||||
}
|
||||
+ self.writeIfExists(modificationSequence) { value -> Int in
|
||||
+ writeIfExists(modificationSequence) { value -> Int in
|
||||
self.writeString(" (MODSEQ ") + self.writeModificationSequenceValue(value) + self.writeString(")")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,11 +294,11 @@ extension EncodeBuffer {
|
||||
|
||||
@discardableResult mutating func writeMailboxInfo(_ list: MailboxInfo) -> Int {
|
||||
self.writeString("(")
|
||||
+ self.writeIfExists(list.attributes) { (flags) -> Int in
|
||||
+ writeIfExists(list.attributes) { (flags) -> Int in
|
||||
self.writeMailboxListFlags(flags)
|
||||
} + self.writeString(") ") + self.writeMailboxPathSeparator(list.path.pathSeparator) + self.writeSpace()
|
||||
+ self.writeMailbox(list.path.name)
|
||||
+ self.write(if: list.objectID != nil || !list.extensions.isEmpty) {
|
||||
+ write(if: list.objectID != nil || !list.extensions.isEmpty) {
|
||||
self.writeSpace() + self.writeMailboxListExtended(objectID: list.objectID, extensions: list.extensions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public struct MailboxUIDValidity: Hashable, Sendable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeEncodedMailboxUIDValidity(_ ref: MailboxUIDValidity) -> Int {
|
||||
self.writeEncodedMailbox(ref.encodedMailbox)
|
||||
+ self.writeIfExists(ref.uidValidity) { value in
|
||||
+ writeIfExists(ref.uidValidity) { value in
|
||||
self.writeString(";UIDVALIDITY=") + self.writeUIDValidity(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public struct MechanismBase64: Hashable, Sendable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeMechanismBase64(_ data: MechanismBase64) -> Int {
|
||||
self.writeURLAuthenticationMechanism(data.mechanism)
|
||||
+ self.writeIfExists(data.base64) { base64 in
|
||||
+ writeIfExists(data.base64) { base64 in
|
||||
// mechanism-base64 = mechanism ["=" base64]; the parser decodes, so encode here.
|
||||
self.writeString("=") + self.writeBufferAsBase64(base64)
|
||||
}
|
||||
|
||||
@@ -346,7 +346,7 @@ extension EncodeBuffer {
|
||||
hasExtensionData: Bool
|
||||
) -> Int {
|
||||
self.writeString("BODY")
|
||||
+ self.write(if: hasExtensionData) { () -> Int in
|
||||
+ write(if: hasExtensionData) { () -> Int in
|
||||
self.writeString("STRUCTURE")
|
||||
} + self.writeSpace() + self.writeBody(body)
|
||||
}
|
||||
@@ -357,14 +357,14 @@ extension EncodeBuffer {
|
||||
string: ByteBuffer?
|
||||
) -> Int {
|
||||
self.writeString("BODY") + self.writeSection(section)
|
||||
+ self.writeIfExists(number) { (number) -> Int in
|
||||
+ writeIfExists(number) { (number) -> Int in
|
||||
self.writeString("<\(number)>")
|
||||
} + self.writeSpace() + self.writeNString(string)
|
||||
}
|
||||
|
||||
@discardableResult mutating func writeMessageAttribute_bodySectionText(number: Int?, size: Int) -> Int {
|
||||
self.writeString("BODY[TEXT]")
|
||||
+ self.writeIfExists(number) { (number) -> Int in
|
||||
+ writeIfExists(number) { (number) -> Int in
|
||||
self.writeString("<\(number)>")
|
||||
} + self.writeString(" {\(size)}\r\n")
|
||||
}
|
||||
|
||||
@@ -109,10 +109,10 @@ public struct MessagePath: Hashable, Sendable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeMessagePath(_ data: MessagePath) -> Int {
|
||||
self.writeEncodedMailboxUIDValidity(data.mailboxReference) + self.writeIUID(data.iUID)
|
||||
+ self.writeIfExists(data.section) { section in
|
||||
+ writeIfExists(data.section) { section in
|
||||
self.writeURLMessageSection(section)
|
||||
}
|
||||
+ self.writeIfExists(data.range) { partial in
|
||||
+ writeIfExists(data.range) { partial in
|
||||
self.writeMessagePathByteRange(partial)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,13 +241,13 @@ extension ExtendedSearchResponse {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeExtendedSearchResponse(_ response: ExtendedSearchResponse) -> Int {
|
||||
self.writeString("ESEARCH")
|
||||
+ self.writeIfExists(response.correlator) { (correlator) -> Int in
|
||||
+ writeIfExists(response.correlator) { (correlator) -> Int in
|
||||
self.writeSearchCorrelator(correlator)
|
||||
}
|
||||
+ self.write(if: response.kind == .uid) {
|
||||
+ write(if: response.kind == .uid) {
|
||||
self.writeString(" UID")
|
||||
}
|
||||
+ self.write(if: response.returnData.count > 0) {
|
||||
+ write(if: response.returnData.count > 0) {
|
||||
self.writeSpace()
|
||||
}
|
||||
+ self.writeArray(response.returnData, parenthesis: false) { (data, self) in
|
||||
|
||||
@@ -70,7 +70,7 @@ public struct ResponseText: Hashable, Sendable {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeResponseText(_ text: ResponseText) -> Int {
|
||||
self.writeIfExists(text.code) { (code) -> Int in
|
||||
writeIfExists(text.code) { (code) -> Int in
|
||||
self.writeString("[") + self.writeResponseTextCode(code) + self.writeString("] ")
|
||||
}
|
||||
|
||||
|
||||
@@ -592,7 +592,7 @@ extension EncodeBuffer {
|
||||
|
||||
private mutating func writeResponseTextCode_badCharsets(_ charsets: [CharacterSetName]) -> Int {
|
||||
self.writeString("BADCHARSET")
|
||||
+ self.write(if: charsets.count >= 1) {
|
||||
+ write(if: charsets.count >= 1) {
|
||||
self.writeSpace()
|
||||
+ self.writeArray(charsets) { (charset, self) in
|
||||
self.writeCharacterSetName(charset)
|
||||
@@ -602,7 +602,7 @@ extension EncodeBuffer {
|
||||
|
||||
private mutating func writeResponseTextCode_other(atom: String, string: String?) -> Int {
|
||||
self.writeString(atom)
|
||||
+ self.writeIfExists(string) { (string) -> Int in
|
||||
+ writeIfExists(string) { (string) -> Int in
|
||||
self.writeString(" \(string)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ extension EncodeBuffer {
|
||||
@discardableResult mutating func writeUIDBatchesResponse(_ response: UIDBatchesResponse) -> Int {
|
||||
self.writeString(#"UIDBATCHES"#)
|
||||
+ self.writeSearchCorrelator(response.correlator)
|
||||
+ self.write(if: !response.batches.isEmpty) {
|
||||
+ write(if: !response.batches.isEmpty) {
|
||||
self.writeString(" ")
|
||||
+ self.writeArray(response.batches, separator: ",", parenthesis: false) { range, buffer -> Int in
|
||||
buffer.writeMessageIdentifierRange(range, descending: true)
|
||||
|
||||
@@ -78,10 +78,10 @@ extension EncodeBuffer {
|
||||
@discardableResult mutating func writeSearchCorrelator(_ correlator: SearchCorrelator) -> Int {
|
||||
// search-correlator = SP "(" "TAG" SP tag-string ")", tag-string = string
|
||||
self.writeString(" (TAG ") + self.writeIMAPString(correlator.tag)
|
||||
+ self.writeIfExists(correlator.mailbox) { mailbox in
|
||||
+ writeIfExists(correlator.mailbox) { mailbox in
|
||||
self.writeString(" MAILBOX ") + self.writeMailbox(mailbox)
|
||||
}
|
||||
+ self.writeIfExists(correlator.uidValidity) { uidValidity in
|
||||
+ writeIfExists(correlator.uidValidity) { uidValidity in
|
||||
self.writeString(" UIDVALIDITY ") + self.writeUIDValidity(uidValidity)
|
||||
} + self.writeString(")")
|
||||
}
|
||||
|
||||
@@ -640,7 +640,7 @@ extension EncodeBuffer {
|
||||
let (i, key) = row
|
||||
return
|
||||
size + self.writeSearchKey(key, encloseInParenthesisIfNeeded: true)
|
||||
+ self.write(if: i < keys.count - 1) { () -> Int in
|
||||
+ write(if: i < keys.count - 1) { () -> Int in
|
||||
self.writeString(" ")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ extension EncodeBuffer {
|
||||
// servers will fail to parse this correctly.
|
||||
return
|
||||
self.writeString(" RETURN (")
|
||||
+ self.writeIfExists(options) { (options) -> Int in
|
||||
+ writeIfExists(options) { (options) -> Int in
|
||||
self.writeArray(options, parenthesis: false) { (option, self) in
|
||||
self.writeSearchReturnOption(option)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ extension EncodeBuffer {
|
||||
_ option: KeyValue<ExtensionKey<SearchReturnOption>, ParameterValue?>
|
||||
) -> Int {
|
||||
self.writeExtensionKey(option.key)
|
||||
+ self.writeIfExists(option.value) { (params) -> Int in
|
||||
+ writeIfExists(option.value) { (params) -> Int in
|
||||
self.writeSpace() + self.writeParameterValue(params)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,7 +354,7 @@ extension EncodeBuffer {
|
||||
|
||||
@discardableResult mutating func writeSection(_ section: SectionSpecifier?) -> Int {
|
||||
self.writeString("[")
|
||||
+ self.writeIfExists(section) { (spec) -> Int in
|
||||
+ writeIfExists(section) { (spec) -> Int in
|
||||
self.writeSectionSpecifier(spec)
|
||||
} + self.writeString("]")
|
||||
}
|
||||
@@ -365,7 +365,7 @@ extension EncodeBuffer {
|
||||
}
|
||||
|
||||
return self.writeSectionPart(spec.part)
|
||||
+ self.writeIfExists(spec.kind) { (kind) -> Int in
|
||||
+ writeIfExists(spec.kind) { (kind) -> Int in
|
||||
self.writeSectionKind(kind, dot: spec.part.array.count > 0 && spec.kind != .complete)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ extension EncodeBuffer {
|
||||
return self.writeString("CONDSTORE")
|
||||
case .objectID(let compound):
|
||||
return self.writeString("OBJECTID")
|
||||
+ self.writeIfExists(compound) { (compound) -> Int in
|
||||
+ writeIfExists(compound) { (compound) -> Int in
|
||||
self.writeSpace() + self.writeCompoundObjectID(compound)
|
||||
}
|
||||
}
|
||||
@@ -189,10 +189,10 @@ extension EncodeBuffer {
|
||||
@discardableResult mutating func writeQResyncParameter(param: QResyncParameter) -> Int {
|
||||
self.writeString("QRESYNC (\(param.uidValidity.rawValue) ")
|
||||
+ self.writeModificationSequenceValue(param.modificationSequenceValue)
|
||||
+ self.writeIfExists(param.knownUIDs) { (set) -> Int in
|
||||
+ writeIfExists(param.knownUIDs) { (set) -> Int in
|
||||
self.writeSpace() + self.writeUIDSet(set)
|
||||
}
|
||||
+ self.writeIfExists(param.sequenceMatchData) { (data) -> Int in
|
||||
+ writeIfExists(param.sequenceMatchData) { (data) -> Int in
|
||||
self.writeSpace() + self.writeSequenceMatchData(data)
|
||||
} + self.writeString(")")
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import struct NIO.ByteBuffer
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeSequenceRange(_ range: SequenceRange) -> Int {
|
||||
self.writeSequenceNumberOrWildcard(range.range.lowerBound)
|
||||
+ self.write(if: range.range.lowerBound < range.range.upperBound) {
|
||||
+ write(if: range.range.lowerBound < range.range.upperBound) {
|
||||
self.writeString(":") + self.writeSequenceNumberOrWildcard(range.range.upperBound)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ extension EncodeBuffer {
|
||||
_ param: KeyValue<ExtensionKey<Kind>, ParameterValue?>
|
||||
) -> Int {
|
||||
self.writeExtensionKey(param.key)
|
||||
+ self.writeIfExists(param.value) { (value) -> Int in
|
||||
+ writeIfExists(param.value) { (value) -> Int in
|
||||
self.writeSpace() + self.writeParameterValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import struct NIO.ByteBuffer
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeTaggedExtensionComp(_ comp: [String]) -> Int {
|
||||
self.write(if: comp.count >= 1) {
|
||||
write(if: comp.count >= 1) {
|
||||
self.writeArray(comp) { (string, self) in
|
||||
self.writeIMAPString(string)
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ extension EncodeBuffer {
|
||||
let a = descending ? range.range.upperBound : range.range.lowerBound
|
||||
let b = descending ? range.range.lowerBound : range.range.upperBound
|
||||
return self.writeMessageIdentifier(a)
|
||||
+ self.write(if: range.range.lowerBound < range.range.upperBound) {
|
||||
+ write(if: range.range.lowerBound < range.range.upperBound) {
|
||||
self.writeString(":") + self.writeMessageIdentifier(b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ extension EncodeBuffer {
|
||||
return self.writeEncodedSearchQuery(list)
|
||||
case .fetch(path: let path, authenticatedURL: let authenticatedURL):
|
||||
return self.writeMessagePath(path)
|
||||
+ self.writeIfExists(authenticatedURL) { authenticatedURL in
|
||||
+ writeIfExists(authenticatedURL) { authenticatedURL in
|
||||
self.writeIAuthenticatedURL(authenticatedURL)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,10 +82,10 @@ public struct UserAuthenticationMechanism: Hashable, Sendable {
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeUserAuthenticationMechanism(_ data: UserAuthenticationMechanism) -> Int {
|
||||
self.writeIfExists(data.encodedUser) { user in
|
||||
writeIfExists(data.encodedUser) { user in
|
||||
self.writeEncodedUser(user)
|
||||
}
|
||||
+ self.writeIfExists(data.authenticationMechanism) { iAuth in
|
||||
+ writeIfExists(data.authenticationMechanism) { iAuth in
|
||||
self.writeIMAPURLAuthenticationMechanism(iAuth)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ extension EncodeBuffer {
|
||||
return self.writeString("BINARY")
|
||||
case .body(let section, let offset):
|
||||
return self.writeString("BODY") + self.writeSection(section)
|
||||
+ self.writeIfExists(offset) { offset in
|
||||
+ writeIfExists(offset) { offset in
|
||||
self.writeString("<\(offset)>")
|
||||
}
|
||||
case .rfc822:
|
||||
|
||||
Reference in new issue
Block a user