use std::collections::HashMap;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RateLimitSnapshot {
pub requests_remaining: Option<u64>,
pub tokens_remaining: Option<u64>,
pub requests_reset_at: Option<DateTime<Utc>>,
pub tokens_reset_at: Option<DateTime<Utc>>,
#[serde(default)]
pub raw: HashMap<String, String>,
}
impl RateLimitSnapshot {
pub fn new() -> Self {
Self::default()
}
pub fn is_approaching_limit(&self, threshold: f64) -> bool {
let _ = threshold.clamp(0.0, 1.0);
let req_low = self
.requests_remaining
.is_some_and(|r| r <= LOW_REMAINING_FLOOR);
let tok_low = self
.tokens_remaining
.is_some_and(|t| t <= LOW_REMAINING_FLOOR);
req_low || tok_low
}
}
pub const LOW_REMAINING_FLOOR: u64 = 5;