Skip to content

Commit b58782f

Browse files
committed
Implement protocol conformance in extensions
1 parent 136fd40 commit b58782f

9 files changed

+208
-189
lines changed

Sources/PackageModel/Manifest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414

1515
/// This contains the declarative specification loaded from package manifest
1616
/// files, and the tools for working with the manifest.
17-
public final class Manifest: ObjectIdentifierProtocol, CustomStringConvertible, Codable {
17+
public final class Manifest: ObjectIdentifierProtocol {
1818

1919
/// The standard filename for the manifest.
2020
public static let filename = basename + ".swift"
@@ -327,15 +327,15 @@ public final class Manifest: ObjectIdentifierProtocol, CustomStringConvertible,
327327
return false
328328
}
329329
}
330+
}
330331

331-
// MARK: - CustomStringConvertible
332-
332+
extension Manifest: CustomStringConvertible {
333333
public var description: String {
334334
return "<Manifest: \(name)>"
335335
}
336+
}
336337

337-
// MARK: - Codable
338-
338+
extension Manifest: Codable {
339339
/// Coding user info key for dump-package command.
340340
///
341341
/// Presence of this key will hide some keys when encoding the Manifest object.

Sources/PackageModel/Manifest/PackageDependencyDescription.swift

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import TSCBasic
1414
public struct PackageDependencyDescription: Equatable, Codable {
1515

1616
/// The dependency requirement.
17-
public enum Requirement: Equatable, Hashable, CustomStringConvertible, Codable {
17+
public enum Requirement: Equatable, Hashable {
1818
case exact(Version)
1919
case range(Range<Version>)
2020
case revision(String)
@@ -28,72 +28,6 @@ public struct PackageDependencyDescription: Equatable, Codable {
2828
public static func upToNextMinor(from version: TSCUtility.Version) -> Requirement {
2929
return .range(version..<Version(version.major, version.minor + 1, 0))
3030
}
31-
32-
public var description: String {
33-
switch self {
34-
case .exact(let version):
35-
return version.description
36-
case .range(let range):
37-
return range.description
38-
case .revision(let revision):
39-
return "revision[\(revision)]"
40-
case .branch(let branch):
41-
return "branch[\(branch)]"
42-
case .localPackage:
43-
return "local"
44-
}
45-
}
46-
47-
private enum CodingKeys: String, CodingKey {
48-
case exact, range, revision, branch, localPackage
49-
}
50-
51-
public func encode(to encoder: Encoder) throws {
52-
var container = encoder.container(keyedBy: CodingKeys.self)
53-
switch self {
54-
case let .exact(a1):
55-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .exact)
56-
try unkeyedContainer.encode(a1)
57-
case let .range(a1):
58-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .range)
59-
try unkeyedContainer.encode(CodableRange(a1))
60-
case let .revision(a1):
61-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .revision)
62-
try unkeyedContainer.encode(a1)
63-
case let .branch(a1):
64-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .branch)
65-
try unkeyedContainer.encode(a1)
66-
case .localPackage:
67-
try container.encodeNil(forKey: .localPackage)
68-
}
69-
}
70-
71-
public init(from decoder: Decoder) throws {
72-
let values = try decoder.container(keyedBy: CodingKeys.self)
73-
guard let key = values.allKeys.first(where: values.contains) else {
74-
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
75-
}
76-
switch key {
77-
case .exact:
78-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
79-
let a1 = try unkeyedValues.decode(Version.self)
80-
self = .exact(a1)
81-
case .range:
82-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
83-
let a1 = try unkeyedValues.decode(CodableRange<Version>.self)
84-
self = .range(a1.range)
85-
case .revision:
86-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
87-
let a1 = try unkeyedValues.decode(String.self)
88-
self = .revision(a1)
89-
case .branch:
90-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
91-
let a1 = try unkeyedValues.decode(String.self)
92-
self = .branch(a1)
93-
case .localPackage:
94-
self = .localPackage
95-
}
96-
}
9731
}
9832

