Use indoc to clean up format_new_item

This commit is contained in:
SkyfallWasTaken 2024-07-01 18:39:26 +01:00
parent 3da38ffaa5
commit 50629e6a3b
2 changed files with 34 additions and 6 deletions

View file

@ -17,10 +17,8 @@ console_error_panic_hook = { version = "0.1.1" }
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.118"
scraper = "0.19.0"
indoc = "2.0.5"
# We don't use getrandom, but we enable the "js" feature to enable
# WebAssembly support.
getrandom = { version = "0.2", features = ["js"] }
[dev-dependencies]
indoc = "2.0.5"

View file

@ -1,3 +1,5 @@
use indoc::{formatdoc, indoc};
use crate::items::ShopItem;
pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> {
@ -42,15 +44,43 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option<String> {
}
pub fn format_new_item(item: ShopItem) -> String {
format!(
"*New item added:* {full_name}\n*Description:* {description}\n*Price:* {price}\n*Stock:* {stock}",
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()),
price = item.price,
stock = item.stock
.map(|stock| stock.to_string())
.unwrap_or("Unlimited".into()),
)
}
}
#[cfg(test)]
mod format_new_tests {
use super::*;
#[test]
fn item_formatted() {
let item = ShopItem {
full_name: "Test".into(),
description: Some("Lorem ipsum".into()),
price: 1,
stock: Some(10),
..Default::default()
};
assert_eq!(
format_new_item(item),
indoc! {"
*New item added:* Test
*Description:* Lorem ipsum
*Price:* 1
*Stock:* 10"}
);
}
}
#[cfg(test)]