Skip to content

Commit 555eda5

Browse files
committed
ContainerRegistry: Represent digest algorithms by an enum
1 parent 5ffe9fd commit 555eda5

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Sources/ContainerRegistry/ImageReference.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func splitReference(_ reference: String) throws -> (String?, String) {
2424
// Hostname heuristic: contains a '.' or a ':', or is localhost
2525
if splits[0] != "localhost", !splits[0].contains("."), !splits[0].contains(":") { return (nil, reference) }
2626

27-
return (String(splits[0]), String(splits[1]))
27+
return ("\(splits[0])", "\(splits[1])")
2828
}
2929

3030
// Split the name into repository and tag parts
@@ -35,8 +35,8 @@ func parseName(_ name: String) throws -> (ImageReference.Repository, any ImageRe
3535
let digestSplit = name.split(separator: "@", maxSplits: 1, omittingEmptySubsequences: false)
3636
if digestSplit.count == 2 {
3737
return (
38-
try ImageReference.Repository(String(digestSplit[0])),
39-
try ImageReference.Digest(String(digestSplit[1]))
38+
try ImageReference.Repository("\(digestSplit[0])"),
39+
try ImageReference.Digest("\(digestSplit[1])")
4040
)
4141
}
4242

@@ -51,8 +51,8 @@ func parseName(_ name: String) throws -> (ImageReference.Repository, any ImageRe
5151

5252
// assert splits == 2
5353
return (
54-
try ImageReference.Repository(String(tagSplit[0])),
55-
try ImageReference.Tag(String(tagSplit[1]))
54+
try ImageReference.Repository("\(tagSplit[0])"),
55+
try ImageReference.Tag("\(tagSplit[1])")
5656
)
5757
}
5858

@@ -214,7 +214,19 @@ extension ImageReference {
214214

215215
/// Digest identifies a specific blob by the hash of the blob's contents.
216216
public struct Digest: Reference, Sendable, Equatable, CustomStringConvertible, CustomDebugStringConvertible {
217-
var algorithm: String
217+
public enum Algorithm: String, Sendable {
218+
case sha256 = "sha256"
219+
case sha512 = "sha512"
220+
221+
init(fromString rawValue: String) throws {
222+
guard let algorithm = Algorithm(rawValue: rawValue) else {
223+
throw RegistryClientError.invalidDigestAlgorithm(rawValue)
224+
}
225+
self = algorithm
226+
}
227+
}
228+
229+
var algorithm: Algorithm
218230
var value: String
219231

220232
public enum ValidationError: Error, Equatable {
@@ -230,16 +242,16 @@ extension ImageReference {
230242
// https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#sha-256
231243
let sha256digest = /(sha256):([a-fA-F0-9]{64})/
232244
if let match = try sha256digest.wholeMatch(in: rawValue) {
233-
algorithm = String(match.1)
234-
value = String(match.2)
245+
algorithm = try Algorithm(fromString: "\(match.1)")
246+
value = "\(match.2)"
235247
return
236248
}
237249

238250
// https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#sha-512
239251
let sha512digest = /(sha512):([a-fA-F0-9]{128})/
240252
if let match = try sha512digest.wholeMatch(in: rawValue) {
241-
algorithm = String(match.1)
242-
value = String(match.2)
253+
algorithm = try Algorithm(fromString: "\(match.1)")
254+
value = "\(match.2)"
243255
return
244256
}
245257

Sources/ContainerRegistry/RegistryClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum RegistryClientError: Error {
2323
case registryParseError(String)
2424
case invalidRegistryPath(String)
2525
case invalidUploadLocation(String)
26+
case invalidDigestAlgorithm(String)
2627
}
2728

2829
extension RegistryClientError: CustomStringConvertible {
@@ -31,6 +32,7 @@ extension RegistryClientError: CustomStringConvertible {
3132
case let .registryParseError(reference): return "Unable to parse registry: \(reference)"
3233
case let .invalidRegistryPath(path): return "Unable to construct URL for registry path: \(path)"
3334
case let .invalidUploadLocation(location): return "Received invalid upload location from registry: \(location)"
35+
case let .invalidDigestAlgorithm(digest): return "Invalid or unsupported digest algorithm: \(digest)"
3436
}
3537
}
3638
}

Tests/ContainerRegistryTests/ImageReferenceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ struct DigestTests {
293293
func testParseValidDigest(digest: String, algorithm: String, value: String) throws {
294294
let parsed = try! ImageReference.Digest(digest)
295295

296-
#expect(parsed.algorithm == algorithm)
296+
#expect("\(parsed.algorithm)" == algorithm)
297297
#expect(parsed.value == value)
298298
#expect("\(parsed)" == digest)
299299
}

0 commit comments

Comments
 (0)