@@ -140,7 +140,12 @@ public enum RelaySelector {
140
140
_ constraints: RelayConstraints ,
141
141
relays: [ RelayWithLocation < T > ]
142
142
) -> [ RelayWithLocation < T > ] {
143
- return relays. filter { relayWithLocation -> Bool in
143
+ // Filter on active status, filter, and location.
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,17 +155,37 @@ 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
167
+ }
168
+
169
+ // Filter on country inclusion.
170
+ let includeInCountryFilteredRelays = filteredRelays. filter { relayWithLocation in
171
+ return switch constraints. locations {
172
+ case . any:
173
+ true
174
+ case let . only( relayConstraint) :
175
+ relayConstraint. locations. contains { location in
176
+ if case . country = location {
177
+ return relayWithLocation. relay. includeInCountry
178
+ }
179
+ return false
180
+ }
181
+ }
182
+ }
183
+
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
164
189
}
165
190
}
166
191
@@ -301,10 +326,9 @@ struct RelayWithLocation<T: AnyRelay> {
301
326
let serverLocation : Location
302
327
303
328
func matches( location: RelayLocation ) -> Bool {
304
- switch location {
329
+ return switch location {
305
330
case let . country( countryCode) :
306
- serverLocation. countryCode == countryCode &&
307
- relay. includeInCountry
331
+ serverLocation. countryCode == countryCode
308
332
309
333
case let . city( countryCode, cityCode) :
310
334
serverLocation. countryCode == countryCode &&
0 commit comments