From 924b8411a800d5cf43648d2620f7cd5cb2081f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Kali=C5=84ski?= <47140412+KaQuMiQ@users.noreply.github.com> Date: Fri, 16 Jun 2023 17:11:29 +0200 Subject: [PATCH] Remove error merge --- Sources/MQ/Errors/TheError.swift | 91 +------------------------ Sources/MQ/Errors/TheErrorGroup.swift | 10 ++- Tests/MQTests/MultipleIssuesTests.swift | 8 --- Tests/MQTests/TheErrorTests.swift | 14 ++-- 4 files changed, 17 insertions(+), 106 deletions(-) diff --git a/Sources/MQ/Errors/TheError.swift b/Sources/MQ/Errors/TheError.swift index 325adf8..f86c217 100644 --- a/Sources/MQ/Errors/TheError.swift +++ b/Sources/MQ/Errors/TheError.swift @@ -16,11 +16,8 @@ public protocol TheError: Error, CustomStringConvertible, CustomDebugStringConve /// with this error instance. It can be used /// to quickly identify error domains or /// group errors by any other meaning. - /// Default implementation does not support mutations, - /// if you need to mutate it please define stored - /// property in your error instance. /// Default group for all errors is ``TheErrorGroup.default``. - var group: TheErrorGroup { get set } + var group: TheErrorGroup { get } /// Source code metadata context for this error. /// It is used to derive default implementations @@ -34,20 +31,6 @@ public protocol TheError: Error, CustomStringConvertible, CustomDebugStringConve /// It is used to derive default implementations /// of ``Hashable`` and ``Equatable`` protocols. var displayableString: DisplayableString { get } - /// ``merge`` adds contents of provided error to this error. - /// - /// When merging, composite should take all informations - /// that it can handle and update self with new data. - /// It is intended to change or add error details - /// when i.e. catching other error while trying to keep - /// diagnostics data in place. - /// - /// - Parameter other: Other error to be merged into this one. - mutating func merge( - with other: TheError, - file: StaticString, - line: UInt - ) /// Function checking equality of errors. /// /// This function can be used to check if two errors @@ -143,61 +126,6 @@ extension TheError /* DisplayableWithString */ { } } -extension TheError { - - /// ``merge`` adds contents of provided error to this error. - /// - /// When merging, composite should take all informations - /// that it can handle and update self with new data. - /// It is intended to change or add error details - /// when i.e. catching other error while trying to keep - /// diagnostics data in place. - /// - /// - Parameters: - /// - other: Other error to be merged into this one. - /// - file: Source code file identifier. - /// Filled automatically based on compile time constants. - /// - line: Line in given source code file. - /// Filled automatically based on compile time constants. - public mutating func merge( - with other: TheError, - file: StaticString = #fileID, - line: UInt = #line - ) { - self.group = .merging(self.group, other.group) - self.context = .merging(self.context, other.context) - } - - /// ``merging`` adds contents of provided error to this error copy. - /// - /// When merging, composite should take all informations - /// that it can handle and update self with new data. - /// It is intended to change or add error deatails - /// when i.e. catching other error while trying to keep - /// diagnostics data in place. - /// - /// - Parameters: - /// - other: Other error to be merged into this error copy. - /// - file: Source code file identifier. - /// Filled automatically based on compile time constants. - /// - line: Line in given source code file. - /// Filled automatically based on compile time constants. - /// - Returns: Copy of this error with error merged. - public func merging( - with other: TheError, - file: StaticString = #fileID, - line: UInt = #line - ) -> TheError { - var copy: Self = self - copy.merge( - with: other, - file: file, - line: line - ) - return copy - } -} - // swift-format-ignore: AllPublicDeclarationsHaveDocumentation extension TheError { @@ -231,21 +159,8 @@ extension TheError { .init(Self.self) } - public var group: TheErrorGroup { - @_transparent @_semantics("constant_evaluable") get { - .default - } - set { - Unimplemented - .error( - message: "Error group assignment is not implemented!" - ) - .with(Self.self, for: "Error type") - .asRuntimeWarning( - message: "Cannot assign error group without stored property, please define one or avoid mutating it." - ) - } - } + @_transparent @_semantics("constant_evaluable") + public var group: TheErrorGroup { .default } /// Terminate process with this error as the cause. /// diff --git a/Sources/MQ/Errors/TheErrorGroup.swift b/Sources/MQ/Errors/TheErrorGroup.swift index abe9893..0211dee 100644 --- a/Sources/MQ/Errors/TheErrorGroup.swift +++ b/Sources/MQ/Errors/TheErrorGroup.swift @@ -15,7 +15,10 @@ public struct TheErrorGroup { extension TheErrorGroup { - internal static func merging( + /// Merge multiple error groups. + /// + /// Note that ordering matters. + public static func merging( _ head: TheErrorGroup, _ mid: TheErrorGroup, _ tail: TheErrorGroup... @@ -23,7 +26,10 @@ extension TheErrorGroup { .merging([head, mid] + tail) } - internal static func merging( + /// Merge multiple error groups. + /// + /// Note that ordering matters. + public static func merging( _ groups: Array ) -> Self { var added: Set = .init() diff --git a/Tests/MQTests/MultipleIssuesTests.swift b/Tests/MQTests/MultipleIssuesTests.swift index 13a54df..94d2e07 100644 --- a/Tests/MQTests/MultipleIssuesTests.swift +++ b/Tests/MQTests/MultipleIssuesTests.swift @@ -23,14 +23,6 @@ final class MultipleIssuesTests: XCTestCase { XCTAssertTrue(multipleIssues.errors.contains(where: { $0 is Undefined })) } - func test_merge_doesNotAddErrorToCollection() { - var multipleIssues: MultipleIssues = .error() - - multipleIssues.merge(with: Undefined.error()) - - XCTAssertTrue(multipleIssues.errors.isEmpty) - } - func test_displayableString_usesProvidedExtractionFunction() { let multipleIssues: MultipleIssues = .error( displayableMessageExtraction: { _ in "MOCK" } diff --git a/Tests/MQTests/TheErrorTests.swift b/Tests/MQTests/TheErrorTests.swift index fd80d86..94ecc7c 100644 --- a/Tests/MQTests/TheErrorTests.swift +++ b/Tests/MQTests/TheErrorTests.swift @@ -4,14 +4,12 @@ import XCTest final class TheErrorTests: XCTestCase { func test_merge_combinesSourceCodeContext() { - var error: Undefined = .error( - file: "file", - line: 42 - ) - - error.merge( - with: - Unimplemented + let error: MultipleIssues = .error( + collecting: Undefined.error( + file: "file", + line: 42 + ), + Unimplemented .error( file: "other_file", line: 0