@@ -16,6 +16,7 @@ import SWBMacro
16
16
public struct ModuleDependency : Hashable , Sendable , SerializableCodable {
17
17
public let name : String
18
18
public let accessLevel : AccessLevel
19
+ public let optional : Bool
19
20
20
21
public enum AccessLevel : String , Hashable , Sendable , CaseIterable , Codable , Serializable , Comparable {
21
22
case Private = " private "
@@ -43,29 +44,25 @@ public struct ModuleDependency: Hashable, Sendable, SerializableCodable {
43
44
}
44
45
}
45
46
46
- public init ( name: String , accessLevel: AccessLevel ) {
47
+ public init ( name: String , accessLevel: AccessLevel , optional : Bool ) {
47
48
self . name = name
48
49
self . accessLevel = accessLevel
50
+ self . optional = optional
49
51
}
50
52
51
53
public init ( entry: String ) throws {
52
- var it = entry. split ( separator: " " ) . makeIterator ( )
53
- switch ( it. next ( ) , it. next ( ) , it. next ( ) ) {
54
- case ( let . some( name) , nil , nil ) :
55
- self . name = String ( name)
56
- self . accessLevel = . Private
57
-
58
- case ( let . some( accessLevel) , let . some( name) , nil ) :
59
- self . name = String ( name)
60
- self . accessLevel = try AccessLevel ( String ( accessLevel) )
61
-
62
- default :
63
- throw StubError . error ( " expected 1 or 2 space-separated components in: \( entry) " )
54
+ let re = #/((?<accessLevel>private|package|public)\s+)?(?<name>\w+)(?<optional>\?)?/#
55
+ guard let match = entry. wholeMatch ( of: re) else {
56
+ throw StubError . error ( " Invalid module dependency format: \( entry) , expected [private|package|public] <name>[?] " )
64
57
}
58
+
59
+ self . name = String ( match. output. name)
60
+ self . accessLevel = try match. output. accessLevel. map { try AccessLevel ( String ( $0) ) } ?? . Private
61
+ self . optional = match. output. optional != nil
65
62
}
66
63
67
64
public var asBuildSettingEntry : String {
68
- " \( accessLevel == . Private ? " " : " \( accessLevel. rawValue) " ) \( name) "
65
+ " \( accessLevel == . Private ? " " : " \( accessLevel. rawValue) " ) \( name) \( optional ? " ? " : " " ) "
69
66
}
70
67
71
68
public var asBuildSettingEntryQuotedIfNeeded : String {
@@ -121,7 +118,7 @@ public struct ModuleDependenciesContext: Sendable, SerializableCodable {
121
118
122
119
public func computeUnusedDependencies( usedModuleNames: Set < String > ) -> [ ModuleDependency ] {
123
120
guard validateUnused != . no else { return [ ] }
124
- return moduleDependencies. filter { !usedModuleNames. contains ( $0. name) }
121
+ return moduleDependencies. filter { !$0 . optional && ! usedModuleNames. contains ( $0. name) }
125
122
}
126
123
127
124
/// Make diagnostics for missing module dependencies.
0 commit comments