Skip to content

Commit

Permalink
Merge pull request #430 from SimplyDanny/main.new-package-loading
Browse files Browse the repository at this point in the history
Use Result to capture success and failure cases
  • Loading branch information
buresdv authored Nov 19, 2024
2 parents dad7b5c + 1a5e2a5 commit 56b98bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cork/Errors/Package Loading Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

enum PackageLoadingError: LocalizedError
enum PackageLoadingError: LocalizedError, Hashable
{
/// When attempting to get the list of raw URLs from the folder containing the packages, the function for loading packages returned nil, therefore, an error occured
case couldNotReadContentsOfParentFolder(failureReason: String)
Expand Down
42 changes: 21 additions & 21 deletions Cork/Logic/Package Loading/Load Up Installed Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import CorkShared
import Foundation

typealias BrewPackages = Set<Result<BrewPackage, PackageLoadingError>>

extension BrewDataStorage
{
/// Parent function for loading installed packages from disk
Expand All @@ -18,7 +20,7 @@ extension BrewDataStorage
/// - Returns: A set of loaded ``BrewPackage``s for the specified ``PackageType``
func loadInstalledPackages(
packageTypeToLoad: PackageType, appState: AppState
) async -> Set<BrewPackage>?
) async -> BrewPackages?
{
/// Start tracking when loading started
let timeLoadingStarted: Date = .now
Expand Down Expand Up @@ -75,7 +77,7 @@ private extension BrewDataStorage
/// Load packages from disk, and convert them into ``BrewPackage``s
func loadInstalledPackagesFromFolder(
packageTypeToLoad: PackageType
) async throws(PackageLoadingError) -> Set<BrewPackage>?
) async throws(PackageLoadingError) -> BrewPackages
{
do
{
Expand All @@ -85,27 +87,23 @@ private extension BrewDataStorage

AppConstants.shared.logger.debug("Loaded contents of folder: \(urlsInParentFolder)")

let packageLoader: Set<BrewPackage> = await withTaskGroup(of: BrewPackage?.self, returning: Set<BrewPackage>.self)
let packageLoader: BrewPackages = await withTaskGroup(of: Result<BrewPackage, PackageLoadingError>.self)
{ taskGroup in
for packageURL in urlsInParentFolder
{
guard taskGroup.addTaskUnlessCancelled(priority: .high, operation: {
try? await self.loadInstalledPackage(packageURL: packageURL)
await self.loadInstalledPackage(packageURL: packageURL)
})
else
{
break
}
}

var loadedPackages: Set<BrewPackage> = .init()

var loadedPackages: BrewPackages = .init(minimumCapacity: urlsInParentFolder.count)
for await loadedPackage in taskGroup
{
if let loadedPackage
{
loadedPackages.insert(loadedPackage)
}
loadedPackages.insert(loadedPackage)
}

return loadedPackages
Expand All @@ -124,7 +122,7 @@ private extension BrewDataStorage
/// For a given `URL` to a package folder containing the various versions of the package, parse the package contained within
/// - Parameter packageURL: `URL` to the package parent folder
/// - Returns: A parsed package of the ``BrewPackage`` type
func loadInstalledPackage(packageURL: URL) async throws(PackageLoadingError) -> BrewPackage
func loadInstalledPackage(packageURL: URL) async -> Result<BrewPackage, PackageLoadingError>
{
/// Get the name of the package - at this stage, it is the last path component
let packageName: String = packageURL.lastPathComponent
Expand All @@ -138,9 +136,9 @@ private extension BrewDataStorage
switch packageURL.packageType
{
case .formula:
throw PackageLoadingError.failedWhileLoadingPackages(failureReason: String(localized: "error.package-loading.last-path-component-of-checked-package-url-is-folder-containing-packages-itself.formulae"))
return .failure(PackageLoadingError.failedWhileLoadingPackages(failureReason: String(localized: "error.package-loading.last-path-component-of-checked-package-url-is-folder-containing-packages-itself.formulae")))
case .cask:
throw PackageLoadingError.failedWhileLoadingPackages(failureReason: String(localized: "error.package-loading.last-path-component-of-checked-package-url-is-folder-containing-packages-itself.casks"))
return .failure(PackageLoadingError.failedWhileLoadingPackages(failureReason: String(localized: "error.package-loading.last-path-component-of-checked-package-url-is-folder-containing-packages-itself.casks")))
}
}

Expand All @@ -163,13 +161,15 @@ private extension BrewDataStorage
{
let wasPackageInstalledIntentionally: Bool = try await packageURL.checkIfPackageWasInstalledIntentionally(versionURLs: versionURLs)

return .init(
name: packageName,
type: packageURL.packageType,
installedOn: packageURL.creationDate,
versions: versionNamesForPackage,
installedIntentionally: wasPackageInstalledIntentionally,
sizeInBytes: packageURL.directorySize
return .success(
.init(
name: packageName,
type: packageURL.packageType,
installedOn: packageURL.creationDate,
versions: versionNamesForPackage,
installedIntentionally: wasPackageInstalledIntentionally,
sizeInBytes: packageURL.directorySize
)
)
}
catch let intentionalInstallationDiscoveryError
Expand All @@ -193,7 +193,7 @@ private extension BrewDataStorage
{
AppConstants.shared.logger.error("Failed while loading package \(packageURL.lastPathComponent, privacy: .public): \(loadingError.localizedDescription)")

throw .failedWhileLoadingCertainPackage(packageURL.lastPathComponent, packageURL, failureReason: loadingError.localizedDescription)
return .failure(.failedWhileLoadingCertainPackage(packageURL.lastPathComponent, packageURL, failureReason: loadingError.localizedDescription))
}
}
}

0 comments on commit 56b98bd

Please sign in to comment.