Skip to content

Commit bbed892

Browse files
committed
Move Codable conformance out of PackageModel+Codable.swift
1 parent cf85142 commit bbed892

File tree

4 files changed

+173
-194
lines changed

4 files changed

+173
-194
lines changed

Sources/PackageModel/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ add_library(PackageModel
1010
BuildSettings.swift
1111
Manifest.swift
1212
Package.swift
13-
PackageModel+Codable.swift
1413
Platform.swift
1514
Product.swift
1615
ResolvedModels.swift

Sources/PackageModel/Manifest.swift

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public struct TargetDescription: Equatable, Codable {
380380
}
381381

382382
/// Represents a target's dependency on another entity.
383-
public enum Dependency: Equatable, ExpressibleByStringLiteral {
383+
public enum Dependency: Equatable, ExpressibleByStringLiteral, Codable {
384384
case target(name: String, condition: PackageConditionDescription?)
385385
case product(name: String, package: String?, condition: PackageConditionDescription?)
386386
case byName(name: String, condition: PackageConditionDescription?)
@@ -396,6 +396,54 @@ public struct TargetDescription: Equatable, Codable {
396396
public static func product(name: String, package: String? = nil) -> Dependency {
397397
return .product(name: name, package: package, condition: nil)
398398
}
399+
400+
private enum CodingKeys: String, CodingKey {
401+
case target, product, byName
402+
}
403+
404+
public func encode(to encoder: Encoder) throws {
405+
var container = encoder.container(keyedBy: CodingKeys.self)
406+
switch self {
407+
case let .target(a1, a2):
408+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .target)
409+
try unkeyedContainer.encode(a1)
410+
try unkeyedContainer.encode(a2)
411+
case let .product(a1, a2, a3):
412+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .product)
413+
try unkeyedContainer.encode(a1)
414+
try unkeyedContainer.encode(a2)
415+
try unkeyedContainer.encode(a3)
416+
case let .byName(a1, a2):
417+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .byName)
418+
try unkeyedContainer.encode(a1)
419+
try unkeyedContainer.encode(a2)
420+
}
421+
}
422+
423+
public init(from decoder: Decoder) throws {
424+
let values = try decoder.container(keyedBy: CodingKeys.self)
425+
guard let key = values.allKeys.first(where: values.contains) else {
426+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
427+
}
428+
switch key {
429+
case .target:
430+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
431+
let a1 = try unkeyedValues.decode(String.self)
432+
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
433+
self = .target(name: a1, condition: a2)
434+
case .product:
435+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
436+
let a1 = try unkeyedValues.decode(String.self)
437+
let a2 = try unkeyedValues.decodeIfPresent(String.self)
438+
let a3 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
439+
self = .product(name: a1, package: a2, condition: a3)
440+
case .byName:
441+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
442+
let a1 = try unkeyedValues.decode(String.self)
443+
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
444+
self = .byName(name: a1, condition: a2)
445+
}
446+
}
399447
}
400448

401449
public struct Resource: Codable, Equatable {
@@ -560,17 +608,57 @@ public struct ProductDescription: Equatable, Codable {
560608
}
561609

562610
/// Represents system package providers.
563-
public enum SystemPackageProviderDescription: Equatable {
611+
public enum SystemPackageProviderDescription: Equatable, Codable {
564612
case brew([String])
565613
case apt([String])
566614
case yum([String])
615+
616+
private enum CodingKeys: String, CodingKey {
617+
case brew, apt, yum
618+
}
619+
620+
public func encode(to encoder: Encoder) throws {
621+
var container = encoder.container(keyedBy: CodingKeys.self)
622+
switch self {
623+
case let .brew(a1):
624+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .brew)
625+
try unkeyedContainer.encode(a1)
626+
case let .apt(a1):
627+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .apt)
628+
try unkeyedContainer.encode(a1)
629+
case let .yum(a1):
630+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .yum)
631+
try unkeyedContainer.encode(a1)
632+
}
633+
}
634+
635+
public init(from decoder: Decoder) throws {
636+
let values = try decoder.container(keyedBy: CodingKeys.self)
637+
guard let key = values.allKeys.first(where: values.contains) else {
638+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
639+
}
640+
switch key {
641+
case .brew:
642+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
643+
let a1 = try unkeyedValues.decode([String].self)
644+
self = .brew(a1)
645+
case .apt:
646+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
647+
let a1 = try unkeyedValues.decode([String].self)
648+
self = .apt(a1)
649+
case .yum:
650+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
651+
let a1 = try unkeyedValues.decode([String].self)
652+
self = .yum(a1)
653+
}
654+
}
567655
}
568656

569657
/// Represents a package dependency.
570658
public struct PackageDependencyDescription: Equatable, Codable {
571659

572660
/// The dependency requirement.
573-
public enum Requirement: Equatable, Hashable, CustomStringConvertible {
661+
public enum Requirement: Equatable, Hashable, CustomStringConvertible, Codable {
574662
case exact(Version)
575663
case range(Range<Version>)
576664
case revision(String)
@@ -599,6 +687,57 @@ public struct PackageDependencyDescription: Equatable, Codable {
599687
return "local"
600688
}
601689
}
690+
691+
private enum CodingKeys: String, CodingKey {
692+
case exact, range, revision, branch, localPackage
693+
}
694+
695+
public func encode(to encoder: Encoder) throws {
696+
var container = encoder.container(keyedBy: CodingKeys.self)
697+
switch self {
698+
case let .exact(a1):
699+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .exact)
700+
try unkeyedContainer.encode(a1)
701+
case let .range(a1):
702+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .range)
703+
try unkeyedContainer.encode(CodableRange(a1))
704+
case let .revision(a1):
705+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .revision)
706+
try unkeyedContainer.encode(a1)
707+
case let .branch(a1):
708+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .branch)
709+
try unkeyedContainer.encode(a1)
710+
case .localPackage:
711+
try container.encodeNil(forKey: .localPackage)
712+
}
713+
}
714+
715+
public init(from decoder: Decoder) throws {
716+
let values = try decoder.container(keyedBy: CodingKeys.self)
717+
guard let key = values.allKeys.first(where: values.contains) else {
718+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
719+
}
720+
switch key {
721+
case .exact:
722+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
723+
let a1 = try unkeyedValues.decode(Version.self)
724+
self = .exact(a1)
725+
case .range:
726+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
727+
let a1 = try unkeyedValues.decode(CodableRange<Version>.self)
728+
self = .range(a1.range)
729+
case .revision:
730+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
731+
let a1 = try unkeyedValues.decode(String.self)
732+
self = .revision(a1)
733+
case .branch:
734+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
735+
let a1 = try unkeyedValues.decode(String.self)
736+
self = .branch(a1)
737+
case .localPackage:
738+
self = .localPackage
739+
}
740+
}
602741
}
603742

604743
/// The name of the dependency explicitly defined in the manifest.

Sources/PackageModel/PackageModel+Codable.swift

Lines changed: 0 additions & 189 deletions
This file was deleted.

0 commit comments

Comments
 (0)