Skip to content

Commit

Permalink
wip: use shields.io for badges temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Jun 30, 2024
1 parent e6ec9c1 commit a2b1c2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
26 changes: 25 additions & 1 deletion src/badge.rs
Original file line number Diff line number Diff line change
@@ -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###"
<svg
Expand Down Expand Up @@ -32,3 +35,24 @@ pub fn generate(title: &str, value: &str) -> 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<String> {
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);
}
}
16 changes: 11 additions & 5 deletions src/currency/monero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ pub async fn balance(State(state): State<AppState>) -> 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(),
)
}

Expand All @@ -222,8 +224,10 @@ pub async fn payouts(State(state): State<AppState>) -> 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(),
)
}

Expand All @@ -234,6 +238,8 @@ pub async fn address(State(state): State<AppState>) -> impl IntoResponse {
crate::badge::generate(
"XMR",
&format!("{}", state.monero.wallet_address.to_string()),
),
)
.await
.unwrap(),
)
}

0 comments on commit a2b1c2e

Please sign in to comment.