Skip to content

Commit

Permalink
AccessMethods are now filtered by the daemon rather than `Connectio…
Browse files Browse the repository at this point in the history
…nModesIterator`

Move the duty of filtering active `AccessMethod`s from
`ConnectionModesIterator` to the daemon. This provides more flexibility
in the iterator as it does not need to know about `AccessMethod` at all.
  • Loading branch information
MarkusPettersson98 committed Sep 25, 2023
1 parent 071db39 commit 20e53b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
3 changes: 2 additions & 1 deletion mullvad-daemon/src/access_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ where
.api_access_methods
.api_access_methods
.iter()
.map(|x| x.access_method.clone())
.filter(|api_access_method| api_access_method.enabled())
.map(|api_access_method| api_access_method.access_method.clone())
.collect(),
)
};
Expand Down
21 changes: 6 additions & 15 deletions mullvad-daemon/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ pub struct ConnectionModesIterator {
}

impl ConnectionModesIterator {
pub fn new(modes: Vec<AccessMethod>) -> ConnectionModesIterator {
pub fn new(access_methods: Vec<AccessMethod>) -> ConnectionModesIterator {
Self {
next: None,
available_modes: Self::get_filtered_access_methods(modes),
available_modes: Self::cycle(access_methods),
}
}

Expand All @@ -186,21 +186,12 @@ impl ConnectionModesIterator {
}
/// Update the collection of [`AccessMethod`] which this iterator will
/// return.
pub fn update_access_methods(&mut self, api_access_methods: Vec<AccessMethod>) {
self.available_modes = Self::get_filtered_access_methods(api_access_methods);
pub fn update_access_methods(&mut self, access_methods: Vec<AccessMethod>) {
self.available_modes = Self::cycle(access_methods)
}

/// [`ConnectionModesIterator`] will only consider [`AccessMethod`]s which
/// are explicitly marked as enabled. As such, a pre-processing step before
/// assigning an iterator to `available_modes` is to filter out all disabled
/// [`AccessMethod`]s that may be present in the input.
fn get_filtered_access_methods(
modes: Vec<AccessMethod>,
) -> Box<dyn Iterator<Item = AccessMethod> + Send> {
Box::new(modes
.into_iter()
// .filter(|access_method| access_method.enabled())
.cycle())
fn cycle(access_methods: Vec<AccessMethod>) -> Box<dyn Iterator<Item = AccessMethod> + Send> {
Box::new(access_methods.into_iter().cycle())
}
}

Expand Down
4 changes: 3 additions & 1 deletion mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ where
.api_access_methods
.api_access_methods
.iter()
.map(|x| x.access_method.clone())
// We only care about the access methods which are set to 'enabled' by the user.
.filter(|api_access_method| api_access_method.enabled())
.map(|api_access_method| api_access_method.access_method.clone())
.collect(),
);

Expand Down

0 comments on commit 20e53b5

Please sign in to comment.