mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-28 05:14:27 +00:00
Resolves #634 The majority of types that were Equatable are now Hashable. Anywhere that we had a custom == and the type was also Hashable we also now have custom hash functions.
83 lines
2.8 KiB
Swift
83 lines
2.8 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
|
||
|
||
/// A `UseAttribute` is a special-use attribute as defined in RFC 6154.
|
||
/// They're used to designate a special use to certain mailboxes.
|
||
/// The raw `String` value is lower-cased on initialisation to ensure
|
||
/// case-insensitive comparison.
|
||
public struct UseAttribute: Hashable {
|
||
/// A mailbox that presents all messages in the user's store.
|
||
public static let all = Self("\\All")
|
||
|
||
/// Used to archive messages - note that the meaning of "archived" will vary from server to server.
|
||
public static let archive = Self("\\Archive")
|
||
|
||
/// Used to store draft messages that have not been sent.
|
||
public static let drafts = Self("\\Drafts")
|
||
|
||
/// Stores messages that have been marked as "important" for some reason.
|
||
public static let flagged = Self("\\Flagged")
|
||
|
||
/// Stores messages deemed to be spam of junk mail, e.g. from a mailing list.
|
||
public static let junk = Self("\\Junk")
|
||
|
||
/// Holds copies of messages that have been sent.
|
||
public static let sent = Self("\\Sent")
|
||
|
||
/// Holds messages that have been deleted or marked for deletion.
|
||
public static let trash = Self("\\Trash")
|
||
|
||
internal let stringValue: String
|
||
|
||
/// Creates a new `UseAttribute` from the raw `String`. Note that
|
||
/// usually it should be sufficient to just use the predefined attributes, e.g. `.drafts`.
|
||
/// `rawValue` will be lowercased.
|
||
public init(_ stringValue: String) {
|
||
self.stringValue = stringValue
|
||
}
|
||
|
||
public static func == (lhs: Self, rhs: Self) -> Bool {
|
||
lhs.stringValue.lowercased() == rhs.stringValue.lowercased()
|
||
}
|
||
|
||
public func hash(into hasher: inout Hasher) {
|
||
self.stringValue.lowercased().hash(into: &hasher)
|
||
}
|
||
}
|
||
|
||
extension UseAttribute {
|
||
/// Special Use can be returned as part of the `LIST` response. They’ll then be
|
||
/// wrapped in `MailboxInfo.Attribute`.
|
||
public init(_ other: MailboxInfo.Attribute) {
|
||
self.init(String(other))
|
||
}
|
||
}
|
||
|
||
extension String {
|
||
/// The raw value of the attribute, e.g. `\\trash`. Always lowercase.
|
||
public init(_ other: UseAttribute) {
|
||
self = other.stringValue
|
||
}
|
||
}
|
||
|
||
// MARK: - Encoding
|
||
|
||
extension EncodeBuffer {
|
||
@discardableResult mutating func writeUseAttribute(_ att: UseAttribute) -> Int {
|
||
self.writeString(att.stringValue)
|
||
}
|
||
}
|