Files
swift-nio-imap/Sources/IMAPCommands/CompletedCommand.swift
T
Daniel Eggert f1dd8b2cba Add ResponseText.Text for response text
`.no(.init(text: "No mailbox x\r\n* BYE pwned"))` was written verbatim,
injecting an untagged response; likewise tagged, BYE, PREAUTH and
continuation responses.

RFC 9051: `text = 1*(TEXT-CHAR / UTF8-2 / UTF8-3 / UTF8-4)`. New
`ResponseText.Text` replaces the `String`; `init?(_:)` rejects NUL, CR,
LF and a leading space (the parser skips it), literals trap. Empty stays
valid. The parser now accepts UTF-8 text.
2026-10-29 15:51:38 +01:00

82 lines
2.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
import NIOIMAP
/// A command that has completed with a tagged response from the server.
public struct CompletedCommand: Equatable, Sendable {
/// The tag identifying this command.
public var tag: IMAPConnection.Tag
/// The completion status from the server.
public var status: TaggedResponse.State
/// The time span from sending to completion.
public var duration: Duration
}
/// A command that completed with an `OK` response.
public struct SuccessfulCommand: Equatable, Sendable {
/// The tag identifying this command.
public var tag: IMAPConnection.Tag
/// The response text from the server.
public var text: ResponseText
/// The time span from sending to completion.
public var duration: Duration
}
// MARK: -
extension SuccessfulCommand {
init(_ completed: CompletedCommand) throws {
guard case .ok(let text) = completed.status else {
throw CommandFailed(completedCommand: completed)
}
self.init(
tag: completed.tag,
text: text,
duration: completed.duration
)
}
}
private struct CommandFailed: Swift.Error {
var completedCommand: CompletedCommand
}
extension CommandFailed: CustomStringConvertible {
var description: String {
let s: String
let text: ResponseText
switch self.completedCommand.status {
case .ok(let t):
s = "OK"
text = t
case .no(let t):
s = "NO"
text = t
case .bad(let t):
s = "BAD"
text = t
}
guard let code = text.code else {
return "Command \(self.completedCommand.tag) failed: \(s) \(String(text.text))"
}
return "Command \(self.completedCommand.tag) failed: \(s) \(String(reflecting: code)) \(String(text.text))"
}
}