use std::sync::mpsc::Receiver;
use std::sync::{Arc, OnceLock};
#[derive(Clone, Debug, PartialEq)]
pub enum LocationOutcome {
Located {
lat: f64,
lon: f64,
accuracy_m: f64,
},
Unavailable(String),
}
pub type LocationChannel = Receiver<LocationOutcome>;
pub trait LocationProvider: Send + Sync {
fn locate(&self) -> LocationChannel;
}
pub fn request_location() -> LocationChannel {
crate::process::current().location.locate()
}
#[must_use]
pub fn unavailable() -> Arc<dyn LocationProvider> {
static NOWHERE: OnceLock<Arc<dyn LocationProvider>> = OnceLock::new();
Arc::clone(NOWHERE.get_or_init(|| Arc::new(Refusing("Location isn't available here"))))
}
struct Refusing(&'static str);
impl LocationProvider for Refusing {
fn locate(&self) -> LocationChannel {
answered(LocationOutcome::Unavailable(self.0.to_owned()))
}
}
fn answered(outcome: LocationOutcome) -> LocationChannel {
let (tx, rx) = std::sync::mpsc::channel();
let _ = tx.send(outcome);
rx
}