use crate::{plugins::units::Units, re};
pub(super) fn rewrite(s: &str, _units: &Units) -> String {
let normalized = s
.replace('’', "'")
.replace("working days", "workdays")
.replace("working day", "workday");
let normalized = re!(r"\b(?:monday|tuesday|wednesday|thursday|friday|saturday|sunday), (\d)")
.replace_all(&normalized, "$1");
let s = normalized.as_ref();
if let Some(c) = re!(r"^(?:date of|date of the|the date of) (.+)$").captures(s) {
return c[1].to_string();
}
if let Some(c) = re!(r"^of (.+ day \d{4})$").captures(s) {
return c[1].to_string();
}
if let Some(c) = re!(r"^(.+?) holiday in (\d{4})$").captures(s) {
return format!("{} {}", &c[1], &c[2]);
}
if let Some(c) = re!(r"^(.+?) in (\d{4})$").captures(s)
&& c[1].contains("labour day")
{
return format!("{} {}", &c[1], &c[2]);
}
if let Some(c) = re!(r"^(?:what was the date|what date was) (.+) ago(?: today)?$").captures(s) {
return format!("{} before today", &c[1]);
}
if let Some(c) = re!(r"^(?:what (?:is )?(?:the )?)?exact date (.+)$").captures(s) {
return c[1].to_string();
}
if let Some(c) = re!(r"^what weekday (?:is|was|will it be) (.+)$").captures(s) {
return format!("day of week {}", &c[1]);
}
if let Some(c) = re!(r"^weekday of (.+)$").captures(s) {
return format!("day of week {}", &c[1]);
}
if s == "how many weekdays are in a week" {
return "weekdays in week".to_string();
}
for suffix in [
", treating weekdays as business days and ignoring holidays",
", with monday to friday as business days and no holiday exclusions",
" if saturday and sunday are non-working",
] {
if let Some(base) = s.strip_suffix(suffix) {
return base.to_string();
}
}
if let Some(c) = re!(r"^how many days between (?:the )?first day of leap year (\d{4}) and its last day, inclusive$").captures(s) {
return format!("days from 1 january {} through 31 december {}", &c[1], &c[1]);
}
if let Some(c) = re!(r"^if (.+) is day (zero|one|0|1), what date is day (\d+)$").captures(s) {
return format!(
"{} + ({} - {}) days",
&c[1],
&c[3],
match &c[2] {
"zero" => "0",
"one" => "1",
n => n,
}
);
}
if let Some(c) = re!(r"^what date was (.+)$").captures(s) {
return c[1].to_string();
}
if let Some(c) = re!(r"^how many days after (.+) is (.+)$").captures(s) {
return format!("days between {} and {}", &c[1], &c[2]);
}
if let Some(c) = re!(r"^how many days until (?:the )?(next .+) from (.+)$").captures(s) {
return format!("days between {} and ({} after {})", &c[2], &c[1], &c[2]);
}
if let Some(base) = s
.strip_suffix(", inclusive")
.or_else(|| s.strip_suffix(", counting both dates"))
{
return if base.contains(" through ") {
base.to_string()
} else {
base.replacen(" to ", " through ", 1)
.replacen(" and ", " through ", 1)
};
}
if let Some(base) = s.strip_suffix(", not counting the start date") {
return base.to_string();
}
if let Some(c) =
re!(r"^(?:count (?:the )?|how many )(.+?) between (.+) and (.+), excluding both endpoints$")
.captures(s)
{
return format!(
"{} between ({} + 1 day) and {}",
c[1].replace("weekdays", "workdays"),
&c[2],
&c[3]
);
}
if let Some(c) =
re!(r"^(?:count (?:the )?|how many )(.+?) (?:are (?:there )?)?(between|from|until) (.+)$")
.captures(s)
{
if &c[2] == "from" && c[3].contains(':') {
return format!("{} in {}", &c[1], &c[3]);
}
return format!(
"{} {} {}",
c[1].trim_start_matches("elapsed ")
.trim_end_matches(" are")
.replace("weeks and days", "duration")
.replace("weekdays", "workdays"),
&c[2],
&c[3]
);
}
if let Some(c) = re!(r"^how many (days|weeks|months|years|weekdays|workdays|business days|mondays|tuesdays|wednesdays|thursdays|fridays|saturdays|sundays) (?:are (?:there )?)?in (.+)$").captures(s)
&& (re!(r"^(?:the )?(?:year )?\d{4}(?: calendar year)?$").is_match(&c[2])
|| re!(r"^(?:january|february|march|april|may|june|july|august|september|october|november|december)(?: \d{4})?$").is_match(&c[2]))
{
let period = c[2].trim_start_matches("the ").trim_start_matches("year ").trim_end_matches(" calendar year");
return format!("{} in {period}", &c[1]);
}
if let Some(c) = re!(r"^how many weeks and days (?:are )?between (.+) and (.+)$").captures(s) {
return format!("{} to {}", &c[1], &c[2]);
}
if let Some(c) = re!(r"^is (\d{4}) a leap year$").captures(s) {
return format!("leap year {}", &c[1]);
}
if let Some(c) =
re!(r"^(.+) calendar (months?|years?|days?|weeks?) (after|before) (.+)$").captures(s)
{
return format!("{} {} {} {}", &c[1], &c[2], &c[3], &c[4]);
}
if let Some(c) = re!(r"^what number occurs most in (.+)$").captures(s) {
return format!("mode of {}", &c[1]);
}
if let Some(c) = re!(r"^how many (.+?) (?:are (?:there )?in|in|for) (.+)$").captures(s) {
return format!("convert {} to {}", &c[2], &c[1]);
}
if let Some(c) = re!(r"^how many (.+?) (?:are there )?(between|from|until) (.+)$").captures(s) {
if &c[2] == "from" && c[3].contains(':') {
return format!("{} in {}", &c[1], &c[3]);
}
return format!(
"{} {} {}",
c[1].trim_start_matches("elapsed ")
.replace("weekdays", "workdays"),
&c[2],
&c[3]
);
}
if let Some(c) = re!(r"^what weekday is (.+)$").captures(s) {
return format!("day of week {}", &c[1]);
}
if let Some(c) = re!(r"^(.+) in the us$").captures(s) {
return c[1].to_string();
}
s.to_string()
}