From 3a7a603e67651966965b691cc8c5aa59a88f8a21 Mon Sep 17 00:00:00 2001 From: Vladislav Mamon Date: Mon, 26 Feb 2024 12:10:20 +0300 Subject: [PATCH] refactor: find distance in km, slightly clean up filters --- src/coord.rs | 7 +++++-- src/filters.rs | 20 ++++++++++++-------- src/main.rs | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/coord.rs b/src/coord.rs index 02d0499..61f67ab 100644 --- a/src/coord.rs +++ b/src/coord.rs @@ -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; @@ -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 } } diff --git a/src/filters.rs b/src/filters.rs index 6eac381..c8ef8de 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -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 } } } @@ -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); +pub struct FilterByProtocol { + protocol: Option, +} impl FilterByProtocol { pub fn new(protocol: Option) -> Self { - Self(protocol) + Self { protocol } } } @@ -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) } } diff --git a/src/main.rs b/src/main.rs index 99ac4e0..9e7d571 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { @@ -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();