plavno 0.3.1

A lightweight terminal music player for Linux, macOS and Windows with service waves and library shuffle
use std::{
    fmt,
    time::{Duration, Instant},
};

#[derive(Debug)]
pub struct RateLimited {
    started: Instant,
    wait: Duration,
}

impl RateLimited {
    pub fn new(wait: Duration) -> Self {
        Self {
            started: Instant::now(),
            wait,
        }
    }
    pub fn active(&self) -> bool {
        self.started.elapsed() < self.wait
    }
}

impl fmt::Display for RateLimited {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Yandex rate limit (HTTP 429); retry in {}s", {
            let wait = self.wait.saturating_sub(self.started.elapsed());
            wait.as_secs()
                .saturating_add(u64::from(wait.subsec_nanos() != 0))
        })
    }
}

impl std::error::Error for RateLimited {}