fel-core 0.3.0

Formspec Expression Language — parser, evaluator, and dependency analysis
Documentation
#![allow(clippy::missing_docs_in_private_items)]
use rust_decimal::Decimal;
use rust_decimal::prelude::*;

use crate::ast::*;
use crate::error::Diagnostic;
use crate::iso_duration::{IsoDurationParse, parse_iso8601_duration};
use crate::types::*;

use super::super::core::Evaluator;
use super::super::util::{dec, parse_time_str};

enum DateOperand {
    Null,
    Ok(Date),
    Invalid,
}

impl<'a> Evaluator<'a> {
    // ── Date helpers ────────────────────────────────────────────

    pub(in crate::evaluator) fn fn_today(&mut self) -> Value {
        match self.env.current_date() {
            Ok(d) => Value::Date(d),
            Err(err) => {
                self.diagnostics
                    .push(Diagnostic::missing_timezone_context("today", err));
                Value::Null
            }
        }
    }

    pub(in crate::evaluator) fn fn_now(&mut self) -> Value {
        match self.env.current_datetime() {
            Ok(d) => Value::Date(d),
            Err(err) => {
                self.diagnostics
                    .push(Diagnostic::missing_timezone_context("now", err));
                Value::Null
            }
        }
    }

    pub(in crate::evaluator) fn fn_date_part(
        &mut self,
        args: &[Expr],
        fn_name: &str,
        f: fn(&Date) -> Decimal,
    ) -> Value {
        match self.eval_date_operand(args, 0, fn_name) {
            DateOperand::Null => Value::Null,
            DateOperand::Ok(d) => Value::Number(f(&d)),
            DateOperand::Invalid => Value::Null,
        }
    }

    pub(in crate::evaluator) fn fn_time_part(&mut self, args: &[Expr], idx: usize) -> Value {
        let s = match self.eval_arg(args, 0) {
            Value::String(s) => s,
            Value::Null => return Value::Null,
            _ => return Value::Null,
        };
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 3 {
            self.diag("invalid time string");
            return Value::Null;
        }
        match parts.get(idx).and_then(|p| p.parse::<Decimal>().ok()) {
            Some(n) => Value::Number(n),
            None => Value::Null,
        }
    }

    pub(in crate::evaluator) fn fn_time(&mut self, args: &[Expr]) -> Value {
        let h = match self.eval_arg(args, 0) {
            Value::Number(n) => n.to_i64().unwrap_or(0),
            _ => return Value::Null,
        };
        let m = match self.eval_arg(args, 1) {
            Value::Number(n) => n.to_i64().unwrap_or(0),
            _ => return Value::Null,
        };
        let s = match self.eval_arg(args, 2) {
            Value::Number(n) => n.to_i64().unwrap_or(0),
            _ => return Value::Null,
        };
        Value::String(format!("{h:02}:{m:02}:{s:02}"))
    }

    pub(in crate::evaluator) fn fn_time_diff(&mut self, args: &[Expr]) -> Value {
        let t1 = match self.eval_arg(args, 0) {
            Value::String(s) => s,
            _ => return Value::Null,
        };
        let t2 = match self.eval_arg(args, 1) {
            Value::String(s) => s,
            _ => return Value::Null,
        };
        match (parse_time_str(&t1), parse_time_str(&t2)) {
            (Some((h1, m1, s1)), Some((h2, m2, s2))) => {
                Value::Number(dec((h1 * 3600 + m1 * 60 + s1) - (h2 * 3600 + m2 * 60 + s2)))
            }
            _ => {
                self.diag("timeDiff: invalid time strings");
                Value::Null
            }
        }
    }

