Skip to main content

lingxia_platform/traits/
location.rs

1use std::future::Future;
2
3use crate::error::PlatformError;
4
5pub const DEFAULT_LOCATION_TIMEOUT_MS: u32 = 10_000;
6
7#[derive(Debug, Clone, Default)]
8pub struct LocationRequestConfig {
9    pub is_high_accuracy: bool,
10    pub high_accuracy_expire_time: Option<u32>,
11    pub include_altitude: bool,
12}
13
14impl LocationRequestConfig {
15    pub fn effective_timeout_ms(&self) -> u32 {
16        self.high_accuracy_expire_time
17            .unwrap_or(DEFAULT_LOCATION_TIMEOUT_MS)
18    }
19}
20
21pub trait Location: Send + Sync + 'static {
22    fn is_location_enabled(&self) -> Result<bool, PlatformError>;
23
24    fn request_location(
25        &self,
26        config: LocationRequestConfig,
27    ) -> impl Future<Output = Result<String, PlatformError>> + Send;
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn location_timeout_defaults_to_ten_seconds() {
36        assert_eq!(
37            LocationRequestConfig::default().effective_timeout_ms(),
38            10_000
39        );
40    }
41
42    #[test]
43    fn location_timeout_preserves_explicit_value() {
44        let config = LocationRequestConfig {
45            high_accuracy_expire_time: Some(2_500),
46            ..LocationRequestConfig::default()
47        };
48        assert_eq!(config.effective_timeout_ms(), 2_500);
49    }
50}