nlcalc 0.1.0

Plugin-based natural language calculator for Rust and the command line
Documentation
use std::{collections::BTreeMap, sync::LazyLock};

pub(crate) struct City {
    pub coordinates: (f64, f64),
    pub timezone: &'static str,
}
pub(crate) fn get(name: &str) -> Option<&'static City> {
    static CITIES: LazyLock<BTreeMap<&str, City>> = LazyLock::new(|| {
        include_str!("../../data/cities.tsv")
            .lines()
            .map(|line| {
                let mut fields = line.split('\t');
                let name = fields.next().unwrap();
                let latitude = fields.next().unwrap().parse().unwrap();
                let longitude = fields.next().unwrap().parse().unwrap();
                let timezone = fields.next().unwrap();
                (
                    name,
                    City {
                        coordinates: (latitude, longitude),
                        timezone,
                    },
                )
            })
            .collect()
    });
    if matches!(name, "the bronx" | "the hague" | "nur-sultan") {
        return None;
    }
    CITIES.get(name)
}

// Vincenty's inverse solution on WGS84, with bounded iteration.
pub(crate) fn distance(a: (f64, f64), b: (f64, f64)) -> f64 {
    let f = 1.0 / 298.257223563;
    let major = 6378137.0;
    let minor = (1.0 - f) * major;
    let (u1, u2) = (
        ((1.0 - f) * a.0.to_radians().tan()).atan(),
        ((1.0 - f) * b.0.to_radians().tan()).atan(),
    );
    let l = (b.1 - a.1).to_radians();
    let mut lambda = l;
    let (mut sigma, mut sin_sigma, mut cos_sigma, mut cos_sq_alpha, mut cos2m) =
        (0.0, 0.0, 0.0, 0.0, 0.0);
    for _ in 0..200 {
        let (sin_l, cos_l) = lambda.sin_cos();
        sin_sigma = (u2.cos() * sin_l).hypot(u1.cos() * u2.sin() - u1.sin() * u2.cos() * cos_l);
        if sin_sigma == 0.0 {
            return 0.0;
        }
        cos_sigma = u1.sin() * u2.sin() + u1.cos() * u2.cos() * cos_l;
        sigma = sin_sigma.atan2(cos_sigma);
        let sin_alpha = u1.cos() * u2.cos() * sin_l / sin_sigma;
        cos_sq_alpha = 1.0 - sin_alpha * sin_alpha;
        cos2m = if cos_sq_alpha == 0.0 {
            0.0
        } else {
            cos_sigma - 2.0 * u1.sin() * u2.sin() / cos_sq_alpha
        };
        let c = f / 16.0 * cos_sq_alpha * (4.0 + f * (4.0 - 3.0 * cos_sq_alpha));
        let previous = lambda;
        lambda = l
            + (1.0 - c)
                * f
                * sin_alpha
                * (sigma + c * sin_sigma * (cos2m + c * cos_sigma * (-1.0 + 2.0 * cos2m * cos2m)));
        if (lambda - previous).abs() < 1e-13 {
            break;
        }
    }
    let u = cos_sq_alpha * (major * major - minor * minor) / (minor * minor);
    let aa = 1.0 + u / 16384.0 * (4096.0 + u * (-768.0 + u * (320.0 - 175.0 * u)));
    let bb = u / 1024.0 * (256.0 + u * (-128.0 + u * (74.0 - 47.0 * u)));
    let delta = bb
        * sin_sigma
        * (cos2m
            + bb / 4.0
                * (cos_sigma * (-1.0 + 2.0 * cos2m * cos2m)
                    - bb / 6.0
                        * cos2m
                        * (-3.0 + 4.0 * sin_sigma * sin_sigma)
                        * (-3.0 + 4.0 * cos2m * cos2m)));
    minor * aa * (sigma - delta)
}