Skip to main content

geodesy/context/
mod.rs

1use std::sync::Arc;
2
3use crate::authoring::*;
4pub mod minimal;
5
6#[cfg(feature = "with_plain")]
7pub mod plain;
8
9/// The key, returned to the user, representing the actual operation handled by the `Context`
10#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
11pub struct OpHandle(uuid::Uuid);
12impl OpHandle {
13    pub fn new() -> Self {
14        OpHandle(uuid::Uuid::new_v4())
15    }
16}
17impl Default for OpHandle {
18    fn default() -> Self {
19        OpHandle(uuid::Uuid::new_v4())
20    }
21}
22
23// ----- T H E   C O N T E X T   T R A I T ---------------------------------------------
24
25/// Modes of communication between the *Rust Geodesy* internals and the external
26/// world (i.e. resources like grids, transformation definitions, or ellipsoid parameters).
27pub trait Context {
28    /// In general, implementations should make sure that `new` differs from `default`
29    /// only by adding access to the builtin adaptors (`geo:in`, `gis:out` etc.)
30    fn new() -> Self
31    where
32        Self: Sized;
33
34    /// Instantiate the operation given by `definition`
35    fn op(&mut self, definition: &str) -> Result<OpHandle, Error>;
36
37    /// Apply operation `op` to `operands`
38    fn apply(
39        &self,
40        op: OpHandle,
41        direction: Direction,
42        operands: &mut dyn CoordinateSet,
43    ) -> Result<usize, Error>;
44
45    /// Globally defined default values (typically just `ellps=GRS80`)
46    fn globals(&self) -> BTreeMap<String, String>;
47
48    /// Definitions of steps
49    fn steps(&self, op: OpHandle) -> Result<Vec<String>, Error>;
50
51    /// Parsed parameters of a specific step
52    fn params(&self, op: OpHandle, index: usize) -> Result<ParsedParameters, Error>;
53
54    /// Register a new user-defined operator
55    fn register_op(&mut self, name: &str, constructor: OpConstructor);
56
57    /// Register a new user-defined resource (macro, ellipsoid parameter set...)
58    fn register_resource(&mut self, name: &str, definition: &str);
59
60    /// Helper for the `Op` instantiation logic in `Op::op(...)`
61    fn get_op(&self, name: &str) -> Result<OpConstructor, Error>;
62
63    /// Helper for the `Op` instantiation logic in `Op::op(...)`
64    fn get_resource(&self, name: &str) -> Result<String, Error>;
65
66    /// Access `blob`-like resources by identifier
67    fn get_blob(&self, name: &str) -> Result<Vec<u8>, Error>;
68
69    /// Access grid resources by identifier
70    fn get_grid(&self, name: &str) -> Result<Arc<BaseGrid>, Error>;
71
72    /// Get search paths for external grids, resources, etc.
73    fn get_paths(&self) -> Vec<std::path::PathBuf> {
74        Vec::new()
75    }
76
77    /// Get grid value by index (helping [`BaseGrid`](crate::grid::BaseGrid)
78    /// access externally stored grid collections)
79    #[expect(unused_variables)]
80    fn get_grid_values(
81        &self,
82        grid: &BaseGrid,
83        index: &[usize],
84        grid_values: &mut [Coor4D],
85    ) -> usize {
86        0
87    }
88}
89
90/// Help context providers provide canonically named, built in coordinate adaptors
91#[rustfmt::skip]
92pub const BUILTIN_ADAPTORS: [(&str, &str); 8] = [
93    ("geo:in",  "adapt from=neuf_deg"),
94    ("geo:out", "adapt to=neuf_deg"  ),
95    ("gis:in",  "adapt from=enuf_deg"),
96    ("gis:out", "adapt to=enuf_deg"  ),
97    ("neu:in",  "adapt from=neuf"    ),
98    ("neu:out", "adapt to=neuf"      ),
99    ("enu:in",  "adapt from=enuf"    ),
100    ("enu:out", "adapt to=enuf"      ),
101];