use crate::json::{Value, number};
#[derive(Clone, Debug, PartialEq)]
pub enum Ttl {
None,
Forever,
Milliseconds(f64),
String(String),
}
pub const DEFAULT_TTL_MILLISECONDS: f64 = 5.0 * 60.0 * 1000.0;
pub const MAX_TTL_MILLISECONDS: f64 = 10.0 * 60.0 * 1000.0;
fn multiplier(unit: char) -> Option<f64> {
Some(match unit {
's' => 1000.0,
'm' => 60.0 * 1000.0,
'h' => 60.0 * 60.0 * 1000.0,
'd' => 24.0 * 60.0 * 60.0 * 1000.0,
'y' => 365.0 * 24.0 * 60.0 * 60.0 * 1000.0,
_ => return None,
})
}
impl Ttl {
pub const DEFAULT: &str = "5m";
pub fn seconds(n: f64) -> Ttl {
Ttl::String(number::format(n) + "s")
}
pub fn minutes(n: f64) -> Ttl {
Ttl::String(number::format(n) + "m")
}
pub fn hours(n: f64) -> Ttl {
Ttl::String(number::format(n) + "h")
}
pub fn days(n: f64) -> Ttl {
Ttl::String(number::format(n) + "d")
}
pub fn from_json(v: &Value) -> Option<Ttl> {
match v {
Value::Number(n) => Some(Ttl::Milliseconds(*n)),
Value::String(s) => Some(Ttl::from(s.as_str())),
_ => None,
}
}
pub fn to_json(&self) -> Value {
match self {
Ttl::None => "none".into(),
Ttl::Forever => "forever".into(),
Ttl::Milliseconds(n) => Value::Number(*n),
Ttl::String(s) => s.as_str().into(),
}
}
pub fn parsed(&self) -> f64 {
match self {
Ttl::None => 0.0,
Ttl::Forever => -1.0,
Ttl::Milliseconds(n) if n.is_nan() => 0.0,
Ttl::Milliseconds(n) if !n.is_finite() || *n < 0.0 => -1.0,
Ttl::Milliseconds(n) => *n,
Ttl::String(s) if s == "none" => 0.0,
Ttl::String(s) if s == "forever" => -1.0,
Ttl::String(s) => {
let Some(unit) = s.chars().last() else {
return f64::NAN;
};
match multiplier(unit) {
Some(m) => number::parse(&s[..s.len() - unit.len_utf8()]) * m,
None => f64::NAN,
}
}
}
}
pub fn compare(a: &Ttl, b: &Ttl) -> f64 {
let (ap, bp) = (a.parsed(), b.parsed());
match (ap == -1.0, bp == -1.0) {
(true, false) => 1.0,
(false, true) => -1.0,
_ => ap - bp,
}
}
pub fn normalized(&self) -> Ttl {
let Ttl::Milliseconds(ttl) = *self else {
return self.clone();
};
if ttl < 0.0 {
return Ttl::Forever;
}
if ttl == 0.0 {
return Ttl::None;
}
let plain = number::format(ttl);
let mut shortest = plain.clone();
for unit in ['y', 'd', 'h', 'm', 's'] {
let candidate =
number::format(ttl / multiplier(unit).expect("unit")) + &unit.to_string();
if candidate.encode_utf16().count() < shortest.encode_utf16().count() {
shortest = candidate;
}
}
if shortest.len() < plain.len() {
Ttl::String(shortest)
} else {
self.clone()
}
}
pub fn clamped(&self) -> f64 {
let p = self.parsed();
if p == -1.0 || p > MAX_TTL_MILLISECONDS {
MAX_TTL_MILLISECONDS
} else {
p
}
}
}
impl From<&str> for Ttl {
fn from(s: &str) -> Ttl {
match s {
"none" => Ttl::None,
"forever" => Ttl::Forever,
s => Ttl::String(s.to_owned()),
}
}
}
impl Default for Ttl {
fn default() -> Ttl {
Ttl::String(Ttl::DEFAULT.to_owned())
}
}