Files
swift-nio-imap/Sources/NIOIMAPCore/Grammar/UID/UIDRange.swift
T
David Evans 23f0a211da Kill raw representable (#406)
* Fix up UID

* Fix up Encoding

* Fix up MediaSubtype

* Fix up Flag

* Fix up BasicKind

* Fix up GmailLabel

* Fix up SequenceNumber

* Fix up attritbute flag

* Cleanup UIDShiftWrapper

* Cleanup SequenceRange

* Cleanup UIDRange

* Cleanup UIDValidity

* Cleanup MetadataValue

* Cleanup SectionSpecPart

* Cleanup UAuthMechanism

* Cleanup UseAttribute

* Protect access

* Fix sanity
2021-01-18 14:12:56 +00:00

86 lines
2.9 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 UIDRange: Hashable {
/// The range expressed as a native Swift range.
public var range: ClosedRange<UID>
/// Creates a new `UIDRange`.
/// - parameter range: A closed range with `UID`s as the upper and lower bound.
public init(_ range: ClosedRange<UID>) {
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<UID>) {
self.init(UID.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<UID>) {
self.init(range.lowerBound ... UID.max)
}
}
// MARK: - CustomStringConvertible
extension UIDRange: CustomStringConvertible {
/// Creates a human-readable representation of the range.
public var description: String {
if self.range.lowerBound < self.range.upperBound {
return "\(self.range.lowerBound):\(self.range.upperBound)"
} else {
return "\(self.range.lowerBound)"
}
}
}
// MARK: - Integer Literal
extension UIDRange: 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(UID(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: UID) {
self.init(value ... value)
}
}
extension UIDRange {
/// Creates a range that covers every valid UID.
public static let all = UIDRange((.min) ... (.max))
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeUIDRange(_ range: UIDRange) -> Int {
self.writeUID(range.range.lowerBound) +
self.write(if: range.range.lowerBound < range.range.upperBound) {
self.writeString(":") +
self.writeUID(range.range.upperBound)
}
}
}