9933
/// The name of the dependency explicitly defined in the manifest.
@@ -116,3 +50,73 @@ public struct PackageDependencyDescription: Equatable, Codable {
11650
self.requirement = requirement
11751
}
11852
}
53+
54+
extension PackageDependencyDescription.Requirement: CustomStringConvertible {
55+
public var description: String {
56+
switch self {
57+
case .exact(let version):
58+
return version.description
59+
case .range(let range):
60+
return range.description
61+
case .revision(let revision):
62+
return "revision[\(revision)]"
63+
case .branch(let branch):
64+
return "branch[\(branch)]"
65+
case .localPackage:
66+
return "local"
67+
}
68+
}
69+
}
70+
71+
extension PackageDependencyDescription.Requirement: Codable {
72+
private enum CodingKeys: String, CodingKey {
73+
case exact, range, revision, branch, localPackage
74+
}
75+
76+
public func encode(to encoder: Encoder) throws {
77+
var container = encoder.container(keyedBy: CodingKeys.self)
78+
switch self {
79+
case let .exact(a1):
80+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .exact)
81+
try unkeyedContainer.encode(a1)
82+
case let .range(a1):
83+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .range)
84+
try unkeyedContainer.encode(CodableRange(a1))
85+
case let .revision(a1):
86+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .revision)
87+
try unkeyedContainer.encode(a1)
88+
case let .branch(a1):
89+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .branch)
90+
try unkeyedContainer.encode(a1)
91+
case .localPackage:
92+
try container.encodeNil(forKey: .localPackage)
93+
}
94+
}
95+
96+
public init(from decoder: Decoder) throws {
97+
let values = try decoder.container(keyedBy: CodingKeys.self)
98+
guard let key = values.allKeys.first(where: values.contains) else {
99+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
100+
}
101+
switch key {
102+
case .exact:
103+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
104+
let a1 = try unkeyedValues.decode(Version.self)
105+
self = .exact(a1)
106+
case .range:
107+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
108+
let a1 = try unkeyedValues.decode(CodableRange<Version>.self)
109+
self = .range(a1.range)
110+
case .revision:
111+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
112+
let a1 = try unkeyedValues.decode(String.self)
113+
self = .revision(a1)
114+
case .branch:
115+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
116+
let a1 = try unkeyedValues.decode(String.self)
117+
self = .branch(a1)
118+
case .localPackage:
119+
self = .localPackage
120+
}
121+
}
122+
}

Sources/PackageModel/Manifest/SystemPackageProviderDescription.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public enum SystemPackageProviderDescription: Equatable, Codable {
1313
case brew([String])
1414
case apt([String])
1515
case yum([String])
16+
}
1617

18+
extension SystemPackageProviderDescription {
1719
private enum CodingKeys: String, CodingKey {
1820
case brew, apt, yum
1921
}

Sources/PackageModel/Manifest/TargetDescription.swift

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,18 @@ public struct TargetDescription: Equatable, Codable {
2020
}
2121

2222
/// Represents a target's dependency on another entity.
23-
public enum Dependency: Equatable, ExpressibleByStringLiteral, Codable {
23+
public enum Dependency: Equatable {
2424
case target(name: String, condition: PackageConditionDescription?)
2525
case product(name: String, package: String?, condition: PackageConditionDescription?)
2626
case byName(name: String, condition: PackageConditionDescription?)
2727

28-
public init(stringLiteral value: String) {
29-
self = .byName(name: value, condition: nil)
30-
}
31-
3228
public static func target(name: String) -> Dependency {
3329
return .target(name: name, condition: nil)
3430
}
3531

3632
public static func product(name: String, package: String? = nil) -> Dependency {
3733
return .product(name: name, package: package, condition: nil)
3834
}
39-
40-
private enum CodingKeys: String, CodingKey {
41-
case target, product, byName
42-
}
43-
44-
public func encode(to encoder: Encoder) throws {
45-
var container = encoder.container(keyedBy: CodingKeys.self)
46-
switch self {
47-
case let .target(a1, a2):
48-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .target)
49-
try unkeyedContainer.encode(a1)
50-
try unkeyedContainer.encode(a2)
51-
case let .product(a1, a2, a3):
52-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .product)
53-
try unkeyedContainer.encode(a1)
54-
try unkeyedContainer.encode(a2)
55-
try unkeyedContainer.encode(a3)
56-
case let .byName(a1, a2):
57-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .byName)
58-
try unkeyedContainer.encode(a1)
59-
try unkeyedContainer.encode(a2)
60-
}
61-
}
62-
63-
public init(from decoder: Decoder) throws {
64-
let values = try decoder.container(keyedBy: CodingKeys.self)
65-
guard let key = values.allKeys.first(where: values.contains) else {
66-
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
67-
}
68-
switch key {
69-
case .target:
70-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
71-
let a1 = try unkeyedValues.decode(String.self)
72-
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
73-
self = .target(name: a1, condition: a2)
74-
case .product:
75-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
76-
let a1 = try unkeyedValues.decode(String.self)
77-
let a2 = try unkeyedValues.decodeIfPresent(String.self)
78-
let a3 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
79-
self = .product(name: a1, package: a2, condition: a3)
80-
case .byName:
81-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
82-
let a1 = try unkeyedValues.decode(String.self)
83-
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
84-
self = .byName(name: a1, condition: a2)
85-
}
86-
}
8735
}
8836

