Skip to content

Commit

Permalink
refactor: find distance in km, slightly clean up filters
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Feb 26, 2024
1 parent fd0d3b3 commit 3a7a603
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Coord {
.ok_or_else(|| CoordError::GetCoordsFailed)
}

/// Finds the distance (in meters) between two coordinates using the haversine formula.
/// Finds the distance (in kilometers) between two coordinates using the haversine formula.
pub fn distance_to(&self, other: &Self) -> f64 {
// Earth radius in meters. This is *average*, since Earth is not a sphere, but a spheroid.
const R: f64 = 6_371_000f64;
Expand All @@ -66,6 +66,9 @@ impl Coord {
let hav_delta_lam = phi1.cos() * phi2.cos() * haversine(lam2 - lam1);
let hav_delta = hav_delta_phi + hav_delta_lam;

(2.0 * R * hav_delta.sqrt().asin() * 1_000.0).round() / 1_000.0
// The distance in meters.
let distance = (2.0 * R * hav_delta.sqrt().asin() * 1_000.0).round() / 1_000.0;

distance / 1_000.0
}
}
20 changes: 12 additions & 8 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ pub trait Filter: Debug {
fn matches(&self, relay: &Relay) -> bool;
}

/// Filter by distance. The distance is in kilometers.
#[derive(Debug)]
pub struct FilterByDistance {
user: Coord,
coord: Coord,
distance: f64,
}

impl FilterByDistance {
pub fn new(user: Coord, distance: f64) -> Self {
Self { user, distance }
pub fn new(coord: Coord, distance: f64) -> Self {
Self { coord, distance }
}
}

Expand All @@ -38,16 +39,19 @@ impl Filter for FilterByDistance {
}

fn matches(&self, relay: &Relay) -> bool {
(relay.coord.distance_to(&self.user) / 1_000.0) < self.distance
relay.coord.distance_to(&self.coord) < self.distance
}
}

/// Filter by protocol. `None` means any protocol.
#[derive(Debug)]
pub struct FilterByProtocol(Option<Protocol>);
pub struct FilterByProtocol {
protocol: Option<Protocol>,
}

impl FilterByProtocol {
pub fn new(protocol: Option<Protocol>) -> Self {
Self(protocol)
Self { protocol }
}
}

Expand All @@ -58,8 +62,8 @@ impl Filter for FilterByProtocol {

fn matches(&self, relay: &Relay) -> bool {
self
.0
.protocol
.as_ref()
.map_or(true, |use_protocol| relay.protocol == *use_protocol)
.map_or(true, |protocol| relay.protocol == *protocol)
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn main() -> anyhow::Result<()> {
let spinner = Spinner::new();

// -----------------------------------------------------------------------------------------------
// 1. Get current location, either via arguments or via Mullvad API.
// 1. Get the current location, either via arguments or via Mullvad API.
spinner.set_message("Getting current location");

let user = match cli.latitude.zip(cli.longitude) {
Expand All @@ -41,7 +41,7 @@ async fn main() -> anyhow::Result<()> {
thread::sleep(std::time::Duration::from_secs(1));

// -----------------------------------------------------------------------------------------------
// 3. Pinging relays.
// 3. Ping relays.
spinner.set_message("Pinging relays");

let mut tasks = Vec::new();
Expand Down

0 comments on commit 3a7a603

Please sign in to comment.