aboutsummaryrefslogtreecommitdiffstats
path: root/src/ctx.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/ctx.rs
parentd2c27e17897f80929d2ba5fed1055eade27a6b08 (diff)
downloadlfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.tar.gz
lfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.tar.bz2
lfm_embed-2feec01f58ce903d07d53bb1b79dc6b448b1a146.zip
Clippy
Diffstat (limited to 'src/ctx.rs')
-rw-r--r--src/ctx.rs32
1 files changed, 23 insertions, 9 deletions
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<str> },
}
+ #[derive(serde::Serialize, Debug)]
+ pub struct Data {
+ pub user: User,
+ pub scrobble: Scrobble,
+ pub font: Option<Font>,
+ pub query: BTreeMap<String, String>,
+ }
+
/// 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<Font>, query: BTreeMap<String, String>, }
+ Data { #[serde(flatten)] data: Box<Data> }
+ }
+
+ impl From<Data> 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<Arc<(de::User, de::Track)>, (StatusCode, &'static str)>, font_query: Option<FontQuery>, query: BTreeMap<String, String>) -> 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)
}
}
}