use datumesh_core::lexical;
pub(crate) fn canonical(s: &str, date: bool) -> Option<String> {
if date && s.len() == 10 && lexical::check_date(s, "$").is_ok() {
return Some(format!("{s}T23:59:59.999999Z"));
}
let body = s.strip_suffix('Z')?;
let (base, frac) = match body.find('.') {
Some(i) if i + 1 < body.len() => (&body[..i], &body[i + 1..]),
Some(_) => return None,
None => (body, ""),
};
if frac.len() > 6 || !frac.bytes().all(|c| c.is_ascii_digit()) {
return None;
}
let out = format!("{base}.{frac:0<6}Z");
lexical::check_instant(&out, "$").ok()?;
Some(out)
}
pub(crate) fn canonical_or_same(s: &str, date: bool) -> String {
canonical(s, date).unwrap_or_else(|| s.to_string())
}
pub(crate) fn in_query(rest: &str) -> String {
let Some((path, query)) = rest.split_once('?') else {
return rest.to_string();
};
let ps: Vec<String> = query
.split('&')
.map(|p| match p.strip_prefix("asof=") {
Some(v) => format!("asof={}", canonical_or_same(v, true)),
None => p.to_string(),
})
.collect();
format!("{path}?{}", ps.join("&"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn forms() {
assert_eq!(canonical("2026-05-10T12:00:00Z", false).as_deref(), Some("2026-05-10T12:00:00.000000Z"));
assert_eq!(canonical("2026-05-10T12:00:00.5Z", false).as_deref(), Some("2026-05-10T12:00:00.500000Z"));
assert_eq!(canonical("2026-05-10T12:00:00.123456Z", false).as_deref(), Some("2026-05-10T12:00:00.123456Z"));
assert_eq!(canonical("2026-05-10", true).as_deref(), Some("2026-05-10T23:59:59.999999Z"));
assert_eq!(canonical("2026-05-10", false), None);
assert_eq!(canonical("2026-05-10T12:00:00.5+01:00", false), None);
assert_eq!(canonical("2026-05-10T12:00:00.1234567Z", false), None);
assert_eq!(canonical("2026-05-10T12:00:00.Z", false), None);
assert_eq!(canonical("2026-05-10T12:00Z", false), None);
assert_eq!(canonical("2026-05-1", true), None);
assert_eq!(in_query("a/b?x=1&asof=2026-05-10"), "a/b?x=1&asof=2026-05-10T23:59:59.999999Z");
}
}