aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2024-01-12 12:40:49 -0500
committeralyx <alyx@aleteoryx.me>2024-01-12 12:40:49 -0500
commit2feec01f58ce903d07d53bb1b79dc6b448b1a146 (patch)
tree243b2003f596f77d2d0a706911e30e1d245e9b7c /src/config.rs
parentd2c27e17897f80929d2ba5fed1055eade27a6b08 (diff)
downloadlfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.tar.gz
lfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.tar.bz2
lfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.zip
Clippy
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/config.rs b/src/config.rs
index 49b515e..e635f4a 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -14,13 +14,25 @@ use tokio::sync::RwLock;
use handlebars::{Handlebars, handlebars_helper};
use duration_str as ds;
-static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", include_str!("themes/plain.hbs"))];
+type CacheFuture<Output> = Pin<Box<(dyn Future<Output = Result<Output, (StatusCode, &'static str)>> + Send + Sync)>>;
+type CacheGetter<Output> = fn(&String) -> CacheFuture<Output>;
+type Cache<Output> = Arc<RwLock<AsyncCache<String, Output, CacheGetter<Output>>>>;
+
+type FontFuture = CacheFuture<Arc<str>>;
+type FontGetter = CacheGetter<Arc<str>>;
+type FontCache = Cache<Arc<str>>;
+
+type UserFuture = CacheFuture<Arc<(User, Track)>>;
+type UserGetter = CacheGetter<Arc<(User, Track)>>;
+type UserCache = Cache<Arc<(User, Track)>>;
+
+static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("themes/plain.hbs"))];
pub static STATE: LazyLock<Arc<State>> = LazyLock::new(|| {
State::new()
});
-fn user_getter(username: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<(User, Track)>, (StatusCode, &'static str)>> + Send + Sync)>> {
+fn user_getter(username: &String) -> UserFuture {
let username = urlencoding::encode(username.as_ref()).to_string();
Box::pin(async move {
let userreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", STATE.lastfm_api_key))
@@ -39,13 +51,13 @@ fn user_getter(username: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<(Us
let tracksinfo = tracksreq.json::<GetRecentTracks>().await
.map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})?
- .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;
+ .recenttracks.track.into_iter().next().ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;
Ok(Arc::new((userinfo, tracksinfo)))
})
}
-fn font_getter(fontname: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<str>, (StatusCode, &'static str)>> + Send + Sync)>> {
+fn font_getter(fontname: &String) -> FontFuture {
let fontname = urlencoding::encode(fontname.as_ref()).to_string();
Box::pin(async move {
let Some(google_api_key) = STATE.google_api_key.clone()
@@ -70,10 +82,7 @@ fn font_getter(fontname: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<str
})
}
-type UserGetter = fn(&String) -> Pin<Box<(dyn Future<Output = Result<Arc<(User, Track)>, (StatusCode, &'static str)>> + Send + Sync)>>;
-type UserCache = Arc<RwLock<AsyncCache<String, Arc<(User, Track)>, UserGetter>>>;
-type FontGetter = fn(&String) -> Pin<Box<(dyn Future<Output = Result<Arc<str>, (StatusCode, &'static str)>> + Send + Sync)>>;
-type FontCache = Arc<RwLock<AsyncCache<String, Arc<str>, FontGetter>>>;
+
#[derive(Debug)]
enum Whitelist {
Exclusive{cache: UserCache, whitelist: BTreeSet<String>},
@@ -142,7 +151,7 @@ impl State {
whitelist: {
let load_whitelist = || -> Option<BTreeSet<String>> {
var("LFME_WHITELIST").ok().map(
- |w| w.split(",").map(|s| s.trim().to_string()).collect()
+ |w| w.split(',').map(|s| s.trim().to_string()).collect()
)
};