8937
public struct Resource: Codable, Equatable {
@@ -222,3 +170,59 @@ public struct TargetDescription: Equatable, Codable {
222170
self.checksum = checksum
223171
}
224172
}
173+
174+
extension TargetDescription.Dependency: Codable {
175+
private enum CodingKeys: String, CodingKey {
176+
case target, product, byName
177+
}
178+
179+
public func encode(to encoder: Encoder) throws {
180+
var container = encoder.container(keyedBy: CodingKeys.self)
181+
switch self {
182+
case let .target(a1, a2):
183+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .target)
184+
try unkeyedContainer.encode(a1)
185+
try unkeyedContainer.encode(a2)
186+
case let .product(a1, a2, a3):
187+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .product)
188+
try unkeyedContainer.encode(a1)
189+
try unkeyedContainer.encode(a2)
190+
try unkeyedContainer.encode(a3)
191+
case let .byName(a1, a2):
192+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .byName)
193+
try unkeyedContainer.encode(a1)
194+
try unkeyedContainer.encode(a2)
195+
}
196+
}
197+
198+
public init(from decoder: Decoder) throws {
199+
let values = try decoder.container(keyedBy: CodingKeys.self)
200+
guard let key = values.allKeys.first(where: values.contains) else {
201+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
202+
}
203+
switch key {
204+
case .target:
205+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
206+
let a1 = try unkeyedValues.decode(String.self)
207+
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
208+
self = .target(name: a1, condition: a2)
209+
case .product:
210+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
211+
let a1 = try unkeyedValues.decode(String.self)
212+
let a2 = try unkeyedValues.decodeIfPresent(String.self)
213+
let a3 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
214+
self = .product(name: a1, package: a2, condition: a3)
215+
case .byName:
216+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
217+
let a1 = try unkeyedValues.decode(String.self)
218+
let a2 = try unkeyedValues.decodeIfPresent(PackageConditionDescription.self)
219+
self = .byName(name: a1, condition: a2)
220+
}
221+
}
222+
}
223+
224+
extension TargetDescription.Dependency: ExpressibleByStringLiteral {
225+
public init(stringLiteral value: String) {
226+
self = .byName(name: value, condition: nil)
227+
}
228+
}

Sources/PackageModel/Package.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import TSCUtility
4242
/// 5. A loaded package, as in #4, for which the targets have also been
4343
/// loaded. There is not currently a data structure for this, but it is the
4444
/// result after `PackageLoading.transmute()`.
45-
public final class Package: Codable {
45+
public final class Package: ObjectIdentifierProtocol, Codable {
4646
/// The manifest describing the package.
4747
public let manifest: Manifest
4848

@@ -91,6 +91,13 @@ public final class Package: Codable {
9191
case noManifest(baseURL: String, version: String?)
9292
}
9393
}
94+
95+
extension Package: CustomStringConvertible {
96+
public var description: String {
97+
return name
98+
}
99+
}
100+
94101
extension Package.Error: CustomStringConvertible {
95102
public var description: String {
96103
switch self {
@@ -103,13 +110,3 @@ extension Package.Error: CustomStringConvertible {
103110
}
104111
}
105112
}
106-
107-
extension Package: CustomStringConvertible {
108-
public var description: String {
109-
return name
110-
}
111-
}
112-
113-
extension Package: ObjectIdentifierProtocol {
114-
}
115-

0 commit comments

Comments
 (0)