Skip to main content

pepl_stdlib/modules/
location.rs

1//! `location` capability module — GPS/location access (host-delegated).
2//!
3//! Functions: current.
4//! Location access is host-delegated — the runtime host reads actual device
5//! sensors via `env.host_call(cap_id=3, fn_id=1, payload)`. This module
6//! validates arguments and returns a `CapabilityCall` error to signal the
7//! caller to route the call to the host.
8//!
9//! # Cap ID / Fn ID Mapping
10//!
11//! | fn_id | Function |
12//! |-------|----------|
13//! | 1     | current  |
14
15use crate::capability::{CAP_LOCATION, LOCATION_CURRENT};
16use crate::error::StdlibError;
17use crate::module::StdlibModule;
18use crate::value::Value;
19
20/// The `location` capability module.
21pub struct LocationModule;
22
23impl LocationModule {
24    pub fn new() -> Self {
25        Self
26    }
27}
28
29impl Default for LocationModule {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl StdlibModule for LocationModule {
36    fn name(&self) -> &'static str {
37        "location"
38    }
39
40    fn has_function(&self, function: &str) -> bool {
41        matches!(function, "current")
42    }
43
44    fn call(&self, function: &str, args: Vec<Value>) -> Result<Value, StdlibError> {
45        match function {
46            "current" => self.current(args),
47            _ => Err(StdlibError::unknown_function("location", function)),
48        }
49    }
50}
51
52impl LocationModule {
53    /// `location.current() -> Result<{ lat: number, lon: number }, LocationError>`
54    ///
55    /// Validates: no args.
56    /// Returns `CapabilityCall` with cap_id=3, fn_id=1.
57    fn current(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
58        if !args.is_empty() {
59            return Err(StdlibError::wrong_args("location.current", 0, args.len()));
60        }
61        Err(StdlibError::capability_call(
62            "location",
63            "current",
64            CAP_LOCATION,
65            LOCATION_CURRENT,
66            args,
67        ))
68    }
69}