@@ -16,6 +16,7 @@ import SWBMacro
1616public struct ModuleDependency : Hashable , Sendable , SerializableCodable {
1717 public let name : String
1818 public let accessLevel : AccessLevel
19+ public let optional : Bool
1920
2021 public enum AccessLevel : String , Hashable , Sendable , CaseIterable , Codable , Serializable , Comparable {
2122 case Private = " private "
@@ -43,29 +44,25 @@ public struct ModuleDependency: Hashable, Sendable, SerializableCodable {
4344 }
4445 }
4546
46- public init ( name: String , accessLevel: AccessLevel ) {
47+ public init ( name: String , accessLevel: AccessLevel , optional : Bool ) {
4748 self . name = name
4849 self . accessLevel = accessLevel
50+ self . optional = optional
4951 }
5052
5153 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>[?] " )
6457 }
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
6562 }
6663
6764 public var asBuildSettingEntry : String {
68- " \( accessLevel == . Private ? " " : " \( accessLevel. rawValue) " ) \( name) "
65+ " \( accessLevel == . Private ? " " : " \( accessLevel. rawValue) " ) \( name) \( optional ? " ? " : " " ) "
6966 }
7067
7168 public var asBuildSettingEntryQuotedIfNeeded : String {
@@ -121,7 +118,7 @@ public struct ModuleDependenciesContext: Sendable, SerializableCodable {
121118
122119 public func computeUnusedDependencies( usedModuleNames: Set < String > ) -> [ ModuleDependency ] {
123120 guard validateUnused != . no else { return [ ] }
124- return moduleDependencies. filter { !usedModuleNames. contains ( $0. name) }
121+ return moduleDependencies. filter { !$0 . optional && ! usedModuleNames. contains ( $0. name) }
125122 }
126123
127124 /// Make diagnostics for missing module dependencies.
0 commit comments