mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-22 04:30:43 +00:00
Rename Partial → ByteRange (#736)
This commit is contained in:
1 parent
cc3b72e93f
commit
01577b28be
10 files changed
+78
-114
No files matched your search
+8
-2
@@ -12,8 +12,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import struct NIO.ByteBuffer
|
||||
|
||||
/// Represents a range of bytes in a larger whole. See RFC 5092
|
||||
public struct PartialRange: Hashable {
|
||||
public struct ByteRange: Hashable {
|
||||
/// The offset in bytes from the beginning of the message/data in question.
|
||||
public var offset: Int
|
||||
|
||||
@@ -32,7 +34,11 @@ public struct PartialRange: Hashable {
|
||||
// MARK: - Encoding
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writePartialRange(_ data: PartialRange) -> Int {
|
||||
@discardableResult mutating func writePartial(_ num: ClosedRange<UInt32>) -> Int {
|
||||
self.writeString("<\(num.lowerBound).\(num.count)>")
|
||||
}
|
||||
|
||||
@discardableResult mutating func writePartialRange(_ data: ByteRange) -> Int {
|
||||
self.writeString("\(data.offset)") +
|
||||
self.writeIfExists(data.length) { length in
|
||||
self.writeString(".\(length)")
|
||||
+12
-10
@@ -12,27 +12,29 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Used to append a `PartialRange` to the end of a *UID FETCH BODY.PEEK*.
|
||||
public struct IPartial: Hashable {
|
||||
/// The `PartialRange` to append.
|
||||
public var range: PartialRange
|
||||
extension MessagePath {
|
||||
/// Used to append a `ByteRange` as part of a URL.
|
||||
public struct ByteRange: Hashable {
|
||||
/// The `PartialRange` to append.
|
||||
public var range: NIOIMAPCore.ByteRange
|
||||
|
||||
/// Creates a new `IPartial`.
|
||||
/// - parameter range: The `PartialRange` to append.
|
||||
public init(range: PartialRange) {
|
||||
self.range = range
|
||||
/// Creates a new `MessagePath.ByteRange`.
|
||||
/// - parameter range: The `PartialRange` to append.
|
||||
public init(range: NIOIMAPCore.ByteRange) {
|
||||
self.range = range
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Encoding
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeIPartial(_ data: IPartial) -> Int {
|
||||
@discardableResult mutating func writeMessagePathByteRange(_ data: MessagePath.ByteRange) -> Int {
|
||||
self.writeString("/;PARTIAL=") +
|
||||
self.writePartialRange(data.range)
|
||||
}
|
||||
|
||||
@discardableResult mutating func writeIPartialOnly(_ data: IPartial) -> Int {
|
||||
@discardableResult mutating func writeMessagePathByteRangeOnly(_ data: MessagePath.ByteRange) -> Int {
|
||||
self.writeString(";PARTIAL=") +
|
||||
self.writePartialRange(data.range)
|
||||
}
|
||||
@@ -24,14 +24,14 @@ public struct MessagePath: Hashable {
|
||||
public var section: URLMessageSection?
|
||||
|
||||
/// A specific range of bytes of the message/section in question.
|
||||
public var range: IPartial?
|
||||
public var range: MessagePath.ByteRange?
|
||||
|
||||
/// Create a new `MessagePath`.
|
||||
/// Create a new `MessagePath`.z
|
||||
/// - parameter mailboxUIDValidity: Connection details for a server and a specific mailbox hosted on that server.
|
||||
/// - parameter iUID: The UID of the message in question.
|
||||
/// - parameter IMAPURLSection: An optional section of the message in question.
|
||||
/// - parameter iPartial: A specific range of bytes of the message/section in question.
|
||||
public init(mailboxReference: MailboxUIDValidity, iUID: IUID, section: URLMessageSection? = nil, range: IPartial? = nil) {
|
||||
public init(mailboxReference: MailboxUIDValidity, iUID: IUID, section: URLMessageSection? = nil, range: MessagePath.ByteRange? = nil) {
|
||||
self.mailboxReference = mailboxReference
|
||||
self.iUID = iUID
|
||||
self.section = section
|
||||
@@ -49,7 +49,7 @@ extension EncodeBuffer {
|
||||
self.writeURLMessageSection(section)
|
||||
} +
|
||||
self.writeIfExists(data.range) { partial in
|
||||
self.writeIPartial(partial)
|
||||
self.writeMessagePathByteRange(partial)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +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
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writePartial(_ num: ClosedRange<UInt32>) -> Int {
|
||||
self.writeString("<\(num.lowerBound).\(num.count)>")
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,16 @@
|
||||
/// Provides a variety of ways to load message data.
|
||||
public enum URLFetchType: Hashable {
|
||||
/// Uses a mailbox reference and message UID to load a message, and optional message section and part.
|
||||
case refUidSectionPartial(ref: MailboxUIDValidity, uid: IUID, section: URLMessageSection?, partial: IPartial?)
|
||||
case refUidSectionPartial(ref: MailboxUIDValidity, uid: IUID, section: URLMessageSection?, partial: MessagePath.ByteRange?)
|
||||
|
||||
/// Specifies the section of a message to fetch using a message UID, and optionally a specific part of that message.
|
||||
case uidSectionPartial(uid: IUID, section: URLMessageSection?, partial: IPartial?)
|
||||
case uidSectionPartial(uid: IUID, section: URLMessageSection?, partial: MessagePath.ByteRange?)
|
||||
|
||||
/// Specifies the section of a message to fetch, and optionally a specific part of that message.
|
||||
case sectionPartial(section: URLMessageSection, partial: IPartial?)
|
||||
case sectionPartial(section: URLMessageSection, partial: MessagePath.ByteRange?)
|
||||
|
||||
/// Specifies the part of a message to fetch.
|
||||
case partialOnly(IPartial)
|
||||
case partialOnly(MessagePath.ByteRange)
|
||||
}
|
||||
|
||||
// MARK: - Encoding
|
||||
@@ -32,35 +32,35 @@ public enum URLFetchType: Hashable {
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeURLFetchType(_ data: URLFetchType) -> Int {
|
||||
switch data {
|
||||
case .refUidSectionPartial(ref: let ref, uid: let uid, section: let section, partial: let partial):
|
||||
case .refUidSectionPartial(ref: let ref, uid: let uid, section: let section, partial: let range):
|
||||
return self.writeEncodedMailboxUIDValidity(ref) +
|
||||
self.writeIUIDOnly(uid) +
|
||||
self.writeIfExists(section) { section in
|
||||
self.writeString("/") +
|
||||
self.writeURLMessageSectionOnly(section)
|
||||
} +
|
||||
self.writeIfExists(partial) { partial in
|
||||
self.writeIfExists(range) { range in
|
||||
self.writeString("/") +
|
||||
self.writeIPartialOnly(partial)
|
||||
self.writeMessagePathByteRangeOnly(range)
|
||||
}
|
||||
case .uidSectionPartial(uid: let uid, section: let section, partial: let partial):
|
||||
case .uidSectionPartial(uid: let uid, section: let section, partial: let range):
|
||||
return self.writeIUIDOnly(uid) +
|
||||
self.writeIfExists(section) { section in
|
||||
self.writeString("/") +
|
||||
self.writeURLMessageSectionOnly(section)
|
||||
} +
|
||||
self.writeIfExists(partial) { partial in
|
||||
self.writeIfExists(range) { range in
|
||||
self.writeString("/") +
|
||||
self.writeIPartialOnly(partial)
|
||||
self.writeMessagePathByteRangeOnly(range)
|
||||
}
|
||||
case .sectionPartial(section: let section, partial: let partial):
|
||||
return self.writeURLMessageSectionOnly(section) +
|
||||
self.writeIfExists(partial) { partial in
|
||||
self.writeString("/") +
|
||||
self.writeIPartialOnly(partial)
|
||||
self.writeMessagePathByteRangeOnly(partial)
|
||||
}
|
||||
case .partialOnly(let partial):
|
||||
return self.writeIPartialOnly(partial)
|
||||
return self.writeMessagePathByteRangeOnly(partial)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -747,15 +747,15 @@ extension GrammarParser {
|
||||
try PL.parseNewline(buffer: &buffer, tracker: tracker)
|
||||
}
|
||||
|
||||
func parseIPartial(buffer: inout ParseBuffer, tracker: StackTracker) throws -> IPartial {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> IPartial in
|
||||
func parseMessagePathByteRange(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MessagePath.ByteRange {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> MessagePath.ByteRange in
|
||||
try PL.parseFixedString("/", buffer: &buffer, tracker: tracker)
|
||||
return try parseIPartialOnly(buffer: &buffer, tracker: tracker)
|
||||
return try parseMessagePathByteRangeOnly(buffer: &buffer, tracker: tracker)
|
||||
}
|
||||
}
|
||||
|
||||
func parseIPartialOnly(buffer: inout ParseBuffer, tracker: StackTracker) throws -> IPartial {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> IPartial in
|
||||
func parseMessagePathByteRangeOnly(buffer: inout ParseBuffer, tracker: StackTracker) throws -> MessagePath.ByteRange {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> MessagePath.ByteRange in
|
||||
try PL.parseFixedString(";PARTIAL=", buffer: &buffer, tracker: tracker)
|
||||
return .init(range: try self.parsePartialRange(buffer: &buffer, tracker: tracker))
|
||||
}
|
||||
@@ -919,23 +919,23 @@ extension GrammarParser {
|
||||
}
|
||||
|
||||
var section = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIMAPURLSection)
|
||||
var partial: IPartial?
|
||||
var byteRange: MessagePath.ByteRange?
|
||||
if section?.encodedSection.section.last == Character(.init(UInt8(ascii: "/"))) {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker in
|
||||
section!.encodedSection.section = String(section!.encodedSection.section.dropLast())
|
||||
|
||||
partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIPartialOnly)
|
||||
byteRange = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseMessagePathByteRangeOnly)
|
||||
}
|
||||
} else {
|
||||
partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIPartial)
|
||||
byteRange = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseMessagePathByteRange)
|
||||
}
|
||||
return .init(mailboxReference: ref, iUID: uid, section: section, range: partial)
|
||||
return .init(mailboxReference: ref, iUID: uid, section: section, range: byteRange)
|
||||
}
|
||||
}
|
||||
|
||||
func parseURLFetchType(buffer: inout ParseBuffer, tracker: StackTracker) throws -> URLFetchType {
|
||||
func parseURLFetchType_partialOnly(buffer: inout ParseBuffer, tracker: StackTracker) throws -> URLFetchType {
|
||||
let partial = try self.parseIPartialOnly(buffer: &buffer, tracker: tracker)
|
||||
let partial = try self.parseMessagePathByteRangeOnly(buffer: &buffer, tracker: tracker)
|
||||
return .partialOnly(partial)
|
||||
}
|
||||
|
||||
@@ -944,16 +944,16 @@ extension GrammarParser {
|
||||
if section.encodedSection.section.last == Character(.init(UInt8(ascii: "/"))) {
|
||||
section.encodedSection.section = String(section.encodedSection.section.dropLast())
|
||||
do {
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIPartialOnly)
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseMessagePathByteRangeOnly)
|
||||
return .sectionPartial(section: section, partial: partial)
|
||||
} catch is ParserError {
|
||||
section.encodedSection.section.append("/")
|
||||
return .sectionPartial(section: section, partial: nil)
|
||||
}
|
||||
}
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> IPartial in
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> MessagePath.ByteRange in
|
||||
try PL.parseFixedString("/", buffer: &buffer, tracker: tracker)
|
||||
return try self.parseIPartialOnly(buffer: &buffer, tracker: tracker)
|
||||
return try self.parseMessagePathByteRangeOnly(buffer: &buffer, tracker: tracker)
|
||||
})
|
||||
return .sectionPartial(section: section, partial: partial)
|
||||
}
|
||||
@@ -967,16 +967,16 @@ extension GrammarParser {
|
||||
if section?.encodedSection.section.last == Character(.init(UInt8(ascii: "/"))) {
|
||||
section!.encodedSection.section = String(section!.encodedSection.section.dropLast())
|
||||
do {
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIPartialOnly)
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseMessagePathByteRangeOnly)
|
||||
return .uidSectionPartial(uid: uid, section: section, partial: partial)
|
||||
} catch is ParserError {
|
||||
section?.encodedSection.section.append("/")
|
||||
return .uidSectionPartial(uid: uid, section: section, partial: nil)
|
||||
}
|
||||
}
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> IPartial in
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> MessagePath.ByteRange in
|
||||
try PL.parseFixedString("/", buffer: &buffer, tracker: tracker)
|
||||
return try self.parseIPartialOnly(buffer: &buffer, tracker: tracker)
|
||||
return try self.parseMessagePathByteRangeOnly(buffer: &buffer, tracker: tracker)
|
||||
})
|
||||
return .uidSectionPartial(uid: uid, section: section, partial: partial)
|
||||
}
|
||||
@@ -991,16 +991,16 @@ extension GrammarParser {
|
||||
if section?.encodedSection.section.last == Character(.init(UInt8(ascii: "/"))) {
|
||||
section!.encodedSection.section = String(section!.encodedSection.section.dropLast())
|
||||
do {
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseIPartialOnly)
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: self.parseMessagePathByteRangeOnly)
|
||||
return .refUidSectionPartial(ref: ref, uid: uid, section: section, partial: partial)
|
||||
} catch is ParserError {
|
||||
section?.encodedSection.section.append("/")
|
||||
return .refUidSectionPartial(ref: ref, uid: uid, section: section, partial: nil)
|
||||
}
|
||||
}
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> IPartial in
|
||||
let partial = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: { buffer, tracker -> MessagePath.ByteRange in
|
||||
try PL.parseFixedString("/", buffer: &buffer, tracker: tracker)
|
||||
return try self.parseIPartialOnly(buffer: &buffer, tracker: tracker)
|
||||
return try self.parseMessagePathByteRangeOnly(buffer: &buffer, tracker: tracker)
|
||||
})
|
||||
return .refUidSectionPartial(ref: ref, uid: uid, section: section, partial: partial)
|
||||
}
|
||||
@@ -1709,13 +1709,13 @@ extension GrammarParser {
|
||||
}
|
||||
}
|
||||
|
||||
func parsePartialRange(buffer: inout ParseBuffer, tracker: StackTracker) throws -> PartialRange {
|
||||
func parsePartialRange(buffer: inout ParseBuffer, tracker: StackTracker) throws -> ByteRange {
|
||||
func parsePartialRange_length(buffer: inout ParseBuffer, tracker: StackTracker) throws -> Int {
|
||||
try PL.parseFixedString(".", buffer: &buffer, tracker: tracker)
|
||||
return try self.parseNumber(buffer: &buffer, tracker: tracker)
|
||||
}
|
||||
|
||||
return try PL.composite(buffer: &buffer, tracker: tracker) { (buffer, tracker) -> PartialRange in
|
||||
return try PL.composite(buffer: &buffer, tracker: tracker) { (buffer, tracker) -> ByteRange in
|
||||
let offset = try self.parseNumber(buffer: &buffer, tracker: tracker)
|
||||
let length = try PL.parseOptional(buffer: &buffer, tracker: tracker, parser: parsePartialRange_length)
|
||||
return .init(offset: offset, length: length)
|
||||
|
||||
+10
-2
@@ -16,11 +16,11 @@ import NIO
|
||||
@_spi(NIOIMAPInternal) @testable import NIOIMAPCore
|
||||
import XCTest
|
||||
|
||||
class Partial_Tests: EncodeTestClass {}
|
||||
class ByteRange_Tests: EncodeTestClass {}
|
||||
|
||||
// MARK: - Encoding
|
||||
|
||||
extension Partial_Tests {
|
||||
extension ByteRange_Tests {
|
||||
func testEncode() {
|
||||
let inputs: [(ClosedRange<UInt32>, String, UInt)] = [
|
||||
/// Encoded format is `<offset.count>`:
|
||||
@@ -39,4 +39,12 @@ extension Partial_Tests {
|
||||
XCTAssertEqual(self.testBufferString, expectedString, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
func testEncode_ByteRange() {
|
||||
let inputs: [(ByteRange, String, UInt)] = [
|
||||
(.init(offset: 1, length: nil), "1", #line),
|
||||
(.init(offset: 1, length: 2), "1.2", #line),
|
||||
]
|
||||
self.iterateInputs(inputs: inputs, encoder: { self.testBuffer.writePartialRange($0) })
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -16,16 +16,16 @@ import NIO
|
||||
@_spi(NIOIMAPInternal) @testable import NIOIMAPCore
|
||||
import XCTest
|
||||
|
||||
class IPartial_Tests: EncodeTestClass {}
|
||||
class MessagePathByteRange_Tests: EncodeTestClass {}
|
||||
|
||||
// MARK: - Encoding
|
||||
|
||||
extension IPartial_Tests {
|
||||
func testEncode_IPartial() {
|
||||
let inputs: [(IPartial, String, UInt)] = [
|
||||
extension MessagePathByteRange_Tests {
|
||||
func testEncode_MessagePathByteRange() {
|
||||
let inputs: [(MessagePath.ByteRange, String, UInt)] = [
|
||||
(.init(range: .init(offset: 1, length: nil)), "/;PARTIAL=1", #line),
|
||||
(.init(range: .init(offset: 1, length: 2)), "/;PARTIAL=1.2", #line),
|
||||
]
|
||||
self.iterateInputs(inputs: inputs, encoder: { self.testBuffer.writeIPartial($0) })
|
||||
self.iterateInputs(inputs: inputs, encoder: { self.testBuffer.writeMessagePathByteRange($0) })
|
||||
}
|
||||
}
|
||||
@@ -1,31 +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
|
||||
@_spi(NIOIMAPInternal) @testable import NIOIMAPCore
|
||||
import XCTest
|
||||
|
||||
class PartialRange_Tests: EncodeTestClass {}
|
||||
|
||||
// MARK: - Encoding
|
||||
|
||||
extension PartialRange_Tests {
|
||||
func testEncode() {
|
||||
let inputs: [(PartialRange, String, UInt)] = [
|
||||
(.init(offset: 1, length: nil), "1", #line),
|
||||
(.init(offset: 1, length: 2), "1.2", #line),
|
||||
]
|
||||
self.iterateInputs(inputs: inputs, encoder: { self.testBuffer.writePartialRange($0) })
|
||||
}
|
||||
}
|
||||
@@ -1402,12 +1402,12 @@ extension ParserUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - parseIPartial
|
||||
// MARK: - parseMessagePathByteRange
|
||||
|
||||
extension ParserUnitTests {
|
||||
func testParseIPartial() {
|
||||
func testParseMessagePathByteRange() {
|
||||
self.iterateTests(
|
||||
testFunction: GrammarParser().parseIPartial,
|
||||
testFunction: GrammarParser().parseMessagePathByteRange,
|
||||
validInputs: [
|
||||
("/;PARTIAL=1", " ", .init(range: .init(offset: 1, length: nil)), #line),
|
||||
("/;PARTIAL=1.2", " ", .init(range: .init(offset: 1, length: 2)), #line),
|
||||
@@ -1423,12 +1423,12 @@ extension ParserUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - parseIPartialOnly
|
||||
// MARK: - parseMessagePathByteRangeOnly
|
||||
|
||||
extension ParserUnitTests {
|
||||
func testParseIPartialOnly() {
|
||||
func testParseMessagePathByteRangeOnly() {
|
||||
self.iterateTests(
|
||||
testFunction: GrammarParser().parseIPartialOnly,
|
||||
testFunction: GrammarParser().parseMessagePathByteRangeOnly,
|
||||
validInputs: [
|
||||
(";PARTIAL=1", " ", .init(range: .init(offset: 1, length: nil)), #line),
|
||||
(";PARTIAL=1.2", " ", .init(range: .init(offset: 1, length: 2)), #line),
|
||||
|
||||
Reference in new issue
Block a user