    pub(in crate::evaluator) fn fn_duration(&mut self, args: &[Expr]) -> Value {
        let v = self.eval_arg(args, 0);
        match v {
            Value::String(s) => match parse_iso8601_duration(&s) {
                IsoDurationParse::Milliseconds(ms) => {
                    // Warn when the date component contains year (Y) or month (M before T).
                    // These use nominal lengths (365d, 30d), not calendar arithmetic.
                    // `W` is intentionally excluded: 1W = 7d exactly is not a nominal-length lie.
                    let has_year = s.contains('Y');
                    let has_date_month = {
                        // 'M' before any 'T' is a month designator; after 'T' it is minutes.
                        let t_pos = s.find('T').unwrap_or(s.len());
                        s[..t_pos].contains('M')
                    };
                    if has_year || has_date_month {
                        self.diagnostics.push(Diagnostic::warning(
                            "duration: year/month components use nominal lengths (365d, 30d); \
                             use dateAdd(\"years\"|\"months\", ...) for calendar arithmetic",
                        ));
                    }
                    Value::Number(dec(ms))
                }
                IsoDurationParse::Invalid => {
                    self.diag("duration: invalid ISO 8601 duration string");
                    Value::Null
                }
                IsoDurationParse::OutOfRange => {
                    self.diag("duration: duration exceeds representable range (milliseconds)");
                    Value::Null
                }
            },
            Value::Null => Value::Null,
            _ => {
                self.diag(format!("duration: expected string, got {}", v.type_name()));
                Value::Null
            }
        }
    }

    pub(in crate::evaluator) fn fn_date_diff(&mut self, args: &[Expr]) -> Value {
        let d1 = match self.eval_date_operand(args, 0, "dateDiff") {
            DateOperand::Ok(d) => d,
            DateOperand::Null | DateOperand::Invalid => return Value::Null,
        };
        let d2 = match self.eval_date_operand(args, 1, "dateDiff") {
            DateOperand::Ok(d) => d,
            DateOperand::Null | DateOperand::Invalid => return Value::Null,
        };
        let unit = match self.eval_arg(args, 2) {
            Value::String(s) => s,
            _ => return Value::Null,
        };
        let result = match unit.as_str() {
            "days" => d1.ordinal_days() - d2.ordinal_days(),
            "months" => {
                (d1.year() as i64 * 12 + d1.month() as i64)
                    - (d2.year() as i64 * 12 + d2.month() as i64)
            }
            "years" => d1.year() as i64 - d2.year() as i64,
            _ => {
                self.diag(format!("dateDiff: unknown unit '{unit}'"));
                return Value::Null;
            }
        };
        Value::Number(dec(result))
    }

    pub(in crate::evaluator) fn fn_date_add(&mut self, args: &[Expr]) -> Value {
        let d = match self.eval_date_operand(args, 0, "dateAdd") {
            DateOperand::Ok(d) => d,
            DateOperand::Null | DateOperand::Invalid => return Value::Null,
        };
        let n = match self.eval_arg(args, 1) {
            Value::Number(n) => n.to_i64().unwrap_or(0),
            _ => return Value::Null,
        };
        let unit = match self.eval_arg(args, 2) {
            Value::String(s) => s,
            _ => return Value::Null,
        };
        match unit.as_str() {
            "days" => Value::Date(date_add_days(&d, n)),
            "months" => {
                let total = d.year() as i64 * 12 + (d.month() as i64 - 1) + n;
                let new_year = (total.div_euclid(12)) as i32;
                let new_month = (total.rem_euclid(12) + 1) as u32;
                let max_day = days_in_month(new_year, new_month);
                let new_day = d.day().min(max_day);
                Value::Date(Date::Date {
                    year: new_year,
                    month: new_month,
                    day: new_day,
                })
            }
            "years" => {
                let new_year = d.year() + n as i32;
                let max_day = days_in_month(new_year, d.month());
                let new_day = d.day().min(max_day);
                Value::Date(Date::Date {
                    year: new_year,
                    month: d.month(),
                    day: new_day,
                })
            }
            _ => {
                self.diag(format!("dateAdd: unknown unit '{unit}'"));
                Value::Null
            }
        }
    }

    /// Try to coerce an ISO date/datetime string to Date. Returns None on failure.
    pub(in crate::evaluator) fn coerce_string_to_date(&self, s: &str) -> Option<Date> {
        parse_date_literal(&format!("@{s}")).or_else(|| parse_datetime_literal(&format!("@{s}")))
    }

    fn eval_date_operand(&mut self, args: &[Expr], idx: usize, fn_name: &str) -> DateOperand {
        match self.eval_arg(args, idx) {
            Value::Null => DateOperand::Null,
            Value::Date(d) => DateOperand::Ok(d),
            Value::String(s) => match self.coerce_string_to_date(&s) {
                Some(d) => DateOperand::Ok(d),
                None => {
                    self.diag(format!("{fn_name}: invalid date string"));
                    DateOperand::Invalid
                }
            },
            ref other => {
                self.reject_expected_type(fn_name, "date or string", other);
                DateOperand::Invalid
            }
        }
    }
}