Skip to content

Commit 3a37b3f

Browse files
committed
Move ProductFilter declaration
Reorganize ProductFilter protocol conformance
1 parent c26db39 commit 3a37b3f

File tree

2 files changed

+100
-88
lines changed

2 files changed

+100
-88
lines changed

Sources/PackageModel/Manifest.swift

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -367,91 +367,3 @@ public final class Manifest: ObjectIdentifierProtocol, CustomStringConvertible,
367367
try container.encode(packageKind, forKey: .packageKind)
368368
}
369369
}
370-
371-
/// The products requested of a package.
372-
///
373-
/// Any product which matches the filter will be used for dependency resolution, whereas unrequested products will be ingored.
374-
///
375-
/// Requested products need not actually exist in the package. Under certain circumstances, the resolver may request names whose package of origin are unknown. The intended package will recognize and fullfill the request; packages that do not know what it is will simply ignore it.
376-
public enum ProductFilter: Equatable, Hashable, Codable, JSONMappable, JSONSerializable, CustomStringConvertible {
377-
378-
/// All products, targets, and tests are requested.
379-
///
380-
/// This is used for root packages.
381-
case everything
382-
383-
/// A set of specific products requested by one or more client packages.
384-
case specific(Set<String>)
385-
386-
public func union(_ other: ProductFilter) -> ProductFilter {
387-
switch self {
388-
case .everything:
389-
return .everything
390-
case .specific(let set):
391-
switch other {
392-
case .everything:
393-
return .everything
394-
case .specific(let otherSet):
395-
return .specific(set.union(otherSet))
396-
}
397-
}
398-
}
399-
public mutating func formUnion(_ other: ProductFilter) {
400-
self = self.union(other)
401-
}
402-
403-
public func contains(_ product: String) -> Bool {
404-
switch self {
405-
case .everything:
406-
return true
407-
case .specific(let set):
408-
return set.contains(product)
409-
}
410-
}
411-
412-
public func encode(to encoder: Encoder) throws {
413-
let optionalSet: Set<String>?
414-
switch self {
415-
case .everything:
416-
optionalSet = nil
417-
case .specific(let set):
418-
optionalSet = set
419-
}
420-
var container = encoder.singleValueContainer()
421-
try container.encode(optionalSet?.sorted())
422-
}
423-
public init(from decoder: Decoder) throws {
424-
let container = try decoder.singleValueContainer()
425-
let optionalSet: Set<String>? = try container.decode([String]?.self).map { Set($0) }
426-
if let set = optionalSet {
427-
self = .specific(set)
428-
} else {
429-
self = .everything
430-
}
431-
}
432-
433-
public func toJSON() -> JSON {
434-
switch self {
435-
case .everything:
436-
return "all".toJSON()
437-
case .specific(let products):
438-
return products.sorted().toJSON()
439-
}
440-
}
441-
public init(json: JSON) throws {
442-
if let products = try? [String](json: json) {
443-
self = .specific(Set(products))
444-
} else {
445-
self = .everything
446-
}
447-
}
448-
449-
public var description: String {
450-
switch self {
451-
case .everything:
452-
return "[everything]"
453-
case .specific(let set):
454-
return "[\(set.sorted().joined(separator: ", "))]"
455-
}
456-
}
457-
}

Sources/PackageModel/Product.swift

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,50 @@ public enum ProductType: Equatable {
7474
case test
7575
}
7676

77+
/// The products requested of a package.
78+
///
79+
/// Any product which matches the filter will be used for dependency resolution, whereas unrequested products will be ingored.
80+
///
81+
/// Requested products need not actually exist in the package. Under certain circumstances, the resolver may request names whose package of origin are unknown. The intended package will recognize and fullfill the request; packages that do not know what it is will simply ignore it.
82+
public enum ProductFilter: Equatable, Hashable {
83+
84+
/// All products, targets, and tests are requested.
85+
///
86+
/// This is used for root packages.
87+
case everything
88+
89+
/// A set of specific products requested by one or more client packages.
90+
case specific(Set<String>)
91+
92+
public func union(_ other: ProductFilter) -> ProductFilter {
93+
switch self {
94+
case .everything:
95+
return .everything
96+
case .specific(let set):
97+
switch other {
98+
case .everything:
99+
return .everything
100+
case .specific(let otherSet):
101+
return .specific(set.union(otherSet))
102+
}
103+
}
104+
}
105+
106+
public mutating func formUnion(_ other: ProductFilter) {
107+
self = self.union(other)
108+
}
109+
110+
public func contains(_ product: String) -> Bool {
111+
switch self {
112+
case .everything:
113+
return true
114+
case .specific(let set):
115+
return set.contains(product)
116+
}
117+
}
118+
}
119+
120+
77121
// MARK: - CustomStringConvertible
78122

79123
extension Product: CustomStringConvertible {
@@ -102,6 +146,17 @@ extension ProductType: CustomStringConvertible {
102146
}
103147
}
104148

149+
extension ProductFilter: CustomStringConvertible {
150+
public var description: String {
151+
switch self {
152+
case .everything:
153+
return "[everything]"
154+
case .specific(let set):
155+
return "[\(set.sorted().joined(separator: ", "))]"
156+
}
157+
}
158+
}
159+
105160
// MARK: - Codable
106161

107162
extension ProductType: Codable {
@@ -139,3 +194,48 @@ extension ProductType: Codable {
139194
}
140195
}
141196
}
197+
198+
extension ProductFilter: Codable {
199+
public func encode(to encoder: Encoder) throws {
200+
let optionalSet: Set<String>?
201+
switch self {
202+
case .everything:
203+
optionalSet = nil
204+
case .specific(let set):
205+
optionalSet = set
206+
}
207+
var container = encoder.singleValueContainer()
208+
try container.encode(optionalSet?.sorted())
209+
}
210+
211+
public init(from decoder: Decoder) throws {
212+
let container = try decoder.singleValueContainer()
213+
let optionalSet: Set<String>? = try container.decode([String]?.self).map { Set($0) }
214+
if let set = optionalSet {
215+
self = .specific(set)
216+
} else {
217+
self = .everything
218+
}
219+
}
220+
}
221+
222+
// MARK: - JSON
223+
224+
extension ProductFilter: JSONSerializable, JSONMappable {
225+
public func toJSON() -> JSON {
226+
switch self {
227+
case .everything:
228+
return "all".toJSON()
229+
case .specific(let products):
230+
return products.sorted().toJSON()
231+
}
232+
}
233+
234+
public init(json: JSON) throws {
235+
if let products = try? [String](json: json) {
236+
self = .specific(Set(products))
237+
} else {
238+
self = .everything
239+
}
240+
}
241+
}

0 commit comments

Comments
 (0)