From 8cd34d64e1aecc64376dbddb8b4798ffa71a0e26 Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Mon, 6 Nov 2023 19:53:09 -0700 Subject: [PATCH] Minor refactoring --- .../FileVerificationError.swift | 44 +++++++++++++++++++ ...Error.swift => ImageProcessingError.swift} | 2 +- Sources/Backend/Runners/Create.swift | 2 +- Sources/Backend/Runners/ListFormats.swift | 2 +- Sources/Backend/Utilities/Errors.swift | 42 ------------------ Sources/Backend/Utilities/IconUtil.swift | 2 +- Sources/Backend/Utilities/OutputHandle.swift | 2 +- 7 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 Sources/Backend/FileManagement/FileVerificationError.swift rename Sources/Backend/ImageProcessing/{ProcessingError.swift => ImageProcessingError.swift} (95%) diff --git a/Sources/Backend/FileManagement/FileVerificationError.swift b/Sources/Backend/FileManagement/FileVerificationError.swift new file mode 100644 index 0000000..efe8b5e --- /dev/null +++ b/Sources/Backend/FileManagement/FileVerificationError.swift @@ -0,0 +1,44 @@ +// +// FileVerificationError.swift +// createicns +// + +/// An error that can be thrown during file verification. +enum FileVerificationError: FormattedError { + case alreadyExists(String) + case doesNotExist(String) + case isDirectory(String) + case isNotDirectory(String) + case invalidPathExtension(String, FileType?) + + var errorMessage: FormattedText { + switch self { + case .alreadyExists(let path): + return "'\(path, color: .yellow)' already exists" + case .doesNotExist(let path): + return "No such file or directory '\(path, color: .yellow)'" + case .isDirectory(let path): + return "'\(path, color: .yellow)' is a directory" + case .isNotDirectory(let path): + return "'\(path, color: .yellow)' is not a directory" + case .invalidPathExtension(let pathExtension, let outputType): + let start: FormattedText = "Invalid path extension '\(pathExtension, color: .yellow, style: .bold)'" + guard let outputType else { + return start + } + if let type = outputType.preferredFilenameExtension { + return start + " for expected output type '\(type, color: .cyan, style: .bold)'" + } + return start + " for unknown output type" + } + } + + var fix: FormattedText? { + if case .invalidPathExtension(_, let outputType) = self { + if let type = outputType?.preferredFilenameExtension { + return "Use path extension '\(type, color: .green, style: .bold)'" + } + } + return nil + } +} diff --git a/Sources/Backend/ImageProcessing/ProcessingError.swift b/Sources/Backend/ImageProcessing/ImageProcessingError.swift similarity index 95% rename from Sources/Backend/ImageProcessing/ProcessingError.swift rename to Sources/Backend/ImageProcessing/ImageProcessingError.swift index 84639a2..0ac7c4a 100644 --- a/Sources/Backend/ImageProcessing/ProcessingError.swift +++ b/Sources/Backend/ImageProcessing/ImageProcessingError.swift @@ -1,5 +1,5 @@ // -// ProcessingError.swift +// ImageProcessingError.swift // createicns // diff --git a/Sources/Backend/Runners/Create.swift b/Sources/Backend/Runners/Create.swift index bef3f32..87e5131 100644 --- a/Sources/Backend/Runners/Create.swift +++ b/Sources/Backend/Runners/Create.swift @@ -43,7 +43,7 @@ struct Create: Runner { { return fileType == .iconset } - // TODO: Handle this instead of assuming false. + // TODO: Handle this instead of assuming false return false } }() diff --git a/Sources/Backend/Runners/ListFormats.swift b/Sources/Backend/Runners/ListFormats.swift index 3d66fcc..30d954a 100644 --- a/Sources/Backend/Runners/ListFormats.swift +++ b/Sources/Backend/Runners/ListFormats.swift @@ -63,7 +63,7 @@ struct ListFormats: Runner { .trimmingSuffix { $0.isWhitespace } // lazy hack (see above) } - // finally, join the lines into a single string. + // finally, join the lines into a single string return lines.joined(separator: "\n") } diff --git a/Sources/Backend/Utilities/Errors.swift b/Sources/Backend/Utilities/Errors.swift index 600db72..521deba 100644 --- a/Sources/Backend/Utilities/Errors.swift +++ b/Sources/Backend/Utilities/Errors.swift @@ -87,45 +87,3 @@ struct ContextualDataError: FormattedError { self.init(data, context: String(describing: context)) } } - -// MARK: - FileVerificationError - -/// An error that can be thrown during file verification. -enum FileVerificationError: FormattedError { - case alreadyExists(String) - case doesNotExist(String) - case isDirectory(String) - case isNotDirectory(String) - case invalidPathExtension(String, FileType?) - - var errorMessage: FormattedText { - switch self { - case .alreadyExists(let path): - return "'\(path, color: .yellow)' already exists" - case .doesNotExist(let path): - return "No such file or directory '\(path, color: .yellow)'" - case .isDirectory(let path): - return "'\(path, color: .yellow)' is a directory" - case .isNotDirectory(let path): - return "'\(path, color: .yellow)' is not a directory" - case .invalidPathExtension(let pathExtension, let outputType): - let start: FormattedText = "Invalid path extension '\(pathExtension, color: .yellow, style: .bold)'" - guard let outputType else { - return start - } - if let type = outputType.preferredFilenameExtension { - return start + " for expected output type '\(type, color: .cyan, style: .bold)'" - } - return start + " for unknown output type" - } - } - - var fix: FormattedText? { - if case .invalidPathExtension(_, let outputType) = self { - if let type = outputType?.preferredFilenameExtension { - return "Use path extension '\(type, color: .green, style: .bold)'" - } - } - return nil - } -} diff --git a/Sources/Backend/Utilities/IconUtil.swift b/Sources/Backend/Utilities/IconUtil.swift index b1cc0f8..0d12686 100644 --- a/Sources/Backend/Utilities/IconUtil.swift +++ b/Sources/Backend/Utilities/IconUtil.swift @@ -57,7 +57,7 @@ enum IconUtil { } }() - // iconutil only returns data if something went wrong. + // iconutil only returns data if something went wrong if let data { throw ContextualDataError(data, context: self) } diff --git a/Sources/Backend/Utilities/OutputHandle.swift b/Sources/Backend/Utilities/OutputHandle.swift index 87a2709..d093619 100644 --- a/Sources/Backend/Utilities/OutputHandle.swift +++ b/Sources/Backend/Utilities/OutputHandle.swift @@ -109,7 +109,7 @@ extension OutputHandle: Hashable { extension OutputHandle: TextOutputStream { private func write(_ elements: S, to fileHandle: FileHandle) where S.Element == UInt8 { if #available(macOS 10.15.4, *) { - // we want to know about a failure here, so a force try is acceptable + // can't recover from failures here, so force try is acceptable // swiftlint:disable:next force_try try! fileHandle.write(contentsOf: Data(elements)) } else {