diff --git a/Sources/Containerization/Image/ImageStore/ImageStore+Export.swift b/Sources/Containerization/Image/ImageStore/ImageStore+Export.swift index 7fa1eadd..422ed107 100644 --- a/Sources/Containerization/Image/ImageStore/ImageStore+Export.swift +++ b/Sources/Containerization/Image/ImageStore/ImageStore+Export.swift @@ -91,14 +91,14 @@ extension ImageStore { for layerGroup in pushQueue { for desc in layerGroup { await progress?([ - ProgressEvent(event: "add-total-size", value: desc.size), - ProgressEvent(event: "add-total-items", value: 1), + .addTotalSize(desc.size), + .addTotalItems(1), ]) } } await progress?([ - ProgressEvent(event: "add-total-size", value: localIndexData.count), - ProgressEvent(event: "add-total-items", value: 1), + .addTotalSize(Int64(localIndexData.count)), + .addTotalItems(1), ]) } @@ -135,15 +135,15 @@ extension ImageStore { } try await client.push(name: name, ref: tag, descriptor: descriptor, streamGenerator: generator, progress: progress) await progress?([ - ProgressEvent(event: "add-size", value: descriptor.size), - ProgressEvent(event: "add-items", value: 1), + .addSize(descriptor.size), + .addItems(1), ]) } catch let err as ContainerizationError { guard err.code != .exists else { // We reported the total items and size and have to account for them in existing content. await progress?([ - ProgressEvent(event: "add-size", value: descriptor.size), - ProgressEvent(event: "add-items", value: 1), + .addSize(descriptor.size), + .addItems(1), ]) return } diff --git a/Sources/Containerization/Image/ImageStore/ImageStore+Import.swift b/Sources/Containerization/Image/ImageStore/ImageStore+Import.swift index 9adcf6ff..9bc39a7f 100644 --- a/Sources/Containerization/Image/ImageStore/ImageStore+Import.swift +++ b/Sources/Containerization/Image/ImageStore/ImageStore+Import.swift @@ -52,8 +52,8 @@ extension ImageStore { size += desc.size } await progress([ - ProgressEvent(event: "add-total-size", value: size), - ProgressEvent(event: "add-total-items", value: toProcess.count), + .addTotalSize(size), + .addTotalItems(toProcess.count), ]) } @@ -146,9 +146,9 @@ extension ImageStore { try FileManager.default.copyItem(at: found.path, to: ingestDir.appendingPathComponent(descriptor.digest.trimmingDigestPrefix)) await progress?([ // Count the size of the blob - ProgressEvent(event: "add-size", value: descriptor.size), + .addSize(descriptor.size), // Count the number of blobs - ProgressEvent(event: "add-items", value: 1), + .addItems(1), ]) return } @@ -160,7 +160,7 @@ extension ImageStore { } // Count the number of blobs await progress?([ - ProgressEvent(event: "add-items", value: 1) + .addItems(1) ]) } @@ -190,7 +190,7 @@ extension ImageStore { if let progress { let size = Int64(result.size) await progress([ - ProgressEvent(event: "add-size", value: size) + .addSize(size) ]) } guard result.digest.digestString == descriptor.digest else { diff --git a/Sources/ContainerizationEXT4/Formatter+Unpack.swift b/Sources/ContainerizationEXT4/Formatter+Unpack.swift index fee04cc3..a25b6f43 100644 --- a/Sources/ContainerizationEXT4/Formatter+Unpack.swift +++ b/Sources/ContainerizationEXT4/Formatter+Unpack.swift @@ -44,7 +44,7 @@ extension EXT4.Formatter { if let progress { Task { await progress([ - ProgressEvent(event: "add-items", value: 1) + .addItems(1) ]) } } @@ -88,7 +88,7 @@ extension EXT4.Formatter { if let progress, let size = entry.size { Task { await progress([ - ProgressEvent(event: "add-size", value: Int64(size)) + .addSize(Int64(size)) ]) } } diff --git a/Sources/ContainerizationExtras/ProgressEvent.swift b/Sources/ContainerizationExtras/ProgressEvent.swift index 3210aa93..38e80bbc 100644 --- a/Sources/ContainerizationExtras/ProgressEvent.swift +++ b/Sources/ContainerizationExtras/ProgressEvent.swift @@ -15,23 +15,35 @@ //===----------------------------------------------------------------------===// /// A progress update event. -public struct ProgressEvent: Sendable { - /// The event name. The possible values: +public enum ProgressEvent: Sendable { + /// The possible values: /// - `add-items`: Increment the number of processed items by `value`. /// - `add-total-items`: Increment the total number of items to process by `value`. /// - `add-size`: Increment the size of processed items by `value`. /// - `add-total-size`: Increment the total size of items to process by `value`. - public let event: String - /// The event value. - public let value: any Sendable + case addItems(Int) + case addTotalItems(Int) + case addSize(Int64) + case addTotalSize(Int64) + + /// The event name. + public var event: String { + switch self { + case .addItems: "add-items" + case .addTotalItems: "add-total-items" + case .addSize: "add-size" + case .addTotalSize: "add-total-size" + } + } - /// Creates an instance. - /// - Parameters: - /// - event: The event name. - /// - value: The event value. - public init(event: String, value: any Sendable) { - self.event = event - self.value = value + /// The event value. + public var value: any Sendable { + switch self { + case .addItems(let value): value + case .addTotalItems(let value): value + case .addSize(let value): value + case .addTotalSize(let value): value + } } } diff --git a/Sources/ContainerizationOCI/Client/LocalOCILayoutClient.swift b/Sources/ContainerizationOCI/Client/LocalOCILayoutClient.swift index 80632668..7e0f79d8 100644 --- a/Sources/ContainerizationOCI/Client/LocalOCILayoutClient.swift +++ b/Sources/ContainerizationOCI/Client/LocalOCILayoutClient.swift @@ -71,7 +71,7 @@ package final class LocalOCILayoutClient: ContentClient { if let progress, let fileSize = fileManager.fileSize(atPath: filePath) { await progress([ - ProgressEvent(event: "add-size", value: fileSize) + .addSize(fileSize) ]) } } catch let error as NSError { @@ -93,7 +93,7 @@ package final class LocalOCILayoutClient: ContentClient { if let progress, let fileSize = fileManager.fileSize(atPath: filePath) { await progress([ - ProgressEvent(event: "add-size", value: fileSize) + .addSize(fileSize) ]) } } catch { diff --git a/Sources/ContainerizationOCI/Client/RegistryClient+Fetch.swift b/Sources/ContainerizationOCI/Client/RegistryClient+Fetch.swift index 3990a9eb..08bece0c 100644 --- a/Sources/ContainerizationOCI/Client/RegistryClient+Fetch.swift +++ b/Sources/ContainerizationOCI/Client/RegistryClient+Fetch.swift @@ -181,7 +181,7 @@ extension RegistryClient { received += readBytes let written = try await writer.write(contentsOf: buf) await progress?([ - ProgressEvent(event: "add-size", value: written) + .addSize(written) ]) guard written == readBytes else { throw ContainerizationError( @@ -224,7 +224,7 @@ extension RegistryClient { let readBytes = Int64(buf.readableBytes) received += readBytes await progress?([ - ProgressEvent(event: "add-size", value: readBytes) + .addSize(readBytes) ]) try fd.write(contentsOf: buf.readableBytesView) hasher.update(data: buf.readableBytesView)