From 2feec01f58ce903d07d53bb1b79dc6b448b1a146 Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 12 Jan 2024 12:40:49 -0500 Subject: Clippy --- src/ctx.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/ctx.rs') 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) } } } -- cgit v1.2.3-54-g00ecf