From 41a5fa81b5c12feb8aab0ad82d42c135e93065f7 Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 22 Nov 2023 19:49:27 -0500 Subject: Add custom font support. --- src/ctx.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/ctx.rs') diff --git a/src/ctx.rs b/src/ctx.rs index 63b54ff..904667d 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -2,6 +2,7 @@ use reqwest::StatusCode; use super::deserialize as de; use std::sync::Arc; use std::collections::BTreeMap; +use super::font::FontQuery; pub mod model { use std::sync::Arc; @@ -66,6 +67,13 @@ pub mod model { pub loved: bool } + #[derive(serde::Serialize, Debug)] + #[serde(untagged)] + pub enum Font { + Url { css: Arc }, + Name { name: Arc }, + } + /// 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. @@ -75,15 +83,15 @@ pub mod model { /// 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, query: BTreeMap } + Data { user: User, scrobble: Scrobble, font: Option, query: BTreeMap, } } } #[derive(Debug)] pub struct ResponseCtx(pub model::Data, pub StatusCode); -impl From<(Result, (StatusCode, &'static str)>, BTreeMap)> for ResponseCtx { - fn from(v: (Result, (StatusCode, &'static str)>, BTreeMap)) -> ResponseCtx { - let (v, q) = v; +impl From<(Result, (StatusCode, &'static str)>, Option, BTreeMap)> for ResponseCtx { + fn from(v: (Result, (StatusCode, &'static str)>, Option, BTreeMap)) -> ResponseCtx { + let (v, f, q) = v; match v { Ok(a) => { let (user, track) = a.as_ref(); @@ -115,6 +123,11 @@ impl From<(Result, (StatusCode, &'static str)>, BTree url: track.url.clone(), loved: track.loved.unwrap_or(false) }, + font: match f { + Some(FontQuery { font: Some(s), include_font: None }) => Some(model::Font::Name { name: s }), + Some(FontQuery { include_font: Some(s), .. }) => Some(model::Font::Url { css: format!("@font-face {{ font-family: 'included_font'; src: url('{}'); }}", s.replace("\\", "\\\\").replace("'", "\\'")).into() }), + _ => None, + }, query: q }, StatusCode::OK) }, -- cgit v1.2.3-54-g00ecf