#[allow(unused_imports)]
use super::*;
impl UserQuota {
pub fn has_space_for(&self, bytes: i64) -> bool {
self.used_bytes + bytes <= self.limit_bytes
}
pub fn usage_percent(&self) -> f64 {
if self.limit_bytes <= 0 {
return 0.0;
}
(self.used_bytes as f64 / self.limit_bytes as f64) * 100.0
}
pub fn is_over_warning_threshold(&self) -> bool {
self.usage_percent() >= self.warning_threshold_percent as f64
}
pub fn add_usage(&mut self, bytes: i64) -> Result<(), QuotaExceeded> {
if self.used_bytes + bytes > self.limit_bytes {
return Err(QuotaExceeded::ValidationFailed(
format!(
"Adding {} bytes would exceed quota limit ({}/{} bytes)",
bytes, self.used_bytes + bytes, self.limit_bytes
),
));
}
self.used_bytes += bytes;
self.metadata.updated_at = Some(Utc::now());
Ok(())
}
pub fn subtract_usage(&mut self, bytes: i64) {
self.used_bytes = (self.used_bytes - bytes).max(0);
self.metadata.updated_at = Some(Utc::now());
}
pub fn update_peak(&mut self) {
if self.used_bytes > self.peak_usage_bytes {
self.peak_usage_bytes = self.used_bytes;
self.peak_usage_at = Some(Utc::now());
}
}
pub fn remaining_bytes(&self) -> i64 {
(self.limit_bytes - self.used_bytes).max(0)
}
pub fn check_invariants(&self) -> Result<(), Vec<&'static str>> {
let mut errors = Vec::new();
if self.used_bytes > self.limit_bytes {
errors.push("used_bytes must not exceed limit_bytes");
}
if self.used_bytes < 0 {
errors.push("used_bytes must be non-negative");
}
if self.file_count < 0 {
errors.push("file_count must be non-negative");
}
if self.warning_threshold_percent < 0 || self.warning_threshold_percent > 100 {
errors.push("warning_threshold_percent must be between 0 and 100");
}
if self.limit_bytes <= 0 {
errors.push("limit_bytes must be positive");
}
if errors.is_empty() { Ok(()) } else { Err(errors) }
}
}