Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Nov 7, 2023
1 parent 2a6c721 commit 8cd34d6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 47 deletions.
44 changes: 44 additions & 0 deletions Sources/Backend/FileManagement/FileVerificationError.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ProcessingError.swift
// ImageProcessingError.swift
// createicns
//

Expand Down
2 changes: 1 addition & 1 deletion Sources/Backend/Runners/Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Backend/Runners/ListFormats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
42 changes: 0 additions & 42 deletions Sources/Backend/Utilities/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/Backend/Utilities/IconUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Backend/Utilities/OutputHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ extension OutputHandle: Hashable {
extension OutputHandle: TextOutputStream {
private func write<S: Sequence>(_ 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 {
Expand Down

0 comments on commit 8cd34d6

Please sign in to comment.