use super::funejson::Value; use super::dates_add_days::add_days; /// The last free day and the first charge date of a trial of `trial_days` days /// starting on `start_date`. The start day counts as day one, so the last free /// day is start + trial_days - 1 and the charge is the day after it. /// /// # Panics /// Panics if `trial_days` is negative or the start date is not a real ISO date. pub fn trial_end(start_date: &str, trial_days: i64) -> TrialPeriod { if trial_days < 0 { panic!("trial days must not be negative, received {}", trial_days); } // add_days validates the start date even when there is no trial, so a // malformed date never slips through as the "first charge date". let first_charge_date = add_days(start_date, trial_days); let last_trial_day = if trial_days == 0 { None } else { Some(add_days(start_date, trial_days - 1)) }; TrialPeriod { last_trial_day, first_charge_date, } } pub fn trial_period_to_value(period: &TrialPeriod) -> Value { Value::obj(vec![ ( "lastTrialDay", match &period.last_trial_day { Some(day) => Value::str(day), None => Value::Null, }, ), ("firstChargeDate", Value::str(&period.first_charge_date)), ]) } pub fn fune_vector(args: &[Value]) -> Value { trial_period_to_value(&trial_end(args[0].as_str(), args[1].as_i64())) }