@@ -140,7 +140,12 @@ public enum RelaySelector {
140
140
_ constraints: RelayConstraints ,
141
141
relays: [ RelayWithLocation < T > ]
142
142
) -> [ RelayWithLocation < T > ] {
143
+ // Filter on active status, filter, and location.
143
144
let filteredRelays = relays. filter { relayWithLocation -> Bool in
145
+ guard relayWithLocation. relay. active else {
146
+ return false
147
+ }
148
+
144
149
switch constraints. filter {
145
150
case . any:
146
151
break
@@ -150,42 +155,38 @@ public enum RelaySelector {
150
155
}
151
156
}
152
157
153
- switch constraints. locations {
158
+ return switch constraints. locations {
154
159
case . any:
155
- return true
160
+ true
156
161
case let . only( relayConstraint) :
157
162
// At least one location must match the relay under test.
158
- return relayConstraint. locations. contains { location in
163
+ relayConstraint. locations. contains { location in
159
164
relayWithLocation. matches ( location: location)
160
165
}
161
166
}
162
- } . filter { relayWithLocation -> Bool in
163
- relayWithLocation. relay. active
164
167
}
165
168
166
- filteredRelays. filter { relayWithLocation in
167
- switch constraints. locations {
169
+ // Filter on country inclusion.
170
+ let includeInCountryFilteredRelays = filteredRelays. filter { relayWithLocation in
171
+ return switch constraints. locations {
168
172
case . any:
169
- return true
173
+ true
170
174
case let . only( relayConstraint) :
171
- // At least one location must match the relay under test.
172
- return relayConstraint. locations. contains { location in
173
- relayWithLocation. matches ( location: location)
175
+ relayConstraint. locations. contains { location in
176
+ if case . country = location {
177
+ return relayWithLocation. relay. includeInCountry
178
+ }
179
+ return false
174
180
}
175
181
}
176
- if case let . country( countryCode) = constraints. locations. {
177
-
178
- }
179
- return true
180
182
}
181
183
182
- // filteredRelays.filter { relayWithLocation in
183
- // if case let .country(countryCode) = relayWithLocation {
184
- //
185
- // }
186
- // }
187
-
188
- return relayWithLocations
184
+ // If no relays should be included in the matched country, instead accept all.
185
+ if includeInCountryFilteredRelays. isEmpty {
186
+ return filteredRelays
187
+ } else {
188
+ return includeInCountryFilteredRelays
189
+ }
189
190
}
190
191
191
192
/// Produce a port that is either user provided or randomly selected, satisfying the given constraints.
0 commit comments