nlcalc 0.1.0

Plugin-based natural language calculator for Rust and the command line
Documentation
//! Explicit media and angle references; labels never supply missing indices.
use super::*;

impl Request<'_> {
    pub(super) fn refraction_clauses(&self, input: &str) -> Result<Option<String>, Error> {
        macro_rules! family {
            ($pattern:literal, $c:ident, $answer:expr) => {
                if let Some($c) = re!($pattern).captures(input) {
                    return $answer.map(Some);
                }
            };
        }
        family!(
            r"(?i)^for light entering [a-z]+, let n1\s*=\s*(.+?), n2\s*=\s*(.+?), and incident angle (.+?) from the normal; using n1 sin\(theta1\)\s*=\s*n2 sin\(theta2\), find theta2 in degrees$",
            c,
            self.snell_from_basis(&c[1], &c[2], &c[3], "normal")
        );
        family!(
            r"(?i)^a ([a-z]+)-([a-z]+) ray has incidence (.+?) measured from the (surface|normal); use n_([a-z]+)\s*=\s*(.+?) and n_([a-z]+)\s*=\s*(.+?) to find refraction from the normal if possible$",
            c,
            {
                let (n1, n2) = medium_indices(&c[1], &c[2], &c[5], &c[6], &c[7], &c[8])?;
                self.snell_from_basis_output(n1, n2, &c[3], &c[4], "conditional")
            }
        );
        family!(
            r"(?i)^for n[₁1]\s*=\s*(.+?) and n[₂2]\s*=\s*(.+?), calculate the transmitted angle at exactly the critical angle when incidence is measured from the normal$",
            c,
            {
                // Validate the critical domain without rounding an unused tiny angle.
                self.critical_ratio(&c[1], &c[2])?;
                Ok("90° in °".into())
            }
        );
        family!(
            r"(?i)^a ray goes from n\s*=\s*(.+?) into n\s*=\s*(.+?) with incidence (.+?) from the (surface|normal); determine refracted angle from the normal$",
            c,
            self.snell_from_basis(&c[1], &c[2], &c[3], &c[4])
        );
        family!(
            r"(?i)^a beam in material n\s*=\s*(.+?) reaches material n\s*=\s*(.+?) at (.+?) from the normal; compute the result or identify total internal reflection$",
            c,
            self.snell_answer(&c[1], &c[2], &c[3], "conditional")
        );
        family!(
            r"(?i)^a ray enters [a-z]+ from [a-z]+ at (.+?) (?:to|from) the (normal|surface); with indices (.+?) and (.+?), (?:find|calculate) the refracted angle from the normal$",
            c,
            // The written indices follow the incoming/outgoing Snell convention.
            self.snell_from_basis(&c[3], &c[4], &c[1], &c[2])
        );
        family!(
            r"(?i)^light passes from [a-z]+ n\s*=\s*(.+?) into [a-z]+ n\s*=\s*(.+?) at (.+?) measured from the normal; determine the snell-law outcome and the critical angle$",
            c,
            self.snell_answer(&c[1], &c[2], &c[3], "critical")
        );
        family!(
            r"(?i)^for a ray travelling from n\s*=\s*(.+?) into n\s*=\s*(.+?) at (.+?) from the normal, calculate the transmitted angle if possible$",
            c,
            self.snell_answer(&c[1], &c[2], &c[3], "conditional")
        );
        family!(
            r"(?i)^an ([a-z]+)-to-([a-z]+) ray has an incidence angle of (.+?) from the (surface|normal); use n_([a-z]+)\s*=\s*(.+?) and n_([a-z]+)\s*=\s*(.+?) to find the refracted angle from the normal$",
            c,
            {
                let (n1, n2) = medium_indices(&c[1], &c[2], &c[5], &c[6], &c[7], &c[8])?;
                self.snell_from_basis(n1, n2, &c[3], &c[4])
            }
        );
        family!(
            r"(?i)^at a boundary from n\s*=\s*(.+?) to n\s*=\s*(.+?), find the transmitted angle when incidence is exactly the critical angle, both angles measured from the normal$",
            c,
            {
                // Validate the critical-angle domain, then retain its defining identity.
                // Validate the critical domain without rounding an unused tiny angle.
                self.critical_ratio(&c[1], &c[2])?;
                Ok("90° in °".into())
            }
        );
        family!(
            r"(?i)^a beam travels from ([a-z]+) to ([a-z]+), with n_([a-z]+)\s*=\s*(.+?) and n_([a-z]+)\s*=\s*(.+?), and strikes at (.+?) from the normal; calculate refraction angle$",
            c,
            {
                let (n1, n2) = medium_indices(&c[1], &c[2], &c[3], &c[4], &c[5], &c[6])?;
                self.snell_answer(n1, n2, &c[7], "angle")
            }
        );
        family!(
            r"(?i)^a ray in material n\s*=\s*(.+?) meets material n\s*=\s*(.+?) at (.+?) from the normal; report whether it refracts and provide the angle if it does$",
            c,
            self.snell_answer(&c[1], &c[2], &c[3], "conditional")
        );
        Ok(None)
    }
}

fn medium_indices<'a>(
    incoming: &str,
    outgoing: &str,
    first_label: &str,
    first: &'a str,
    second_label: &str,
    second: &'a str,
) -> Result<(&'a str, &'a str), Error> {
    if incoming.eq_ignore_ascii_case(outgoing) {
        return Err(invalid());
    }
    if first_label.eq_ignore_ascii_case(incoming) && second_label.eq_ignore_ascii_case(outgoing) {
        Ok((first, second))
    } else if second_label.eq_ignore_ascii_case(incoming)
        && first_label.eq_ignore_ascii_case(outgoing)
    {
        Ok((second, first))
    } else {
        Err(invalid())
    }
}