Skip to content

Commit 22fd29e

Browse files
authored
Merge pull request #67 from mfirhas/master
Format conversion response
2 parents 7bb73de + d96cc83 commit 22fd29e

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ regex = "1"
2828
async-trait = "0.1"
2929
rand = "0.8"
3030

31+
accounting = { version = "0.2.0", features = ["decimal"] }
32+
3133
# log = "0.4"
3234
# pretty_env_logger = "0.4"
3335
# sentry = "0.29.0"

src/handlers/convert.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::commands::Args;
1212
use crate::deps::http_client::http_client;
1313
use crate::error::{AsInternalError, HandlerError};
1414
use crate::handlers::forex::{ConvertResponseData, ForexResp};
15+
use crate::utils::money::format_money_str;
1516

1617
// format of a currency code: USD, IDR, BTC, XAU. Case insensitive.
1718
static CURRENCY_FORMAT: LazyLock<Regex> =
@@ -175,11 +176,12 @@ impl Display for ConvertResponse {
175176
.cloned()
176177
.unwrap_or(ZERO_AMOUNT.into());
177178

179+
let from_fmt = format_money_str(&from_currency, &from_amount);
180+
178181
format!(
179-
"Conversion on {}:\n<b>{} {} = {}</b>",
182+
"Conversion on {}:\n<b>{} = {}</b>",
180183
data.date.format("%Y-%m-%d %H:%M:%S %:z").to_string(),
181-
from_currency,
182-
from_amount,
184+
from_fmt,
183185
data.code,
184186
)
185187
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod config;
1717
mod deps;
1818
mod error;
1919
mod handlers;
20+
mod utils;
2021
use config::config;
2122

2223
static WEBHOOK_ENDPOINT: &'static str = "https://api.mfirhas.com/webhook";

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod money;

src/utils/money.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::str::FromStr;
2+
3+
use accounting::Accounting;
4+
use rust_decimal::Decimal;
5+
use rust_decimal_macros::dec;
6+
7+
pub fn format_money_str(code: &str, money_str: &str) -> String {
8+
let mut ac = Accounting::new_from_seperator(code, 2, ",", ".");
9+
10+
ac.set_format("{s} {v}");
11+
12+
let amount = Decimal::from_str(money_str).unwrap_or(dec!(0));
13+
let money_display = ac.format_money(amount);
14+
15+
money_display
16+
}

0 commit comments

Comments
 (0)