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;
pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> {
pub fn format_item_diff(old: &ShopItem, new: &ShopItem) -> Option<String> {
if old == new {
// The items are the exact same
return None;
@ -19,8 +19,8 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> {
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<String> {
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);
}
}

View file

@ -38,12 +38,12 @@ async fn run_scrape(env: Env) -> Result<String> {
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));
}
}
}