Skip to content

Commit 6a061a0

Browse files
Move ResolvedLocationConstraint to mullvad-relay-selector
1 parent 22dd508 commit 6a061a0

File tree

2 files changed

+68
-88
lines changed

2 files changed

+68
-88
lines changed

mullvad-relay-selector/src/relay_selector/matcher.rs

+65-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use mullvad_types::{
33
constraints::{Constraint, Match},
44
custom_list::CustomListsSettings,
55
relay_constraints::{
6-
BridgeState, InternalBridgeConstraints, Ownership, Providers, ResolvedLocationConstraint,
7-
TransportPort,
6+
BridgeState, GeographicLocationConstraint, InternalBridgeConstraints, LocationConstraint,
7+
Ownership, Providers, TransportPort,
88
},
99
relay_list::{
1010
OpenVpnEndpoint, OpenVpnEndpointData, Relay, RelayEndpointData, WireguardEndpointData,
@@ -286,3 +286,66 @@ fn openvpn_filter_on_port(port: Constraint<TransportPort>, endpoint: &OpenVpnEnd
286286
.any(|port| compatible_port(transport_port, port)),
287287
}
288288
}
289+
290+
// -- Wrapper around LocationConstraint --
291+
292+
#[derive(Debug, Clone)]
293+
pub struct ResolvedLocationConstraint(Vec<GeographicLocationConstraint>);
294+
295+
impl<'a> IntoIterator for &'a ResolvedLocationConstraint {
296+
type Item = &'a GeographicLocationConstraint;
297+
298+
type IntoIter = core::slice::Iter<'a, GeographicLocationConstraint>;
299+
300+
fn into_iter(self) -> Self::IntoIter {
301+
self.0.iter()
302+
}
303+
}
304+
305+
impl IntoIterator for ResolvedLocationConstraint {
306+
type Item = GeographicLocationConstraint;
307+
308+
type IntoIter = std::vec::IntoIter<GeographicLocationConstraint>;
309+
310+
fn into_iter(self) -> Self::IntoIter {
311+
self.0.into_iter()
312+
}
313+
}
314+
315+
impl FromIterator<GeographicLocationConstraint> for ResolvedLocationConstraint {
316+
fn from_iter<T: IntoIterator<Item = GeographicLocationConstraint>>(iter: T) -> Self {
317+
Self(Vec::from_iter(iter))
318+
}
319+
}
320+
321+
impl ResolvedLocationConstraint {
322+
pub fn from_constraint(
323+
location_constraint: Constraint<LocationConstraint>,
324+
custom_lists: &CustomListsSettings,
325+
) -> Constraint<ResolvedLocationConstraint> {
326+
location_constraint.map(|location| Self::from_location_constraint(location, custom_lists))
327+
}
328+
329+
fn from_location_constraint(
330+
location: LocationConstraint,
331+
custom_lists: &CustomListsSettings,
332+
) -> ResolvedLocationConstraint {
333+
match location {
334+
LocationConstraint::Location(location) => Self::from_iter(std::iter::once(location)),
335+
LocationConstraint::CustomList { list_id } => custom_lists
336+
.iter()
337+
.find(|list| list.id == list_id)
338+
.map(|custom_list| Self::from_iter(custom_list.locations.clone()))
339+
.unwrap_or_else(|| {
340+
log::warn!("Resolved non-existent custom list");
341+
Self::from_iter(std::iter::empty())
342+
}),
343+
}
344+
}
345+
}
346+
347+
impl Match<Relay> for ResolvedLocationConstraint {
348+
fn matches(&self, relay: &Relay) -> bool {
349+
self.into_iter().any(|location| location.matches(relay))
350+
}
351+
}

mullvad-types/src/relay_constraints.rs

+3-86
Original file line numberDiff line numberDiff line change
@@ -85,59 +85,9 @@ pub enum LocationConstraint {
8585
CustomList { list_id: Id },
8686
}
8787

88-
#[derive(Debug, Clone)]
89-
pub struct ResolvedLocationConstraint(Vec<GeographicLocationConstraint>);
90-
91-
impl<'a> IntoIterator for &'a ResolvedLocationConstraint {
92-
type Item = &'a GeographicLocationConstraint;
93-
94-
type IntoIter = core::slice::Iter<'a, GeographicLocationConstraint>;
95-
96-
fn into_iter(self) -> Self::IntoIter {
97-
self.0.iter()
98-
}
99-
}
100-
101-
impl IntoIterator for ResolvedLocationConstraint {
102-
type Item = GeographicLocationConstraint;
103-
104-
type IntoIter = std::vec::IntoIter<GeographicLocationConstraint>;
105-
106-
fn into_iter(self) -> Self::IntoIter {
107-
self.0.into_iter()
108-
}
109-
}
110-
111-
impl FromIterator<GeographicLocationConstraint> for ResolvedLocationConstraint {
112-
fn from_iter<T: IntoIterator<Item = GeographicLocationConstraint>>(iter: T) -> Self {
113-
Self(Vec::from_iter(iter))
114-
}
115-
}
116-
117-
impl ResolvedLocationConstraint {
118-
pub fn from_constraint(
119-
location_constraint: Constraint<LocationConstraint>,
120-
custom_lists: &CustomListsSettings,
121-
) -> Constraint<ResolvedLocationConstraint> {
122-
location_constraint.map(|location| Self::from_location_constraint(location, custom_lists))
123-
}
124-
125-
fn from_location_constraint(
126-
location: LocationConstraint,
127-
custom_lists: &CustomListsSettings,
128-
) -> ResolvedLocationConstraint {
129-
match location {
130-
LocationConstraint::Location(location) => Self::from_iter(std::iter::once(location)),
131-
LocationConstraint::CustomList { list_id } => custom_lists
132-
.iter()
133-
.find(|list| list.id == list_id)
134-
.map(|custom_list| Self::from_iter(custom_list.locations.clone()))
135-
.unwrap_or_else(|| {
136-
log::warn!("Resolved non-existent custom list");
137-
Self::from_iter(std::iter::empty())
138-
}),
139-
}
140-
}
88+
pub struct LocationConstraintFormatter<'a> {
89+
pub constraint: &'a LocationConstraint,
90+
pub custom_lists: &'a CustomListsSettings,
14191
}
14292

14393
impl From<GeographicLocationConstraint> for LocationConstraint {
@@ -146,39 +96,6 @@ impl From<GeographicLocationConstraint> for LocationConstraint {
14696
}
14797
}
14898

149-
impl Set<Constraint<ResolvedLocationConstraint>> for Constraint<ResolvedLocationConstraint> {
150-
fn is_subset(&self, other: &Self) -> bool {
151-
match self {
152-
Constraint::Any => other.is_any(),
153-
Constraint::Only(locations) => match other {
154-
Constraint::Any => true,
155-
Constraint::Only(other_locations) => {
156-
for location in locations {
157-
if !other_locations
158-
.into_iter()
159-
.any(|other_location| location.is_subset(other_location))
160-
{
161-
return false;
162-
}
163-
}
164-
true
165-
}
166-
},
167-
}
168-
}
169-
}
170-
171-
impl Match<Relay> for ResolvedLocationConstraint {
172-
fn matches(&self, relay: &Relay) -> bool {
173-
self.into_iter().any(|location| location.matches(relay))
174-
}
175-
}
176-
177-
pub struct LocationConstraintFormatter<'a> {
178-
pub constraint: &'a LocationConstraint,
179-
pub custom_lists: &'a CustomListsSettings,
180-
}
181-
18299
impl<'a> fmt::Display for LocationConstraintFormatter<'a> {
183100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184101
match self.constraint {

0 commit comments

Comments
 (0)