use std::collections::HashMap;
use oxiproj_core::ProjError;
use oxiproj_transformations::GridRegistry;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TmercAlgo {
Auto,
EvendenSnyder,
PoderEngsager,
}
#[derive(Debug, Clone)]
pub struct Context {
pub last_error: Option<ProjError>,
pub tmerc_algo: TmercAlgo,
grid_data: HashMap<String, Vec<u8>>,
}
impl Context {
pub fn new() -> Context {
Context {
last_error: None,
tmerc_algo: TmercAlgo::Auto,
grid_data: HashMap::new(),
}
}
pub fn register_grid(&mut self, name: impl Into<String>, data: Vec<u8>) {
self.grid_data.insert(name.into(), data);
}
}
impl GridRegistry for Context {
fn get_grid(&self, name: &str) -> Option<&[u8]> {
self.grid_data.get(name).map(|v| v.as_slice())
}
}
impl Default for Context {
fn default() -> Self {
Context::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_has_no_error() {
let ctx = Context::new();
assert!(ctx.last_error.is_none());
assert_eq!(ctx.tmerc_algo, TmercAlgo::Auto);
}
#[test]
fn default_matches_new() {
let a = Context::default();
let b = Context::new();
assert!(a.last_error.is_none() && b.last_error.is_none());
assert_eq!(a.tmerc_algo, b.tmerc_algo);
}
#[test]
fn register_and_get_grid() {
let mut ctx = Context::new();
ctx.register_grid("test.gsb", vec![1u8, 2, 3]);
let data = oxiproj_transformations::GridRegistry::get_grid(&ctx, "test.gsb");
assert_eq!(data, Some([1u8, 2, 3].as_ref()));
assert!(oxiproj_transformations::GridRegistry::get_grid(&ctx, "missing").is_none());
}
}