Skip to main content

utiles_core/
projection.rs

1//! Coordinate projection
2use std::fmt;
3
4use serde::{Deserialize, Serialize};
5
6use crate::UtilesCoreError;
7
8/// Projection enum
9#[derive(Debug, Serialize, Deserialize)]
10pub enum Projection {
11    /// Geographic projection (lat/lng coordinates)
12    Geographic,
13
14    /// Mercator projection (x/y coordinates)
15    Mercator,
16}
17
18impl TryFrom<String> for Projection {
19    type Error = UtilesCoreError;
20
21    fn try_from(value: String) -> Result<Self, Self::Error> {
22        match value.to_lowercase().as_str() {
23            "geographic" => Ok(Self::Geographic),
24            "mercator" => Ok(Self::Mercator),
25            _ => Err(UtilesCoreError::InvalidProjection(value)),
26        }
27    }
28}
29
30impl fmt::Display for Projection {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Geographic => write!(f, "geographic"),
34            Self::Mercator => write!(f, "mercator"),
35        }
36    }
37}