mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-26 04:57:51 +00:00
In a previous PR we made UID conform to a new protocol MessageIdentifier. This PR continues the trend of removing concrete types and replacing them with generic types. In this case we have UIDSet and UIDSetNonEmpty. These are identical to SequenceNumberSet and SequenceNumberSetNonEmpty. To allow us to remove the duplicate code a new generic type MessageIdentifierSet has been defined that takes a generic parameter T: MessageIdentifier. Both UID and SequenceNumber conform to MessageIdentifier. In the next PR I will remove the code for SequenceNumberSet* and replace with MessageIdentifierSet. This PR only addresses the UIDSet* types. Note: I will update all documentation in another (probably the last) PR.
88 lines
3.1 KiB
Swift
88 lines
3.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
|
|
/// Represents a range of `UID`s using lower and upper bounds.
|
|
public struct MessageIdentifierRange<IdentifierType: MessageIdentifier>: Hashable {
|
|
/// The range expressed as a native Swift range.
|
|
public var range: ClosedRange<IdentifierType>
|
|
|
|
/// Creates a new `UIDRange`.
|
|
/// - parameter range: A closed range with `UID`s as the upper and lower bound.
|
|
public init(_ range: ClosedRange<IdentifierType>) {
|
|
self.range = range
|
|
}
|
|
|
|
/// Creates a new `UIDRange` from a partial range, using `.min` as the lower bound.
|
|
/// - parameter range: A partial with a `UID` as the upper bound.
|
|
public init(_ range: PartialRangeThrough<IdentifierType>) {
|
|
self.init(IdentifierType.min ... range.upperBound)
|
|
}
|
|
|
|
/// Creates a new `UIDRange` from a partial range, using `.max` as the upper bound.
|
|
/// - parameter rawValue: A partial with a `UID` as the lower bound.
|
|
public init(_ range: PartialRangeFrom<IdentifierType>) {
|
|
self.init(range.lowerBound ... IdentifierType.max)
|
|
}
|
|
}
|
|
|
|
// MARK: - CustomDebugStringConvertible
|
|
|
|
extension MessageIdentifierRange: CustomDebugStringConvertible {
|
|
/// Creates a human-readable representation of the range.
|
|
public var debugDescription: String {
|
|
if self.range.lowerBound < self.range.upperBound {
|
|
return "\(self.range.lowerBound):\(self.range.upperBound)"
|
|
} else {
|
|
return "\(self.range.lowerBound)"
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Integer Literal
|
|
|
|
extension MessageIdentifierRange: ExpressibleByIntegerLiteral {
|
|
/// Creates a range from a single number - essentially a range containing one value.
|
|
/// - parameter value: The raw number to use as both the upper and lower bounds.
|
|
public init(integerLiteral value: UInt32) {
|
|
self.init(IdentifierType(integerLiteral: value))
|
|
}
|
|
|
|
/// Creates a range from a single number - essentially a range containing one value.
|
|
/// - parameter value: The raw number to use as both the upper and lower bounds.
|
|
public init(_ value: IdentifierType) {
|
|
self.init(value ... value)
|
|
}
|
|
}
|
|
|
|
extension MessageIdentifierRange {
|
|
/// Creates a range that covers every valid UID.
|
|
public static var all: Self {
|
|
Self((.min) ... (.max))
|
|
}
|
|
}
|
|
|
|
// MARK: - Encoding
|
|
|
|
extension EncodeBuffer {
|
|
@discardableResult mutating func writeMessageIdentifierRange<T>(_ range: MessageIdentifierRange<T>) -> Int {
|
|
self.writeMessageIdentifier(range.range.lowerBound) +
|
|
self.write(if: range.range.lowerBound < range.range.upperBound) {
|
|
self.writeString(":") +
|
|
self.writeMessageIdentifier(range.range.upperBound)
|
|
}
|
|
}
|
|
}
|