Refactor format.rs for better code organization and naming consistency

This commit is contained in:
SkyfallWasTaken 2024-07-01 19:13:11 +01:00
parent 4084eb4568
commit 42cc05dbab
2 changed files with 13 additions and 13 deletions

View file

@ -2,7 +2,7 @@ use indoc::formatdoc;
use crate::items::ShopItem; use crate::items::ShopItem;
pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> { pub fn format_item_diff(old: &ShopItem, new: &ShopItem) -> Option<String> {
if old == new { if old == new {
// The items are the exact same // The items are the exact same
return None; return None;
@ -19,8 +19,8 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> {
if old.description != new.description { if old.description != new.description {
result.push(format!( result.push(format!(
"*Description:* {} → {}", "*Description:* {} → {}",
old.description.unwrap_or("_not set_".into()), old.description.as_ref().unwrap_or(&"_not set_".into()),
new.description.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<String> {
Some(result.join("\n")) Some(result.join("\n"))
} }
pub fn format_new_item(item: ShopItem) -> String { pub fn format_new_item(item: &ShopItem) -> String {
formatdoc! { formatdoc! {
"*New item added:* {full_name} "*New item added:* {full_name}
*Description:* {description} *Description:* {description}
*Price:* {price} *Price:* {price}
*Stock:* {stock}", *Stock:* {stock}",
full_name = item.full_name, 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, price = item.price,
stock = item.stock stock = item.stock
.map(|stock| stock.to_string()) .map(|stock| stock.to_string())
@ -103,7 +103,7 @@ mod diff_tests {
}; };
assert_eq!( assert_eq!(
diff_items(old, new), format_item_diff(&old, &new),
Some( Some(
indoc! {" indoc! {"
*Name:* Test *Name:* Test
@ -128,7 +128,7 @@ mod diff_tests {
}; };
assert_eq!( assert_eq!(
diff_items(old, new), format_item_diff(&old, &new),
Some( Some(
indoc! {" indoc! {"
*Name:* Test *Name:* Test
@ -153,7 +153,7 @@ mod diff_tests {
}; };
assert_eq!( assert_eq!(
diff_items(old, new), format_item_diff(&old, &new),
Some( Some(
indoc! {" indoc! {"
*Name:* Test *Name:* Test
@ -178,7 +178,7 @@ mod diff_tests {
}; };
assert_eq!( assert_eq!(
diff_items(old, new), format_item_diff(&old, &new),
Some( Some(
indoc! {" indoc! {"
*Name:* Test *Name:* Test
@ -203,7 +203,7 @@ mod diff_tests {
}; };
assert_eq!( assert_eq!(
diff_items(old, new), format_item_diff(&old, &new),
Some( Some(
indoc! {" indoc! {"
*Name:* Test *Name:* Test
@ -221,6 +221,6 @@ mod diff_tests {
..Default::default() ..Default::default()
}; };
assert_eq!(diff_items(item.clone(), item), None); assert_eq!(format_item_diff(&item, &item), None);
} }
} }

View file

@ -38,12 +38,12 @@ async fn run_scrape(env: Env) -> Result<String> {
match old_item { match old_item {
Some(old) => { Some(old) => {
if let Some(diff) = format::format_diff(old, item) { if let Some(diff) = format::format_item_diff(old, item) {
result.push(diff); result.push(diff);
} }
} }
None => { None => {
result.push(format::format_new_item(item)); result.push(format::format_new_item(&item));
} }
} }
} }