fractal/utils/location/
mod.rs

1//! Location API.
2
3use futures_util::Stream;
4use geo_uri::GeoUri;
5
6#[cfg(target_os = "linux")]
7mod linux;
8
9cfg_if::cfg_if! {
10    if #[cfg(target_os = "linux")] {
11        /// The secret API.
12        pub(crate) type Location = linux::LinuxLocation;
13    } else {
14        /// The secret API.
15        pub(crate) type Location = unimplemented::UnimplementedLocation;
16    }
17}
18
19/// Trait implemented by location backends.
20pub(crate) trait LocationExt {
21    /// Whether the location API is available.
22    fn is_available(&self) -> bool;
23
24    /// Initialize the location API.
25    async fn init(&self) -> Result<(), LocationError>;
26
27    /// Listen to a stream of location updates.
28    async fn updates_stream(&self) -> Result<impl Stream<Item = GeoUri> + '_, LocationError>;
29}
30
31/// The fallback location API, used on platforms where it is unimplemented.
32#[cfg(not(target_os = "linux"))]
33mod unimplemented {
34    use super::*;
35
36    #[derive(Debug)]
37    pub(crate) struct UnimplementedLocation;
38
39    impl LocationExt for UnimplementedLocation {
40        /// Whether the location API is available.
41        fn is_available(&self) -> bool {
42            false
43        }
44
45        /// Initialize the location API.
46        async fn init(&self) -> Result<(), LocationError> {
47            unimplemented!()
48        }
49
50        /// Listen to a stream of location updates.
51        async fn updates_stream(&self) -> Result<impl Stream<Item = GeoUri> + '_, LocationError> {
52            unimplemented!()
53        }
54    }
55}
56
57/// High-level errors that can occur while fetching the location.
58#[derive(Debug, Clone, Copy)]
59pub(crate) enum LocationError {
60    /// The user cancelled the request to get the location.
61    Cancelled,
62    /// The location services are disabled on the system.
63    Disabled,
64    /// Another error occurred.
65    Other,
66}