Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleggert committed Dec 30, 2024
1 parent 2dd0091 commit 2e7f589
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 41 deletions.
17 changes: 11 additions & 6 deletions Sources/NIOIMAPCore/Grammar/Command/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,17 @@ extension CommandEncodeBuffer {
}

private mutating func writeCommandKind_uidBatches(batchSize: Int, batchRange: ClosedRange<Int>?) -> Int {
self.buffer.writeString("UIDBATCHES \(batchSize)") + self.buffer.writeIfExists(batchRange) {
let range = UnknownMessageIdentifier(exactly: $0.lowerBound)!...UnknownMessageIdentifier(exactly: $0.upperBound)!
return self.buffer
.writeString(" ") + self.buffer
.writeMessageIdentifierRange(range)
}
self.buffer.writeString("UIDBATCHES \(batchSize)")
+ self.buffer.writeIfExists(batchRange) {
let range =
UnknownMessageIdentifier(exactly: $0.lowerBound)!...UnknownMessageIdentifier(
exactly: $0.upperBound
)!
return self.buffer
.writeString(" ")
+ self.buffer
.writeMessageIdentifierRange(range)
}
}

private mutating func writeCommandKind_custom(name: String, payloads: [Command.CustomCommandPayload]) -> Int {
Expand Down
10 changes: 5 additions & 5 deletions Sources/NIOIMAPCore/Grammar/Response/UIDBatchesResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public struct UIDBatchesResponse: Hashable, Sendable {
extension EncodeBuffer {
@discardableResult mutating func writeUIDBatchesResponse(_ response: UIDBatchesResponse) -> Int {
self.writeString(#"UIDBATCHES (TAG "\#(response.correlator)")"#)
+ self.write(if: !response.batches.isEmpty) {
self.writeString(" ") +
self.writeArray(response.batches, separator: ",", parenthesis: false) { range, buffer -> Int in
buffer.writeMessageIdentifierRange(range, descending: true)
+ self.write(if: !response.batches.isEmpty) {
self.writeString(" ")
+ self.writeArray(response.batches, separator: ",", parenthesis: false) { range, buffer -> Int in
buffer.writeMessageIdentifierRange(range, descending: true)
}
}
}
}
}
11 changes: 7 additions & 4 deletions Sources/NIOIMAPCore/Grammar/UID/MessageIdentifierRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ extension MessageIdentifierRange {
// MARK: - Encoding

extension EncodeBuffer {
@discardableResult mutating func writeMessageIdentifierRange<T>(_ range: MessageIdentifierRange<T>, descending: Bool = false) -> Int {
@discardableResult mutating func writeMessageIdentifierRange<T>(
_ range: MessageIdentifierRange<T>,
descending: Bool = false
) -> Int {
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) {
self.writeString(":") + self.writeMessageIdentifier(b)
}
+ self.write(if: range.range.lowerBound < range.range.upperBound) {
self.writeString(":") + self.writeMessageIdentifier(b)
}
}
}
17 changes: 12 additions & 5 deletions Sources/NIOIMAPCore/Parser/Grammar/GrammarParser+Mailbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ extension GrammarParser {
return .lsub(try self.parseMailboxList(buffer: &buffer, tracker: tracker))
}

func parseMailboxData_extendedSearch(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData {
func parseMailboxData_extendedSearch(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData
{
let response = try self.parseExtendedSearchResponse(buffer: &buffer, tracker: tracker)
return .extendedSearch(response)
}


func parseMailboxData_search_combined(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData {
func parseMailboxData_search_combined(
buffer: inout ParseBuffer,
tracker: StackTracker
) throws -> MailboxData {
func parseMailboxData_search(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData {
let nums = try PL.parseZeroOrMore(buffer: &buffer, tracker: tracker) {
(buffer, tracker) -> UnknownMessageIdentifier in
Expand All @@ -72,7 +75,8 @@ extension GrammarParser {
return .search(nums)
}

func parseMailboxData_searchSort(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData {
func parseMailboxData_searchSort(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData
{
try PL.parseSpaces(buffer: &buffer, tracker: tracker)
var array = [try self.parseNZNumber(buffer: &buffer, tracker: tracker)]
try PL.parseZeroOrMore(
Expand Down Expand Up @@ -111,7 +115,10 @@ extension GrammarParser {
.namespace(try self.parseNamespaceResponse(buffer: &buffer, tracker: tracker))
}

func parseMailboxData_uidBatchesResponse(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MailboxData {
func parseMailboxData_uidBatchesResponse(
buffer: inout ParseBuffer,
tracker: StackTracker
) throws -> MailboxData {
.uidBatches(try self.parseUIDBatchesResponse(buffer: &buffer, tracker: tracker))
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1841,10 +1841,11 @@ extension GrammarParser {
try PL.parseFixedString(#"(TAG ""#, buffer: &buffer, tracker: tracker)
let tag = try self.parseTag(buffer: &buffer, tracker: tracker)
try PL.parseFixedString("\")", buffer: &buffer, tracker: tracker)
let batches = try PL.parseOptional(buffer: &buffer, tracker: tracker) { buffer, tracker -> [UIDRange] in
try PL.parseSpaces(buffer: &buffer, tracker: tracker)
return try parseUIDRangeArray(buffer: &buffer, tracker: tracker)
} ?? []
let batches =
try PL.parseOptional(buffer: &buffer, tracker: tracker) { buffer, tracker -> [UIDRange] in
try PL.parseSpaces(buffer: &buffer, tracker: tracker)
return try parseUIDRangeArray(buffer: &buffer, tracker: tracker)
} ?? []
return UIDBatchesResponse(correlator: tag, batches: batches)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ extension CommandType_Tests {
),
(.compress(.deflate), CommandEncodingOptions(), ["COMPRESS DEFLATE"], #line),
(.uidBatches(batchSize: 2_000), CommandEncodingOptions(), ["UIDBATCHES 2000"], #line),
(.uidBatches(batchSize: 1_000, batchRange: 10...20), CommandEncodingOptions(), ["UIDBATCHES 1000 10:20"], #line),
(
.uidBatches(batchSize: 1_000, batchRange: 10...20), CommandEncodingOptions(), ["UIDBATCHES 1000 10:20"],
#line
),

// Custom

Expand Down
17 changes: 11 additions & 6 deletions Tests/NIOIMAPCoreTests/Grammar/Mailbox/MailboxData+Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ extension MailboxDataTests {
(.search([1, 2, 3, 4, 5]), "SEARCH 1 2 3 4 5", #line),
(.search([20, 23], ModificationSequenceValue(917_162_500)), "SEARCH 20 23 (MODSEQ 917162500)", #line),
(
.uidBatches(UIDBatchesResponse(correlator: "A143", batches: [
99_695...215_295,
20_350...99_696,
7_829...20_351,
1...7830,
])),
.uidBatches(
UIDBatchesResponse(
correlator: "A143",
batches: [
99_695...215_295,
20_350...99_696,
7_829...20_351,
1...7830,
]
)
),
#"UIDBATCHES (TAG "A143") 215295:99695,99696:20350,20351:7829,7830:1"#, #line
),
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ extension GrammarParser_Mailbox_Tests {
),
(
#"LSUB () "." #news.comp.mail.misc"#, "\r\n",
.lsub(MailboxInfo(
attributes: [],
path: try! .init(name: MailboxName("#news.comp.mail.misc"), pathSeparator: "."),
extensions: [:]
)),
.lsub(
MailboxInfo(
attributes: [],
path: try! .init(name: MailboxName("#news.comp.mail.misc"), pathSeparator: "."),
extensions: [:]
)
),
#line
),
(
"ESEARCH MIN 1 MAX 2", "\r\n",
.extendedSearch(.init(correlator: nil, kind: .sequenceNumber, returnData: [.min(1), .max(2)])),
#line
),
("ESEARCH", "\r", .extendedSearch(.init(correlator: nil, kind: .sequenceNumber, returnData: [])), #line),
(
"ESEARCH", "\r", .extendedSearch(.init(correlator: nil, kind: .sequenceNumber, returnData: [])),
#line
),
("1234 EXISTS", "\r\n", .exists(1234), #line),
("5678 RECENT", "\r\n", .recent(5678), #line),
("STATUS INBOX ()", "\r\n", .status(.inbox, .init()), #line),
Expand Down Expand Up @@ -83,7 +88,8 @@ extension GrammarParser_Mailbox_Tests {
(
"SEARCH 1 (MODSEQ 2)", "\r\n",
.searchSort(.init(identifiers: [1], modificationSequence: 2)),
#line),
#line
),
(
"NAMESPACE NIL NIL NIL", "\r\n",
.namespace(.init(userNamespace: [], otherUserNamespace: [], sharedNamespace: [])),
Expand Down
12 changes: 9 additions & 3 deletions Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1483,9 +1483,15 @@ extension ParserUnitTests {
self.iterateTests(
testFunction: GrammarParser().parseUIDBatchesResponse,
validInputs: [
(#" (TAG "A143") 215295:99695,99696:20350,20351:7829,7830:1"#, "\r", .init(correlator: "A143", batches: [
99695...215295, 20350...99696, 7829...20351, 1...7830,
]), #line),
(
#" (TAG "A143") 215295:99695,99696:20350,20351:7829,7830:1"#, "\r",
.init(
correlator: "A143",
batches: [
99695...215295, 20350...99696, 7829...20351, 1...7830,
]
), #line
),
(#" (TAG "A143")"#, "\r", .init(correlator: "A143", batches: []), #line),
(#" (TAG "A143") 99695"#, "\r", .init(correlator: "A143", batches: [99695...99695]), #line),
(#" (TAG "A143") 20350:20350"#, "\r", .init(correlator: "A143", batches: [20350...20350]), #line),
Expand Down

0 comments on commit 2e7f589

Please sign in to comment.