pub(crate) fn strip_suffix<'a>(key: &'a str, lower: &str) -> Option<&'a str> {
let start = key.len().checked_sub(lower.len())?;
let tail = key.get(start..)?;
(tail == lower
|| (tail.eq_ignore_ascii_case(lower) && !tail.bytes().any(|b| b.is_ascii_lowercase())))
.then(|| &key[..start])
}
pub(crate) fn has_suffix_ci(key: &str, lower: &str) -> bool {
strip_suffix(key, lower).is_some()
}
pub(crate) fn currency_code<'a>(key: &'a str, unit: &str) -> Option<&'a str> {
let (_, code) = strip_suffix(key, unit)?.rsplit_once('_')?;
((3..=4).contains(&code.len()) && code.bytes().all(|b| b.is_ascii_alphabetic())).then_some(code)
}
pub(crate) const REGISTERED_SUFFIXES: &[(&str, &[&str])] = &[
(
"duration",
&["_ns", "_us", "_ms", "_s", "_minutes", "_hours", "_days"],
),
(
"timestamp",
&["_epoch_s", "_epoch_ms", "_epoch_ns", "_rfc3339"],
),
(
"strict_string",
&["_rfc3339_date", "_rfc3339_time", "_bcp47", "_utc_offset"],
),
("size", &["_bytes"]),
("percentage", &["_percent"]),
(
"currency",
&[
"_msats",
"_sats",
"_usd_cents",
"_eur_cents",
"_jpy",
"_{code}_cents",
"_{code}_micro",
],
),
("sensitive", &["_secret", "_url"]),
];
pub(crate) fn has_registered_suffix(key: &str) -> bool {
REGISTERED_SUFFIXES
.iter()
.flat_map(|(_, suffixes)| *suffixes)
.any(|suffix| match suffix.strip_prefix("_{code}") {
Some(unit) => currency_code(key, unit).is_some(),
None => has_suffix_ci(key, suffix),
})
}