From 7db8ac69c93f1d3ab3ce7ca1a00f9ef031abbf92 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Thu, 21 Nov 2024 06:58:06 -0800 Subject: [PATCH] refactor: remove code duplication --- src/router/link.rs | 12 +----------- src/views/templates.rs | 27 ++++++++++++--------------- templates/link.html | 2 +- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/router/link.rs b/src/router/link.rs index 33315e0..bfa6511 100644 --- a/src/router/link.rs +++ b/src/router/link.rs @@ -51,23 +51,13 @@ async fn link_delete( Ok(Html("".to_string())) } -#[inline] -pub fn get_remaining_text(downloads_remaining: u8) -> String { - let plural = if downloads_remaining > 1 { "s" } else { "" }; - format!( - "You have {} download{} remaining!", - downloads_remaining, plural - ) -} - async fn remaining( State(state): State, axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { let records = state.records.lock().await; if let Some(record) = records.get(&id) { - let downloads_remaining = record.downloads_remaining(); - Html(get_remaining_text(downloads_remaining)) + Html(crate::templates::get_downloads_remaining_text(record)) } else { Html("?".to_string()) } diff --git a/src/views/templates.rs b/src/views/templates.rs index 945c1eb..5487d18 100644 --- a/src/views/templates.rs +++ b/src/views/templates.rs @@ -1,6 +1,6 @@ use rinja_axum::Template; -use crate::{link::get_remaining_text, UploadRecord}; +use crate::UploadRecord; #[derive(Template)] #[template(path = "welcome.html")] @@ -14,6 +14,17 @@ impl WelcomeTemplate { } } +#[inline] +pub fn get_downloads_remaining_text(record: &UploadRecord) -> String { + let downloads_remaining = record.max_downloads - record.downloads; + let plural = if downloads_remaining > 1 { "s" } else { "" }; + + format!( + "You have {} download{} remaining!", + downloads_remaining, plural + ) +} + #[derive(Template)] #[template(path = "link.html")] pub struct DownloadLinkTemplate { @@ -21,13 +32,6 @@ pub struct DownloadLinkTemplate { pub record: UploadRecord, } -impl DownloadLinkTemplate { - fn get_downloads_remaining_text(record: &UploadRecord) -> String { - let downloads_remaining = record.max_downloads - record.downloads; - get_remaining_text(downloads_remaining) - } -} - #[derive(Template)] #[template(path = "link.html", block = "content")] pub struct DownloadLinkFragment { @@ -35,13 +39,6 @@ pub struct DownloadLinkFragment { pub record: UploadRecord, } -impl DownloadLinkFragment { - fn get_downloads_remaining_text(record: &UploadRecord) -> String { - let downloads_remaining = record.max_downloads - record.downloads; - get_remaining_text(downloads_remaining) - } -} - #[derive(Template)] #[template(path = "linklist.html")] pub struct LinkListTemplate { diff --git a/templates/link.html b/templates/link.html index 4f8d4eb..da7db4c 100644 --- a/templates/link.html +++ b/templates/link.html @@ -12,7 +12,7 @@ hx-get="/link/{{ id }}/remaining" hx-trigger="click from:#link delay:0.2s, every 10s" > - {{ Self::get_downloads_remaining_text(record) }} + {{ self::get_downloads_remaining_text(record) }}