From fb876608a7165c1de421ba66b41f0e5b919460a2 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Sun, 12 Jan 2025 16:22:09 -0800 Subject: [PATCH] cleanup: remove unused state code --- src/db.rs | 15 +++++++++++++- src/state.rs | 46 +----------------------------------------- src/views/templates.rs | 2 +- 3 files changed, 16 insertions(+), 47 deletions(-) diff --git a/src/db.rs b/src/db.rs index b1bf659..f15dc71 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,6 @@ -use chrono::{DateTime, Utc}; +use chrono::{DateTime, Duration, Utc}; +#[derive(Debug)] pub struct CacheRecord { pub cache_name: String, pub uploaded: DateTime, @@ -9,6 +10,18 @@ pub struct CacheRecord { pub max_downloads: i32, } +impl CacheRecord { + pub fn can_be_downloaded(&self) -> bool { + let dur_since_upload = Utc::now().signed_duration_since(self.uploaded); + + dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads + } + + pub fn downloads_remaining(&self) -> i32 { + self.max_downloads - self.downloads + } +} + pub struct CacheRecordName { pub cache_name: String, } diff --git a/src/state.rs b/src/state.rs index b6d4400..a7783d2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,51 +1,7 @@ -use std::{ - path::{Path, PathBuf}, - str::FromStr, -}; +use std::str::FromStr; -use chrono::{DateTime, Duration, Utc}; -use serde::{Deserialize, Serialize}; use sqlx::{sqlite::SqliteConnectOptions, SqlitePool}; -#[allow(dead_code)] -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct UploadRecord { - pub uploaded: DateTime, - pub file: PathBuf, - pub downloads: u8, - pub max_downloads: u8, -} - -impl UploadRecord { - pub fn new(file: PathBuf) -> Self { - Self { - file, - ..Default::default() - } - } - - pub fn can_be_downloaded(&self) -> bool { - let dur_since_upload = Utc::now().signed_duration_since(self.uploaded); - - dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads - } - - pub fn downloads_remaining(&self) -> u8 { - self.max_downloads - self.downloads - } -} - -impl Default for UploadRecord { - fn default() -> Self { - Self { - uploaded: Utc::now(), - file: Path::new("").to_owned(), - downloads: 0, - max_downloads: 5, - } - } -} - #[derive(Clone)] pub struct AppState { pub pool: SqlitePool, diff --git a/src/views/templates.rs b/src/views/templates.rs index c6c94bf..d55e5ad 100644 --- a/src/views/templates.rs +++ b/src/views/templates.rs @@ -16,7 +16,7 @@ impl WelcomeTemplate { #[inline] pub fn get_downloads_remaining_text(record: &CacheRecord) -> String { - let downloads_remaining = record.max_downloads - record.downloads; + let downloads_remaining = record.downloads_remaining(); let plural = if downloads_remaining > 1 { "s" } else { "" }; format!(