My personal site
cherry.computer
htmx
tailwind
axum
askama
1use std::sync::Arc;
2
3use crate::scrapers::{Media, MediaType, apple_music::AppleMusicClient, backloggd, letterboxd};
4
5use askama::Template;
6use rand::seq::IndexedRandom;
7use serde::Deserialize;
8
9#[derive(Deserialize)]
10pub struct IndexOptions {
11 #[cfg(debug_assertions)]
12 #[serde(default)]
13 mock: bool,
14}
15
16type MediaList = [(MediaType, Option<Media>); 3];
17
18#[derive(Debug, Clone)]
19pub struct Shas {
20 pub website: Option<String>,
21}
22
23#[derive(Template, Debug, Clone)]
24#[template(path = "index.html")]
25pub struct RootTemplate {
26 media: MediaList,
27 consumption_verb: &'static str,
28 shas: Shas,
29}
30
31impl RootTemplate {
32 pub fn new(
33 apple_music_client: Arc<AppleMusicClient>,
34 shas: Shas,
35 #[allow(unused_variables)] options: &IndexOptions,
36 ) -> RootTemplate {
37 #[cfg(debug_assertions)]
38 let media = if options.mock {
39 mocked_media()
40 } else {
41 Self::fetch_media(apple_music_client)
42 };
43 #[cfg(not(debug_assertions))]
44 let media = Self::fetch_media(apple_music_client);
45
46 let consumption_verb = Self::random_consumption_verb();
47
48 RootTemplate {
49 media,
50 consumption_verb,
51 shas,
52 }
53 }
54
55 fn fetch_media(apple_music_client: Arc<AppleMusicClient>) -> MediaList {
56 [
57 (MediaType::Game, backloggd::try_cached_fetch()),
58 (MediaType::Film, letterboxd::try_cached_fetch()),
59 (MediaType::Song, apple_music_client.try_cached_fetch()),
60 ]
61 }
62
63 fn random_consumption_verb() -> &'static str {
64 static CONSUMPTION_VERBS: &[&str] = &["swallowed", "inhaled", "digested", "ingested"];
65
66 CONSUMPTION_VERBS.choose(&mut rand::rng()).unwrap()
67 }
68}
69
70#[cfg(debug_assertions)]
71fn mocked_media() -> MediaList {
72 [
73 (MediaType::Game, Some(Media {
74 name: "Cyberpunk 2077: Ultimate Edition".to_owned(),
75 image: "https://images.igdb.com/igdb/image/upload/t_cover_big/co7iy1.jpg".to_owned(),
76 context: "Nintendo Switch 2".to_owned(),
77 url: "https://backloggd.com/u/cherryfunk/logs/cyberpunk-2077-ultimate-edition/".to_owned()
78 })),
79 (MediaType::Film, Some(Media {
80 name: "The Thursday Murder Club".to_owned(),
81 image: "https://a.ltrbxd.com/resized/film-poster/6/6/6/2/8/6/666286-the-thursday-murder-club-0-230-0-345-crop.jpg?v=4bfeae38a7".to_owned(),
82 context: "1 star".to_owned(),
83 url: "https://letterboxd.com/ivom/film/the-thursday-murder-club/".to_owned()
84 })),
85 (MediaType::Song, Some(Media {
86 name: "We Might Feel Unsound".to_owned(),
87 image: "https://is1-ssl.mzstatic.com/image/thumb/Music124/v4/f4/b2/8e/f4b28ee4-01c6-232c-56a7-b97fd5b0e0ae/00602527857671.rgb.jpg/240x240bb.jpg".to_owned(),
88 context: "James Blake — James Blake".to_owned(),
89 url: "https://music.apple.com/gb/album/we-might-feel-unsound/1443124478?i=1443125024".to_owned()
90 })),
91 ]
92}