diff --git a/src/badge.rs b/src/badge.rs index 9aa4d0c..2b7702f 100644 --- a/src/badge.rs +++ b/src/badge.rs @@ -1,5 +1,8 @@ +use anyhow::Result; +use cached::proc_macro::once; + /// Generate a simple SVG badge with the given attributes. -pub fn generate(title: &str, value: &str) -> String { +pub fn _generate(title: &str, value: &str) -> String { format!( r###" String { // value.len() * 10, ) } + +/// Load a badge from shields.io. TODO: replace this with a custom generator. +#[once(result = true)] +pub async fn generate(title: &str, value: &str) -> Result { + Ok(reqwest::get(format!( + "https://img.shields.io/badge/{}-{}-green", + title, value + )) + .await? + .text() + .await?) +} + +#[cfg(test)] +mod test { + #[tokio::test] + async fn test_generate() { + let svg = super::generate("test", "value").await.unwrap(); + println!("{}", svg); + } +} diff --git a/src/currency/monero.rs b/src/currency/monero.rs index d59309b..1fd3b12 100644 --- a/src/currency/monero.rs +++ b/src/currency/monero.rs @@ -211,8 +211,10 @@ pub async fn balance(State(state): State) -> impl IntoResponse { [(header::CONTENT_TYPE, "image/svg+xml")], crate::badge::generate( "balance", - &format!("{} XMR", PrettyPrintFloat(monero_balance)), - ), + &format!("{:.1} XMR", PrettyPrintFloat(monero_balance)), + ) + .await + .unwrap(), ) } @@ -222,8 +224,10 @@ pub async fn payouts(State(state): State) -> impl IntoResponse { [(header::CONTENT_TYPE, "image/svg+xml")], crate::badge::generate( "payouts", - &format!("{:.1}", state.monero.get_transfers().await.unwrap().len()), - ), + &format!("{}", state.monero.get_transfers().await.unwrap().len()), + ) + .await + .unwrap(), ) } @@ -234,6 +238,8 @@ pub async fn address(State(state): State) -> impl IntoResponse { crate::badge::generate( "XMR", &format!("{}", state.monero.wallet_address.to_string()), - ), + ) + .await + .unwrap(), ) }