Skip to content

Commit

Permalink
refactor: get rid of useless filter stages
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Feb 27, 2024
1 parent e69b5a7 commit d68682f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 38 deletions.
23 changes: 0 additions & 23 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ use std::time::Duration;
use crate::pinger::RelayTimed;
use crate::relays::{Protocol, Relay};

#[derive(PartialEq)]
pub enum FilterStage {
/// Such filters apply when loading them from the relays file.
Load,
/// Such filters apply after pinging relays.
Ping,
}

/// Filter trait to dynamically dispatch filters.
pub trait Filter: Debug {
type Item;

/// Returns the stage of the filter.
fn stage(&self) -> FilterStage;

/// Filter predicate.
fn matches(&self, item: &Self::Item) -> bool;
}
Expand All @@ -39,10 +28,6 @@ impl FilterByDistance {
impl Filter for FilterByDistance {
type Item = Relay;

fn stage(&self) -> FilterStage {
FilterStage::Load
}

fn matches(&self, relay: &Self::Item) -> bool {
relay.distance < self.distance
}
Expand All @@ -64,10 +49,6 @@ impl FilterByProtocol {
impl Filter for FilterByProtocol {
type Item = Relay;

fn stage(&self) -> FilterStage {
FilterStage::Load
}

fn matches(&self, relay: &Self::Item) -> bool {
self
.protocol
Expand All @@ -92,10 +73,6 @@ impl FilterByRTT {
impl Filter for FilterByRTT {
type Item = RelayTimed;

fn stage(&self) -> FilterStage {
FilterStage::Ping
}

fn matches(&self, timings: &Self::Item) -> bool {
// If `rtt` is `None`, then it means any RTT, so we then default to `true`.
self.rtt.map_or(true, |filter_rtt| {
Expand Down
9 changes: 2 additions & 7 deletions src/pinger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::net::TcpStream;
use tokio::task::JoinHandle;
use tokio::time::{self, Duration, Instant, MissedTickBehavior};

use crate::filters::{Filter, FilterStage};
use crate::filters::Filter;
use crate::relays::Relay;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -192,12 +192,7 @@ impl RelaysPinger {
.await
.map_err(|_| RelaysPingerError::PingerAwaitFailed)?;

if self
.filters
.iter()
.filter(|filter| filter.stage() == FilterStage::Ping)
.all(|filter| filter.matches(&timings))
{
if self.filters.iter().all(|filter| filter.matches(&timings)) {
results.push(timings);
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/relays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::Value;
use thiserror::Error;

use crate::coord::Coord;
use crate::filters::{Filter, FilterStage};
use crate::filters::Filter;

#[derive(Debug, Error)]
pub enum RelaysError {
Expand Down Expand Up @@ -162,13 +162,7 @@ impl RelaysLoader {
};

// There's no reason to filter inactive relays.
if relay.is_active
&& self
.filters
.iter()
.filter(|filter| filter.stage() == FilterStage::Load)
.all(|filter| filter.matches(&relay))
{
if relay.is_active && self.filters.iter().all(|filter| filter.matches(&relay)) {
locations.push(relay);
}
}
Expand Down

0 comments on commit d68682f

Please sign in to comment.