From 42cc05dbab8ee2f21fffdf35f9bd727e3ea9c17d Mon Sep 17 00:00:00 2001 From: SkyfallWasTaken Date: Mon, 1 Jul 2024 19:13:11 +0100 Subject: [PATCH] Refactor format.rs for better code organization and naming consistency --- src/format.rs | 22 +++++++++++----------- src/lib.rs | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/format.rs b/src/format.rs index 8b69dc0..0559ffe 100644 --- a/src/format.rs +++ b/src/format.rs @@ -2,7 +2,7 @@ use indoc::formatdoc; use crate::items::ShopItem; -pub fn diff_items(old: ShopItem, new: ShopItem) -> Option { +pub fn format_item_diff(old: &ShopItem, new: &ShopItem) -> Option { if old == new { // The items are the exact same return None; @@ -19,8 +19,8 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option { if old.description != new.description { result.push(format!( "*Description:* {} → {}", - old.description.unwrap_or("_not set_".into()), - new.description.unwrap_or("_not set_".into()) + old.description.as_ref().unwrap_or(&"_not set_".into()), + new.description.as_ref().unwrap_or(&"_not set_".into()) )); } @@ -43,14 +43,14 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option { Some(result.join("\n")) } -pub fn format_new_item(item: ShopItem) -> String { +pub fn format_new_item(item: &ShopItem) -> String { formatdoc! { "*New item added:* {full_name} *Description:* {description} *Price:* {price} *Stock:* {stock}", full_name = item.full_name, - description = item.description.unwrap_or("_not set_".into()), + description = item.description.as_ref().unwrap_or(&"_not set_".into()), price = item.price, stock = item.stock .map(|stock| stock.to_string()) @@ -103,7 +103,7 @@ mod diff_tests { }; assert_eq!( - diff_items(old, new), + format_item_diff(&old, &new), Some( indoc! {" *Name:* Test @@ -128,7 +128,7 @@ mod diff_tests { }; assert_eq!( - diff_items(old, new), + format_item_diff(&old, &new), Some( indoc! {" *Name:* Test @@ -153,7 +153,7 @@ mod diff_tests { }; assert_eq!( - diff_items(old, new), + format_item_diff(&old, &new), Some( indoc! {" *Name:* Test @@ -178,7 +178,7 @@ mod diff_tests { }; assert_eq!( - diff_items(old, new), + format_item_diff(&old, &new), Some( indoc! {" *Name:* Test @@ -203,7 +203,7 @@ mod diff_tests { }; assert_eq!( - diff_items(old, new), + format_item_diff(&old, &new), Some( indoc! {" *Name:* Test @@ -221,6 +221,6 @@ mod diff_tests { ..Default::default() }; - assert_eq!(diff_items(item.clone(), item), None); + assert_eq!(format_item_diff(&item, &item), None); } } diff --git a/src/lib.rs b/src/lib.rs index c8cbe0a..b962126 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,12 +38,12 @@ async fn run_scrape(env: Env) -> Result { match old_item { Some(old) => { - if let Some(diff) = format::format_diff(old, item) { + if let Some(diff) = format::format_item_diff(old, item) { result.push(diff); } } None => { - result.push(format::format_new_item(item)); + result.push(format::format_new_item(&item)); } } }