oxiproj_engine/
context.rs1use std::collections::HashMap;
6
7use oxiproj_core::ProjError;
8use oxiproj_transformations::GridRegistry;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum TmercAlgo {
14 Auto,
17 EvendenSnyder,
19 PoderEngsager,
21}
22
23#[derive(Debug, Clone)]
28pub struct Context {
29 pub last_error: Option<ProjError>,
31 pub tmerc_algo: TmercAlgo,
33 grid_data: HashMap<String, Vec<u8>>,
35}
36
37impl Context {
38 pub fn new() -> Context {
40 Context {
41 last_error: None,
42 tmerc_algo: TmercAlgo::Auto,
43 grid_data: HashMap::new(),
44 }
45 }
46
47 pub fn register_grid(&mut self, name: impl Into<String>, data: Vec<u8>) {
53 self.grid_data.insert(name.into(), data);
54 }
55}
56
57impl GridRegistry for Context {
58 fn get_grid(&self, name: &str) -> Option<&[u8]> {
59 self.grid_data.get(name).map(|v| v.as_slice())
60 }
61}
62
63impl Default for Context {
64 fn default() -> Self {
65 Context::new()
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn new_has_no_error() {
75 let ctx = Context::new();
76 assert!(ctx.last_error.is_none());
77 assert_eq!(ctx.tmerc_algo, TmercAlgo::Auto);
78 }
79
80 #[test]
81 fn default_matches_new() {
82 let a = Context::default();
83 let b = Context::new();
84 assert!(a.last_error.is_none() && b.last_error.is_none());
85 assert_eq!(a.tmerc_algo, b.tmerc_algo);
86 }
87
88 #[test]
89 fn register_and_get_grid() {
90 let mut ctx = Context::new();
91 ctx.register_grid("test.gsb", vec![1u8, 2, 3]);
92 let data = oxiproj_transformations::GridRegistry::get_grid(&ctx, "test.gsb");
93 assert_eq!(data, Some([1u8, 2, 3].as_ref()));
94 assert!(oxiproj_transformations::GridRegistry::get_grid(&ctx, "missing").is_none());
95 }
96}