mgrs-convert 0.1.0

Convert WGS84 latitude-longitude to and from UTM, UPS, and MGRS grid references, round-tripping exactly at every precision from 100 km to 1 um
Documentation
//! The validated digit-pair count.
//!
//! Mechanism M3: invalid precision is unrepresentable. Every function that takes
//! a precision takes this type, so the range check happens once, at the edge.

#![deny(clippy::arithmetic_side_effects)]

use crate::error::Error;

/// Digit pairs in an MGRS reference, 0 through 11. Cell size is 10^(5-p) metres:
/// 100 km at 0, 1 m at 5, 1 mm at 8, 1 um at 11.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Precision(u8);

impl Precision {
    /// The finest supported precision, 11 digit pairs, a 1 um cell.
    pub const MAX: Precision = Precision(11);

    /// Five digit pairs, a 1 m cell. The overwhelmingly common case.
    pub const ONE_METER: Precision = Precision(5);

    /// Validates a digit-pair count. Values above 11 are rejected.
    ///
    /// ```
    /// use mgrs_convert::{Error, Precision};
    /// assert_eq!(Precision::new(5), Ok(Precision::ONE_METER));
    /// assert_eq!(Precision::new(12), Err(Error::PrecisionOutOfRange { found: 12 }));
    /// ```
    pub fn new(digits: u8) -> Result<Precision, Error> {
        if digits <= 11 {
            Ok(Precision(digits))
        } else {
            Err(Error::PrecisionOutOfRange { found: digits })
        }
    }

    /// The digit-pair count, 0 through 11.
    pub fn get(self) -> u8 {
        self.0
    }
}