Refactor search key (#223)

* More search key tests

* Remove sent search type

* Rename smaller and larger

* Rename search key sequence set case

* Documentation

* Add documentation

* And

* Fix tests

* Documentation
This commit is contained in:
David Evans authored and GitHub committed 2020-06-22 13:48:02 +01:00
1 parent 49b44c521f
commit c34e3889cb
7 files changed
+167 -125

No files matched your search

@@ -60,6 +60,7 @@ public struct Capability: Equatable {
return nil
}
index = self.rawValue.index(after: index)
return String(self.rawValue[index...])
}
@@ -16,43 +16,124 @@ import struct NIO.ByteBuffer
/// IMAPv4 `search-key`
public indirect enum SearchKey: Equatable {
/// RFC 3501: All messages in the mailbox; the default initial key for ANDing.
case all
/// RFC 3501: Messages with the `\Answered` flag set.
case answered
/// RFC 3501: Messages with the `\Deleted` flag set.
case deleted
/// RFC 3501: Messages with the `\Flagged` flag set.
case flagged
/// RFC 3501: Messages that have the `\Recent` flag set but not the `\Seen` flag. This is functionally equivalent to "(RECENT UNSEEN)".
case new
/// RFC 3501: Messages that functionally equivalent to "NOT RECENT" (as opposed to "NOT NEW").
case old
/// RFC 3501: Messages that have the `\Recent` flag set.
case recent
/// RFC 3501: Messages that have the `\Seen` flag set.
case seen
/// RFC 3501: Messages that do not have the `\Answered` flag set.
case unanswered
/// RFC 3501: Messages that do not have the `\Delete` flag set.
case undeleted
/// RFC 3501: Messages that do not have the `\Flagged` flag set.
case unflagged
/// RFC 3501: Messages that do not have the `\Seen` flag set.
case unseen
/// RFC 3501: Messages with the `\Draft` flag set.
case draft
/// RFC 3501: Messages that do not have the `\Draft` flag set.
case undraft
/// RFC 3501: Messages that contain the specified string in the envelope structure’s BCC field.
case bcc(ByteBuffer)
/// RFC 3501: Messages whose internal date (disregarding time and timezone) is earlier than the specified date.
case before(Date)
/// RFC 3501: Messages that contain the specified string in the body of the message.
case body(ByteBuffer)
/// RFC 3501: Messages that contain the specified string in the envelope structure’s CC field.
case cc(ByteBuffer)
/// RFC 3501: Messages that contain the specified string in the envelope structure’s FROM field.
case from(ByteBuffer)
/// RFC 3501: Messages with the specified keyword flag set.
case keyword(Flag.Keyword)
/// RFC 3501: Messages whose internal date (disregarding time and timezone) is within the specified date.
case on(Date)
/// RFC 3501: Messages whose internal date (disregarding time and timezone) is within or later than the specified date.
case since(Date)
/// RFC 3501: Messages that contain the specified string in the envelope structure’s SUBJECT field.
case subject(ByteBuffer)
/// RFC 3501: Messages that contain the specified string in the header or body of the message.
case text(ByteBuffer)
/// RFC 3501: Messages that contain the specified string in the envelope structure’s TO field.
case to(ByteBuffer)
/// RFC 3501: Messages that do not have the specified keyword flag set.
case unkeyword(Flag.Keyword)
/// RFC 3501: Messages that have a header with the specified field-name (as defined in [RFC-2822]) and that contains the specified string in the text of the header (what comes after the colon). If the string to search is zero-length, this matches all messages that have a header the contents.
case header(String, ByteBuffer)
case larger(Int)
/// RFC 3501: Messages with an [RFC-2822] size larger than the specified number of octets.
case messageSizeLarger(Int)
/// RFC 3501: Messages that do not match the specified search key.
case not(SearchKey)
/// RFC 3501: Messages that match either search key.
case or(SearchKey, SearchKey)
case sent(SearchSentType)
case smaller(Int)
/// RFC 3501: Messages whose [RFC-2822] Date: header (disregarding time and timezone) is earlier than the specified date.
case sentBefore(Date)
/// RFC 3501: Messages whose [RFC-2822] Date: header (disregarding time and timezone) is within the specified date.
case sentOn(Date)
/// RFC 3501: Messages whose [RFC-2822] Date: header (disregarding time and timezone) is within or later than the specified date.
case sentSince(Date)
/// RFC 3501: Messages with an [RFC-2822] size smaller than the specified number of octets.
case messageSizeSmaller(Int)
/// RFC 3501: Messages with unique identifiers corresponding to the specified unique identifier set. Sequence set ranges are permitted.
case uid(UIDSet)
case sequenceSet(SequenceSet)
case array([SearchKey])
/// RFC 3501: Messages that match a given sequence set.
case sequenceNumbers(SequenceSet)
/// RFC 3501: Messages that match all of the given keys.
case and([SearchKey])
/// RFC 5032: Messages that are older than the given number of seconds.
case older(Int)
/// RFC 5032: Messages that are younger than the given number of seconds.
case younger(Int)
/// RFC 3501:
case filter(String)
}
@@ -162,7 +243,7 @@ extension EncodeBuffer {
self.writeSpace() +
self.writeIMAPString(value)
case .larger(let n):
case .messageSizeLarger(let n):
return self.writeString("LARGER \(n)")
case .not(let key):
@@ -177,10 +258,7 @@ extension EncodeBuffer {
self.writeSpace() +
self.writeSearchKey(k2)
case .sent(let type):
return self.writeSearchSentType(type)
case .smaller(let n):
case .messageSizeSmaller(let n):
return self.writeString("SMALLER \(n)")
case .uid(let set):
@@ -188,10 +266,10 @@ extension EncodeBuffer {
self.writeString("UID ") +
self.writeUIDSet(set)
case .sequenceSet(let set):
case .sequenceNumbers(let set):
return self.writeSequenceSet(set)
case .array(let array):
case .and(let array):
return self.writeSearchKeys(array)
case .younger(let seconds):
return self.writeString("YOUNGER \(seconds)")
@@ -201,6 +279,18 @@ extension EncodeBuffer {
return
self.writeString("FILTER ") +
self.writeFilterName(filterName)
case .sentBefore(let date):
return
self.writeString("SENTBEFORE ") +
self.writeDate(date)
case .sentOn(let date):
return
self.writeString("SENTON ") +
self.writeDate(date)
case .sentSince(let date):
return
self.writeString("SENTSINCE ") +
self.writeDate(date)
}
}
}
@@ -1,43 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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
// Dervied from `search-key`
public enum SearchSentType: Equatable {
case before(Date)
case on(Date)
case since(Date)
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeSearchSentType(_ type: SearchSentType) -> Int {
switch type {
case .before(let date):
return
self.writeString("SENTBEFORE ") +
self.writeDate(date)
case .on(let date):
return
self.writeString("SENTON ") +
self.writeDate(date)
case .since(let date):
return
self.writeString("SENTSINCE ") +
self.writeDate(date)
}
}
}
@@ -3096,12 +3096,12 @@ extension GrammarParser {
func parseSearchKey_larger(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
try ParserLibrary.parseFixedString("LARGER ", buffer: &buffer, tracker: tracker)
return .larger(try self.parseNumber(buffer: &buffer, tracker: tracker))
return .messageSizeLarger(try self.parseNumber(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_smaller(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
try ParserLibrary.parseFixedString("SMALLER ", buffer: &buffer, tracker: tracker)
return .smaller(try self.parseNumber(buffer: &buffer, tracker: tracker))
return .messageSizeSmaller(try self.parseNumber(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_not(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
@@ -3119,17 +3119,17 @@ extension GrammarParser {
func parseSearchKey_sentBefore(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
try ParserLibrary.parseFixedString("SENTBEFORE ", buffer: &buffer, tracker: tracker)
return .sent(.before(try self.parseDate(buffer: &buffer, tracker: tracker)))
return .sentBefore(try self.parseDate(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_sentOn(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
try ParserLibrary.parseFixedString("SENTON ", buffer: &buffer, tracker: tracker)
return .sent(.on(try self.parseDate(buffer: &buffer, tracker: tracker)))
return .sentOn(try self.parseDate(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_sentSince(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
try ParserLibrary.parseFixedString("SENTSINCE ", buffer: &buffer, tracker: tracker)
return .sent(.since(try self.parseDate(buffer: &buffer, tracker: tracker)))
return .sentSince(try self.parseDate(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_uid(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
@@ -3138,7 +3138,7 @@ extension GrammarParser {
}
func parseSearchKey_sequenceSet(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
.sequenceSet(try self.parseSequenceSet(buffer: &buffer, tracker: tracker))
.sequenceNumbers(try self.parseSequenceSet(buffer: &buffer, tracker: tracker))
}
func parseSearchKey_array(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
@@ -3149,7 +3149,7 @@ extension GrammarParser {
return try self.parseSearchKey(buffer: &buffer, tracker: tracker)
}
try ParserLibrary.parseFixedString(")", buffer: &buffer, tracker: tracker)
return .array(array)
return .and(array)
}
func parseSearchKey_older(buffer: inout ByteBuffer, tracker: StackTracker) throws -> SearchKey {
@@ -50,16 +50,18 @@ extension SearchKeyTests {
(.to("theboss@apple.com"), "TO \"theboss@apple.com\"", #line),
(.unkeyword(Flag.Keyword("nokeyword")), "UNKEYWORD NOKEYWORD", #line),
(.header("header", "value"), "HEADER header \"value\"", #line),
(.larger(333), "LARGER 333", #line),
(.not(.larger(444)), "NOT LARGER 444", #line),
(.or(.smaller(444), .larger(666)), "OR SMALLER 444 LARGER 666", #line),
(.sent(.on(Date(year: 2018, month: 12, day: 7)!)), "SENTON 7-Dec-2018", #line),
(.smaller(555), "SMALLER 555", #line),
(.messageSizeLarger(333), "LARGER 333", #line),
(.not(.messageSizeLarger(444)), "NOT LARGER 444", #line),
(.or(.messageSizeSmaller(444), .messageSizeLarger(666)), "OR SMALLER 444 LARGER 666", #line),
(.sentOn(Date(year: 2018, month: 12, day: 7)!), "SENTON 7-Dec-2018", #line),
(.sentBefore(Date(year: 2018, month: 12, day: 7)!), "SENTBEFORE 7-Dec-2018", #line),
(.sentSince(Date(year: 2018, month: 12, day: 7)!), "SENTSINCE 7-Dec-2018", #line),
(.messageSizeSmaller(555), "SMALLER 555", #line),
(.uid(UIDSet(333 ... 444)), "UID 333:444", #line),
(.sequenceSet(SequenceSet(...222)), "1:222", #line),
(.sequenceSet(SequenceSet(222...)), "222:*", #line),
(.array([]), "()", #line),
(.array([.smaller(444), .larger(333)]), "(SMALLER 444 LARGER 333)", #line),
(.sequenceNumbers(SequenceSet(...222)), "1:222", #line),
(.sequenceNumbers(SequenceSet(222...)), "222:*", #line),
(.and([]), "()", #line),
(.and([.messageSizeSmaller(444), .messageSizeLarger(333)]), "(SMALLER 444 LARGER 333)", #line),
(.filter("name"), "FILTER name", #line),
]
@@ -1,44 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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
@testable import NIOIMAPCore
import XCTest
class SearchSentTypeTests: EncodeTestClass {}
// MARK: - IMAP
extension SearchSentTypeTests {
func testImapEncoded_before() {
let expected = "SENTBEFORE 25-Jun-1994"
let size = self.testBuffer.writeSearchSentType(SearchSentType.before(Date(year: 1994, month: 6, day: 25)!))
XCTAssertEqual(size, expected.utf8.count)
XCTAssertEqual(expected, self.testBufferString)
}
func testImapEncoded_on() {
let expected = "SENTON 7-Dec-2018"
let size = self.testBuffer.writeSearchSentType(SearchSentType.on(Date(year: 2018, month: 12, day: 07)!))
XCTAssertEqual(size, expected.utf8.count)
XCTAssertEqual(expected, self.testBufferString)
}
func testImapEncoded_since() {
let expected = "SENTSINCE 16-Sep-1999"
let size = self.testBuffer.writeSearchSentType(SearchSentType.since(Date(year: 1999, month: 9, day: 16)!))
XCTAssertEqual(size, expected.utf8.count)
XCTAssertEqual(expected, self.testBufferString)
}
}
@@ -2071,6 +2071,42 @@ extension ParserUnitTests {
("SEARCH CHARSET UTF-8 ALL", "\r", .search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.all])), #line),
("SEARCH RETURN () ALL", "\r", .search(returnOptions: [], program: .init(charset: nil, keys: [.all])), #line),
("SEARCH RETURN (MIN) ALL", "\r", .search(returnOptions: [.min], program: .init(charset: nil, keys: [.all])), #line),
(
#"SEARCH CHARSET UTF-8 (OR FROM "me" FROM "you") (OR NEW UNSEEN)"#,
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.and([.or(.from("me"), .from("you"))]), .and([.or(.new, .unseen)])])),
#line
),
(
#"SEARCH CHARSET UTF-8 OR (FROM "me" FROM "you") (NEW UNSEEN)"#,
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.or(.and([.from("me"), .from("you")]), .and([.new, .unseen]))])),
#line
),
(
#"SEARCH CHARSET UTF-8 OR (FROM "me" NEW) (UNSEEN FROM "you")"#,
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.or(.and([.from("me"), .new]), .and([.unseen, .from("you")]))])),
#line
),
(
#"SEARCH CHARSET UTF-8 OR (FROM "me" NOT FROM "you") (NOT NEW UNSEEN)"#,
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.or(.and([.from("me"), .not(.from("you"))]), .and([.not(.new), .unseen]))])),
#line
),
(
"SEARCH CHARSET UTF-8 NOT (NEW UNSEEN)",
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.not(.and([.new, .unseen]))])),
#line
),
(
"SEARCH CHARSET UTF-8 NOT (OR NEW UNSEEN)",
"\r",
.search(returnOptions: [], program: .init(charset: "UTF-8", keys: [.not(.and([.or(.new, .unseen)]))])),
#line
),
]
self.iterateTestInputs(inputs, testFunction: GrammarParser.parseSearch)
}
@@ -2121,12 +2157,12 @@ extension ParserUnitTests {
("DRAFT", "\r", .draft, #line),
("ON 25-jun-1994", "\r", .on(Date(year: 1994, month: 6, day: 25)!), #line),
("SINCE 01-jan-2001", "\r", .since(Date(year: 2001, month: 1, day: 1)!), #line),
("SENTON 02-jan-2002", "\r", .sent(.on(Date(year: 2002, month: 1, day: 2)!)), #line),
("SENTBEFORE 03-jan-2003", "\r", .sent(.before(Date(year: 2003, month: 1, day: 3)!)), #line),
("SENTSINCE 04-jan-2004", "\r", .sent(.since(Date(year: 2004, month: 1, day: 4)!)), #line),
("SENTON 02-jan-2002", "\r", .sentOn(Date(year: 2002, month: 1, day: 2)!), #line),
("SENTBEFORE 03-jan-2003", "\r", .sentBefore(Date(year: 2003, month: 1, day: 3)!), #line),
("SENTSINCE 04-jan-2004", "\r", .sentSince(Date(year: 2004, month: 1, day: 4)!), #line),
("BEFORE 05-jan-2005", "\r", .before(Date(year: 2005, month: 1, day: 5)!), #line),
("LARGER 1234", "\r", .larger(1234), #line),
("SMALLER 5678", "\r", .smaller(5678), #line),
("LARGER 1234", "\r", .messageSizeLarger(1234), #line),
("SMALLER 5678", "\r", .messageSizeSmaller(5678), #line),
("BCC data1", "\r", .bcc("data1"), #line),
("BODY data2", "\r", .body("data2"), #line),
("CC data3", "\r", .cc("data3"), #line),
@@ -2137,12 +2173,12 @@ extension ParserUnitTests {
("KEYWORD key1", "\r", .keyword(Flag.Keyword("key1")), #line),
("HEADER some value", "\r", .header("some", "value"), #line),
("UNKEYWORD key2", "\r", .unkeyword(Flag.Keyword("key2")), #line),
("NOT LARGER 1234", "\r", .not(.larger(1234)), #line),
("OR LARGER 6 SMALLER 4", "\r", .or(.larger(6), .smaller(4)), #line),
("NOT LARGER 1234", "\r", .not(.messageSizeLarger(1234)), #line),
("OR LARGER 6 SMALLER 4", "\r", .or(.messageSizeLarger(6), .messageSizeSmaller(4)), #line),
("UID 2:4", "\r", .uid(UIDSet(2 ... 4)), #line),
("2:4", "\r", .sequenceSet(SequenceSet(2 ... 4)), #line),
("(LARGER 1)", "\r", .array([.larger(1)]), #line),
("(LARGER 1 SMALLER 5 KEYWORD hello)", "\r", .array([.larger(1), .smaller(5), .keyword(Flag.Keyword("hello"))]), #line),
("2:4", "\r", .sequenceNumbers(SequenceSet(2 ... 4)), #line),
("(LARGER 1)", "\r", .and([.messageSizeLarger(1)]), #line),
("(LARGER 1 SMALLER 5 KEYWORD hello)", "\r", .and([.messageSizeLarger(1), .messageSizeSmaller(5), .keyword(Flag.Keyword("hello"))]), #line),
("YOUNGER 34", "\r", .younger(34), #line),
("OLDER 45", "\r", .older(45), #line),
("FILTER something", "\r", .filter("something"), #line),