use super::funejson::Value; /// The time left until `until`, as a number and as a timer's text. A moment /// is reached at `until` itself, as a JWT's exp is (RFC 7519 4.1.4), so /// `now == until` is expired with nothing left. pub fn countdown(until: i64, now: i64) -> Countdown { let seconds = if until > now { until - now } else { 0 }; let h = seconds / 3600; let m = (seconds % 3600) / 60; let s = seconds % 60; let text = if h > 0 { format!("{}:{:02}:{:02}", h, m, s) } else { format!("{}:{:02}", m, s) }; Countdown { expired: seconds == 0, seconds, text } } pub fn countdown_to_value(c: &Countdown) -> Value { Value::obj(vec![ ("expired", Value::Bool(c.expired)), ("seconds", Value::Int(c.seconds)), ("text", Value::str(&c.text)), ]) } fn whole(value: &Value, message: &str) -> i64 { match value { Value::Int(i) => *i, _ => panic!("{}", message), } } pub fn fune_vector(args: &[Value]) -> Value { let until = whole(&args[0], "until must be a whole number of seconds"); let now = whole(&args[1], "now must be a whole number of seconds"); countdown_to_value(&countdown(until, now)) }