From 2feec01f58ce903d07d53bb1b79dc6b448b1a146 Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 12 Jan 2024 12:40:49 -0500 Subject: Clippy --- src/cache.rs | 8 ++++---- src/config.rs | 27 ++++++++++++++++++--------- src/ctx.rs | 32 +++++++++++++++++++++++--------- src/main.rs | 2 +- 4 files changed, 46 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/cache.rs b/src/cache.rs index dbb3759..0e8fd4d 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -21,17 +21,17 @@ where } pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { - if self.is_stale(&key) { + if self.is_stale(key) { log::trace!(target: "lfm::cache", "MISS : interval = {:?}", self.interval); - self.renew(&key).await + self.renew(key).await } else { log::trace!(target: "lfm::cache", "HIT : interval = {:?}", self.interval); - Ok(&self.cache.get(&key).unwrap().1) + Ok(&self.cache.get(key).unwrap().1) } } pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { - let val = (self.func)(&key).await?; + let val = (self.func)(key).await?; self.cache.insert(key.clone(), (Instant::now(), val)); Ok(&self.cache.get(key).unwrap().1) } 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 = Pin> + Send + Sync)>>; +type CacheGetter = fn(&String) -> CacheFuture; +type Cache = Arc>>>; + +type FontFuture = CacheFuture>; +type FontGetter = CacheGetter>; +type FontCache = Cache>; + +type UserFuture = CacheFuture>; +type UserGetter = CacheGetter>; +type UserCache = Cache>; + +static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("themes/plain.hbs"))]; pub static STATE: LazyLock> = LazyLock::new(|| { State::new() }); -fn user_getter(username: &String) -> Pin, (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().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, (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 Pin, (StatusCode, &'static str)>> + Send + Sync)>>; -type UserCache = Arc, UserGetter>>>; -type FontGetter = fn(&String) -> Pin, (StatusCode, &'static str)>> + Send + Sync)>>; -type FontCache = Arc, FontGetter>>>; + #[derive(Debug)] enum Whitelist { Exclusive{cache: UserCache, whitelist: BTreeSet}, @@ -142,7 +151,7 @@ impl State { whitelist: { let load_whitelist = || -> Option> { 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() ) }; diff --git a/src/ctx.rs b/src/ctx.rs index 681230e..d564bd6 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -76,27 +76,41 @@ pub mod model { Name { name: Arc }, } + #[derive(serde::Serialize, Debug)] + pub struct Data { + pub user: User, + pub scrobble: Scrobble, + pub font: Option, + pub query: BTreeMap, + } + /// The context passed in to all themes. /// /// Serialized as untagged, so themes should check if `error` is set and decide whether to show an error state or try rendering user info. #[derive(serde::Serialize, Debug)] #[serde(untagged)] - pub enum Data { + pub enum Root { /// Contains text explaining a potential error. Error { error: &'static str }, /// Contains data about a user and what they're listening to. - Data { user: User, scrobble: Scrobble, font: Option, query: BTreeMap, } + Data { #[serde(flatten)] data: Box } + } + + impl From for Root { + fn from(v: Data) -> Self { + Self::Data { data: Box::new(v) } + } } } #[derive(Debug)] -pub struct ResponseCtx(pub model::Data, pub StatusCode); +pub struct ResponseCtx(pub model::Root, pub StatusCode); impl ResponseCtx { pub async fn create(api_result: Result, (StatusCode, &'static str)>, font_query: Option, query: BTreeMap) -> ResponseCtx { match api_result { Ok(a) => { let (user, track) = a.as_ref(); - ResponseCtx(model::Data::Data { + ResponseCtx((model::Data { user: model::User { name: user.name.clone(), realname: user.realname.clone(), @@ -127,7 +141,7 @@ impl ResponseCtx { Some(FontQuery { google_font: Some(f), .. }) if STATE.has_google_api_key() => { let css = match STATE.get_fontinfo(&f.to_string()).await { Ok(css) => css, - Err((status, error)) => { return ResponseCtx(model::Data::Error {error}, status); } + Err((status, error)) => { return ResponseCtx(model::Root::Error {error}, status); } }; Some(model::Font::External { css, @@ -138,18 +152,18 @@ impl ResponseCtx { model::Font::External { css: format!( "@font-face {{ font-family: 'included_font'; src: url('{}'); }}", - f.replace("\\", "\\\\") - .replace("'", "\\'")).into(), + f.replace('\\', "\\\\") + .replace('\'', "\\'")).into(), name: "included_font".into() }), Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }), _ => None, }, query - }, StatusCode::OK) + }).into(), StatusCode::OK) }, Err((status, error)) => { - ResponseCtx(model::Data::Error {error}, status) + ResponseCtx(model::Root::Error {error}, status) } } } diff --git a/src/main.rs b/src/main.rs index 34dafda..cf7856a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ async fn main() { let (ctx, dur) = STATE.get_userinfo(&s).await; let ResponseCtx(data, status) = ResponseCtx::create(ctx, q.font, q.rest).await; - let theme = q.theme.filter(|a| STATE.handlebars().has_template(&a)).unwrap_or_else(|| STATE.default_theme()); + let theme = q.theme.filter(|a| STATE.handlebars().has_template(a)).unwrap_or_else(|| STATE.default_theme()); log::debug!(target: "lfm::server::user", "Using theme {theme}"); warp::reply::with_header( warp::reply::with_header( -- cgit v1.2.3-54-g00ecf