1use crate::json::{Value, number};
4
5#[derive(Clone, Debug, PartialEq)]
10pub enum Ttl {
11 None,
13 Forever,
15 Milliseconds(f64),
17 String(String),
20}
21
22pub const DEFAULT_TTL_MILLISECONDS: f64 = 5.0 * 60.0 * 1000.0;
24pub const MAX_TTL_MILLISECONDS: f64 = 10.0 * 60.0 * 1000.0;
26
27fn multiplier(unit: char) -> Option<f64> {
28 Some(match unit {
29 's' => 1000.0,
30 'm' => 60.0 * 1000.0,
31 'h' => 60.0 * 60.0 * 1000.0,
32 'd' => 24.0 * 60.0 * 60.0 * 1000.0,
33 'y' => 365.0 * 24.0 * 60.0 * 60.0 * 1000.0,
34 _ => return None,
35 })
36}
37
38impl Ttl {
39 pub const DEFAULT: &str = "5m";
41
42 pub const DEFAULT_PRELOAD: Ttl = Ttl::None;
46
47 pub fn seconds(n: f64) -> Ttl {
49 Ttl::String(number::format(n) + "s")
50 }
51
52 pub fn minutes(n: f64) -> Ttl {
54 Ttl::String(number::format(n) + "m")
55 }
56
57 pub fn hours(n: f64) -> Ttl {
59 Ttl::String(number::format(n) + "h")
60 }
61
62 pub fn days(n: f64) -> Ttl {
64 Ttl::String(number::format(n) + "d")
65 }
66
67 pub fn from_json(v: &Value) -> Option<Ttl> {
69 match v {
70 Value::Number(n) => Some(Ttl::Milliseconds(*n)),
71 Value::String(s) => Some(Ttl::from(s.as_str())),
72 _ => None,
73 }
74 }
75
76 pub fn to_json(&self) -> Value {
78 match self {
79 Ttl::None => "none".into(),
80 Ttl::Forever => "forever".into(),
81 Ttl::Milliseconds(n) => Value::Number(*n),
82 Ttl::String(s) => s.as_str().into(),
83 }
84 }
85
86 pub fn parsed(&self) -> f64 {
88 match self {
89 Ttl::None => 0.0,
90 Ttl::Forever => -1.0,
91 Ttl::Milliseconds(n) if n.is_nan() => 0.0,
92 Ttl::Milliseconds(n) if !n.is_finite() || *n < 0.0 => -1.0,
93 Ttl::Milliseconds(n) => *n,
94 Ttl::String(s) if s == "none" => 0.0,
95 Ttl::String(s) if s == "forever" => -1.0,
96 Ttl::String(s) => {
97 let Some(unit) = s.chars().last() else {
98 return f64::NAN;
99 };
100 match multiplier(unit) {
101 Some(m) => number::parse(&s[..s.len() - unit.len_utf8()]) * m,
102 None => f64::NAN,
103 }
104 }
105 }
106 }
107
108 pub fn compare(a: &Ttl, b: &Ttl) -> f64 {
110 let (ap, bp) = (a.parsed(), b.parsed());
111 match (ap == -1.0, bp == -1.0) {
112 (true, false) => 1.0,
113 (false, true) => -1.0,
114 _ => ap - bp,
115 }
116 }
117
118 pub fn normalized(&self) -> Ttl {
121 let Ttl::Milliseconds(ttl) = *self else {
122 return self.clone();
123 };
124 if ttl < 0.0 {
125 return Ttl::Forever;
126 }
127 if ttl == 0.0 {
128 return Ttl::None;
129 }
130 let plain = number::format(ttl);
131 let mut shortest = plain.clone();
132 for unit in ['y', 'd', 'h', 'm', 's'] {
133 let candidate =
134 number::format(ttl / multiplier(unit).expect("unit")) + &unit.to_string();
135 if candidate.encode_utf16().count() < shortest.encode_utf16().count() {
136 shortest = candidate;
137 }
138 }
139 if shortest.len() < plain.len() {
140 Ttl::String(shortest)
141 } else {
142 self.clone()
143 }
144 }
145
146 pub fn clamped(&self) -> f64 {
149 let p = self.parsed();
150 if p == -1.0 || p > MAX_TTL_MILLISECONDS {
151 MAX_TTL_MILLISECONDS
152 } else {
153 p
154 }
155 }
156}
157
158impl From<&str> for Ttl {
159 fn from(s: &str) -> Ttl {
160 match s {
161 "none" => Ttl::None,
162 "forever" => Ttl::Forever,
163 s => Ttl::String(s.to_owned()),
164 }
165 }
166}
167
168impl Default for Ttl {
169 fn default() -> Ttl {
170 Ttl::String(Ttl::DEFAULT.to_owned())
171 }
172}