Skip to main content

webserver_base/templates/
base.rs

1//! Per-server template data: what is true of every page on the site.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use super::site_entity::SiteEntity;
7use super::smart_app_banner::SmartAppBanner;
8use super::theme::{Fallback, ThemeColor, ThemeScript};
9
10/// Everything [`BaseTemplateData::new`] requires.
11///
12/// A named-field struct rather than sixteen positional arguments: every field
13/// is mandatory either way, but this one cannot be built with two of them
14/// transposed.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct BaseTemplateDataParams {
17    /// The site's name, e.g. `Boggledygook`.
18    pub project: String,
19    /// The default `<meta name="description">`.
20    pub description: String,
21    /// Who wrote it.
22    pub author: String,
23    /// The site's origin, without a trailing slash — `https://www.example.com`.
24    pub base_url: String,
25    /// The Twitter/X handle, without the leading `@`.
26    pub twitter_username: String,
27    /// The default Open Graph / Twitter card image.
28    pub social_image: String,
29    /// The default alt text for that image.
30    pub social_image_alt: String,
31    /// The browser-chrome colour, and the schemes this site supports.
32    pub theme_color: ThemeColor,
33    /// Which theme to stamp when neither the reader nor their OS states a
34    /// preference. Design data, so it lives beside the colours it chooses
35    /// between.
36    pub theme_fallback: Fallback,
37    /// Who or what the site represents, for schema.org.
38    pub site_entity: SiteEntity,
39    /// The `<html lang>` language subtag, e.g. `en`.
40    pub language_code: String,
41    /// The region subtag, e.g. `US`.
42    pub country_code: String,
43    /// Other places this author exists, emitted as `og:see_also`.
44    pub see_also: Vec<String>,
45    /// The first year of the copyright range. The last is computed per render.
46    pub copyright_start: String,
47    /// Stylesheets every page loads.
48    pub style_sheets: Vec<String>,
49    /// Scripts every page loads.
50    pub scripts: Vec<String>,
51}
52
53/// Per-server template data.
54///
55/// Built once at boot and held in the server's state. Everything on it can be
56/// overridden for a single page by [`PageTemplateData`](super::PageTemplateData),
57/// except the fields that identify the site itself.
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
59pub struct BaseTemplateData {
60    project: String,
61    description: String,
62    author: String,
63    base_url: String,
64    twitter_username: String,
65    social_image: String,
66    social_image_alt: String,
67    theme_color: ThemeColor,
68    theme_fallback: Fallback,
69    site_entity: SiteEntity,
70    language_code: String,
71    country_code: String,
72    see_also: Vec<String>,
73    copyright_start: String,
74    style_sheets: Vec<String>,
75    scripts: Vec<String>,
76    #[serde(default)]
77    site: Value,
78    #[serde(default)]
79    smart_app_banner: Option<SmartAppBanner>,
80}
81
82impl BaseTemplateData {
83    /// Builds base data with every field supplied.
84    ///
85    /// Two paths are normalised on the way in. A trailing slash on `base_url`
86    /// is stripped, so joining it with a page URL cannot produce
87    /// `https://example.com//blog`. A *leading* slash on `social_image` is
88    /// stripped, so it keys into the cache-buster map exactly like the
89    /// stylesheet and script paths beside it — without that, the social card is
90    /// the one asset on the site that can never be content-hashed.
91    #[must_use]
92    pub fn new(params: BaseTemplateDataParams) -> Self {
93        Self {
94            project: params.project,
95            description: params.description,
96            author: params.author,
97            base_url: normalize_base_url(&params.base_url),
98            twitter_username: params.twitter_username,
99            social_image: normalize_asset_path(&params.social_image),
100            social_image_alt: params.social_image_alt,
101            theme_color: params.theme_color,
102            theme_fallback: params.theme_fallback,
103            site_entity: params.site_entity,
104            language_code: params.language_code,
105            country_code: params.country_code,
106            see_also: params.see_also,
107            copyright_start: params.copyright_start,
108            style_sheets: params.style_sheets,
109            scripts: params.scripts,
110            site: Value::Null,
111            smart_app_banner: None,
112        }
113    }
114
115    /// The project's own site-wide data, reachable on every page — the 404
116    /// included — as `{{site.…}}`.
117    ///
118    /// For what every page shares but the library has no field for: a nav
119    /// tree, a phone number, opening hours. Supplying it once here is what
120    /// lets a partial read it on a page whose `{{app}}` knows nothing about it.
121    ///
122    /// # Panics
123    ///
124    /// If `site` cannot be serialized — at boot, never per request.
125    #[must_use]
126    pub fn with_site<A>(mut self, site: A) -> Self
127    where
128        A: Serialize,
129    {
130        self.site = serde_json::to_value(site)
131            .unwrap_or_else(|error| panic!("the site data is unserializable: {error}"));
132        self
133    }
134
135    /// Advertises the site's iOS app (or App Clip) to Safari on every page, as
136    /// `<meta name="apple-itunes-app">`. Absent by default.
137    #[must_use]
138    pub fn with_smart_app_banner(mut self, smart_app_banner: SmartAppBanner) -> Self {
139        self.smart_app_banner = Some(smart_app_banner);
140        self
141    }
142
143    /// The Smart App Banner, if the site declared one.
144    #[must_use]
145    pub const fn smart_app_banner(&self) -> Option<&SmartAppBanner> {
146        self.smart_app_banner.as_ref()
147    }
148
149    /// Overrides the author.
150    #[must_use]
151    pub fn with_author(mut self, author: impl Into<String>) -> Self {
152        self.author = author.into();
153        self
154    }
155
156    /// Overrides the Twitter/X handle.
157    #[must_use]
158    pub fn with_twitter_username(mut self, twitter_username: impl Into<String>) -> Self {
159        self.twitter_username = twitter_username.into();
160        self
161    }
162
163    /// Overrides who or what the site represents.
164    #[must_use]
165    pub fn with_site_entity(mut self, site_entity: SiteEntity) -> Self {
166        self.site_entity = site_entity;
167        self
168    }
169
170    /// Overrides the language subtag.
171    #[must_use]
172    pub fn with_language_code(mut self, language_code: impl Into<String>) -> Self {
173        self.language_code = language_code.into();
174        self
175    }
176
177    /// Overrides the region subtag.
178    #[must_use]
179    pub fn with_country_code(mut self, country_code: impl Into<String>) -> Self {
180        self.country_code = country_code.into();
181        self
182    }
183
184    /// Replaces the `og:see_also` list wholesale.
185    ///
186    /// For a project that should not carry the author's personal links at all.
187    #[must_use]
188    pub fn replace_see_also<I, S>(mut self, see_also: I) -> Self
189    where
190        I: IntoIterator<Item = S>,
191        S: Into<String>,
192    {
193        self.see_also = see_also.into_iter().map(Into::into).collect();
194        self
195    }
196
197    /// Appends to the `og:see_also` list.
198    ///
199    /// For a project with its own presence alongside the author's — a band's
200    /// `YouTube` channel next to a personal one.
201    #[must_use]
202    pub fn extend_see_also<I, S>(mut self, see_also: I) -> Self
203    where
204        I: IntoIterator<Item = S>,
205        S: Into<String>,
206    {
207        self.see_also.extend(see_also.into_iter().map(Into::into));
208        self
209    }
210
211    /// Appends to the site-wide stylesheet list.
212    #[must_use]
213    pub fn extend_style_sheets<I, S>(mut self, style_sheets: I) -> Self
214    where
215        I: IntoIterator<Item = S>,
216        S: Into<String>,
217    {
218        self.style_sheets
219            .extend(style_sheets.into_iter().map(Into::into));
220        self
221    }
222
223    /// Appends to the site-wide script list.
224    #[must_use]
225    pub fn extend_scripts<I, S>(mut self, scripts: I) -> Self
226    where
227        I: IntoIterator<Item = S>,
228        S: Into<String>,
229    {
230        self.scripts.extend(scripts.into_iter().map(Into::into));
231        self
232    }
233
234    /// The project's site-wide data; `null` unless
235    /// [`with_site`](Self::with_site) was called.
236    #[must_use]
237    pub const fn site(&self) -> &Value {
238        &self.site
239    }
240
241    /// The site's name.
242    #[must_use]
243    pub fn project(&self) -> &str {
244        &self.project
245    }
246    /// The default description.
247    #[must_use]
248    pub fn description(&self) -> &str {
249        &self.description
250    }
251    /// The author.
252    #[must_use]
253    pub fn author(&self) -> &str {
254        &self.author
255    }
256    /// The site origin, with no trailing slash.
257    #[must_use]
258    pub fn base_url(&self) -> &str {
259        &self.base_url
260    }
261    /// The Twitter/X handle.
262    #[must_use]
263    pub fn twitter_username(&self) -> &str {
264        &self.twitter_username
265    }
266    /// The default social image.
267    #[must_use]
268    pub fn social_image(&self) -> &str {
269        &self.social_image
270    }
271    /// The default social image alt text.
272    #[must_use]
273    pub fn social_image_alt(&self) -> &str {
274        &self.social_image_alt
275    }
276    /// The browser-chrome colour and supported schemes.
277    #[must_use]
278    pub const fn theme_color(&self) -> &ThemeColor {
279        &self.theme_color
280    }
281    /// Who or what the site represents.
282    #[must_use]
283    pub const fn site_entity(&self) -> &SiteEntity {
284        &self.site_entity
285    }
286    /// The pre-paint theme script this site's design implies.
287    #[must_use]
288    pub const fn theme_script(&self) -> ThemeScript {
289        ThemeScript::new(self.theme_fallback)
290    }
291    /// The language subtag.
292    #[must_use]
293    pub fn language_code(&self) -> &str {
294        &self.language_code
295    }
296    /// The region subtag.
297    #[must_use]
298    pub fn country_code(&self) -> &str {
299        &self.country_code
300    }
301    /// The `og:see_also` list.
302    #[must_use]
303    pub fn see_also(&self) -> &[String] {
304        &self.see_also
305    }
306    /// The first year of the copyright range.
307    #[must_use]
308    pub fn copyright_start(&self) -> &str {
309        &self.copyright_start
310    }
311    /// The site-wide stylesheets.
312    #[must_use]
313    pub fn style_sheets(&self) -> &[String] {
314        &self.style_sheets
315    }
316    /// The site-wide scripts.
317    #[must_use]
318    pub fn scripts(&self) -> &[String] {
319        &self.scripts
320    }
321}
322
323/// Strips any trailing slashes from a site origin.
324fn normalize_base_url(base_url: &str) -> String {
325    base_url.trim().trim_end_matches('/').to_string()
326}
327
328/// Strips a leading slash, so an asset path matches its cache-buster key.
329fn normalize_asset_path(path: &str) -> String {
330    path.trim().trim_start_matches('/').to_string()
331}
332
333// ─────────────────────────────────────────────────────────────────────────────
334// preset
335// ─────────────────────────────────────────────────────────────────────────────
336
337/// One of Todd Griffin's social profiles: what a project's icon row renders.
338#[cfg(feature = "preset")]
339#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
340pub struct Social {
341    /// The network's name, for `aria-label` and `title`.
342    pub label: &'static str,
343    /// The profile URL.
344    pub href: &'static str,
345    /// SVG path data for a `0 0 24 24` view box, drawn in `currentColor` (Simple Icons 16.36.0, CC0).
346    pub icon: &'static str,
347}
348
349/// Todd Griffin's social profiles, in the order every project shows them. The single source for both
350/// [`GODDTRIFFIN_SEE_ALSO`] and a site's icon row: add, drop or reorder a network here only.
351#[cfg(feature = "preset")]
352pub const GODDTRIFFIN_SOCIALS: [Social; 8] = [
353    Social {
354        label: "X",
355        href: "https://x.com/goddtriffin",
356        icon: "M14.234 10.162 22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299-.929-1.329L3.076 1.56h3.182l5.965 8.532.929 1.329 7.754 11.09h-3.182z",
357    },
358    Social {
359        label: "LinkedIn",
360        href: "https://www.linkedin.com/in/todd-griffin/",
361        icon: "M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z",
362    },
363    Social {
364        label: "GitHub",
365        href: "https://github.com/goddtriffin",
366        icon: "M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12",
367    },
368    Social {
369        label: "YouTube",
370        href: "https://www.youtube.com/@goddtriffin",
371        icon: "M23.498 6.186a3.016 3.016 0 0 0-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 0 0 .502 6.186C0 8.07 0 12 0 12s0 3.93.502 5.814a3.016 3.016 0 0 0 2.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 0 0 2.122-2.136C24 15.93 24 12 24 12s0-3.93-.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z",
372    },
373    Social {
374        label: "Instagram",
375        href: "https://www.instagram.com/goddtriffin/",
376        icon: "M7.0301.084c-1.2768.0602-2.1487.264-2.911.5634-.7888.3075-1.4575.72-2.1228 1.3877-.6652.6677-1.075 1.3368-1.3802 2.127-.2954.7638-.4956 1.6365-.552 2.914-.0564 1.2775-.0689 1.6882-.0626 4.947.0062 3.2586.0206 3.6671.0825 4.9473.061 1.2765.264 2.1482.5635 2.9107.308.7889.72 1.4573 1.388 2.1228.6679.6655 1.3365 1.0743 2.1285 1.38.7632.295 1.6361.4961 2.9134.552 1.2773.056 1.6884.069 4.9462.0627 3.2578-.0062 3.668-.0207 4.9478-.0814 1.28-.0607 2.147-.2652 2.9098-.5633.7889-.3086 1.4578-.72 2.1228-1.3881.665-.6682 1.0745-1.3378 1.3795-2.1284.2957-.7632.4966-1.636.552-2.9124.056-1.2809.0692-1.6898.063-4.948-.0063-3.2583-.021-3.6668-.0817-4.9465-.0607-1.2797-.264-2.1487-.5633-2.9117-.3084-.7889-.72-1.4568-1.3876-2.1228C21.2982 1.33 20.628.9208 19.8378.6165 19.074.321 18.2017.1197 16.9244.0645 15.6471.0093 15.236-.005 11.977.0014 8.718.0076 8.31.0215 7.0301.0839m.1402 21.6932c-1.17-.0509-1.8053-.2453-2.2287-.408-.5606-.216-.96-.4771-1.3819-.895-.422-.4178-.6811-.8186-.9-1.378-.1644-.4234-.3624-1.058-.4171-2.228-.0595-1.2645-.072-1.6442-.079-4.848-.007-3.2037.0053-3.583.0607-4.848.05-1.169.2456-1.805.408-2.2282.216-.5613.4762-.96.895-1.3816.4188-.4217.8184-.6814 1.3783-.9003.423-.1651 1.0575-.3614 2.227-.4171 1.2655-.06 1.6447-.072 4.848-.079 3.2033-.007 3.5835.005 4.8495.0608 1.169.0508 1.8053.2445 2.228.408.5608.216.96.4754 1.3816.895.4217.4194.6816.8176.9005 1.3787.1653.4217.3617 1.056.4169 2.2263.0602 1.2655.0739 1.645.0796 4.848.0058 3.203-.0055 3.5834-.061 4.848-.051 1.17-.245 1.8055-.408 2.2294-.216.5604-.4763.96-.8954 1.3814-.419.4215-.8181.6811-1.3783.9-.4224.1649-1.0577.3617-2.2262.4174-1.2656.0595-1.6448.072-4.8493.079-3.2045.007-3.5825-.006-4.848-.0608M16.953 5.5864A1.44 1.44 0 1 0 18.39 4.144a1.44 1.44 0 0 0-1.437 1.4424M5.8385 12.012c.0067 3.4032 2.7706 6.1557 6.173 6.1493 3.4026-.0065 6.157-2.7701 6.1506-6.1733-.0065-3.4032-2.771-6.1565-6.174-6.1498-3.403.0067-6.156 2.771-6.1496 6.1738M8 12.0077a4 4 0 1 1 4.008 3.9921A3.9996 3.9996 0 0 1 8 12.0077",
377    },
378    Social {
379        label: "Facebook",
380        href: "https://www.facebook.com/goddtriffin/",
381        icon: "M9.101 23.691v-7.98H6.627v-3.667h2.474v-1.58c0-4.085 1.848-5.978 5.858-5.978.401 0 .955.042 1.468.103a8.68 8.68 0 0 1 1.141.195v3.325a8.623 8.623 0 0 0-.653-.036 26.805 26.805 0 0 0-.733-.009c-.707 0-1.259.096-1.675.309a1.686 1.686 0 0 0-.679.622c-.258.42-.374.995-.374 1.752v1.297h3.919l-.386 2.103-.287 1.564h-3.246v8.245C19.396 23.238 24 18.179 24 12.044c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.628 3.874 10.35 9.101 11.647Z",
382    },
383    Social {
384        label: "Reddit",
385        href: "https://www.reddit.com/user/goddtriffin",
386        icon: "M12 0C5.373 0 0 5.373 0 12c0 3.314 1.343 6.314 3.515 8.485l-2.286 2.286C.775 23.225 1.097 24 1.738 24H12c6.627 0 12-5.373 12-12S18.627 0 12 0Zm4.388 3.199c1.104 0 1.999.895 1.999 1.999 0 1.105-.895 2-1.999 2-.946 0-1.739-.657-1.947-1.539v.002c-1.147.162-2.032 1.15-2.032 2.341v.007c1.776.067 3.4.567 4.686 1.363.473-.363 1.064-.58 1.707-.58 1.547 0 2.802 1.254 2.802 2.802 0 1.117-.655 2.081-1.601 2.531-.088 3.256-3.637 5.876-7.997 5.876-4.361 0-7.905-2.617-7.998-5.87-.954-.447-1.614-1.415-1.614-2.538 0-1.548 1.255-2.802 2.803-2.802.645 0 1.239.218 1.712.585 1.275-.79 2.881-1.291 4.64-1.365v-.01c0-1.663 1.263-3.034 2.88-3.207.188-.911.993-1.595 1.959-1.595Zm-8.085 8.376c-.784 0-1.459.78-1.506 1.797-.047 1.016.64 1.429 1.426 1.429.786 0 1.371-.369 1.418-1.385.047-1.017-.553-1.841-1.338-1.841Zm7.406 0c-.786 0-1.385.824-1.338 1.841.047 1.017.634 1.385 1.418 1.385.785 0 1.473-.413 1.426-1.429-.046-1.017-.721-1.797-1.506-1.797Zm-3.703 4.013c-.974 0-1.907.048-2.77.135-.147.015-.241.168-.183.305.483 1.154 1.622 1.964 2.953 1.964 1.33 0 2.47-.81 2.953-1.964.057-.137-.037-.29-.184-.305-.863-.087-1.795-.135-2.769-.135Z",
387    },
388    Social {
389        label: "itch.io",
390        href: "https://goddtriffin.itch.io/",
391        icon: "M3.13 1.338C2.08 1.96.02 4.328 0 4.95v1.03c0 1.303 1.22 2.45 2.325 2.45 1.33 0 2.436-1.102 2.436-2.41 0 1.308 1.07 2.41 2.4 2.41 1.328 0 2.362-1.102 2.362-2.41 0 1.308 1.137 2.41 2.466 2.41h.024c1.33 0 2.466-1.102 2.466-2.41 0 1.308 1.034 2.41 2.363 2.41 1.33 0 2.4-1.102 2.4-2.41 0 1.308 1.106 2.41 2.435 2.41C22.78 8.43 24 7.282 24 5.98V4.95c-.02-.62-2.082-2.99-3.13-3.612-3.253-.114-5.508-.134-8.87-.133-3.362 0-7.945.053-8.87.133zm6.376 6.477a2.74 2.74 0 0 1-.468.602c-.5.49-1.19.795-1.947.795a2.786 2.786 0 0 1-1.95-.795c-.182-.178-.32-.37-.446-.59-.127.222-.303.412-.486.59a2.788 2.788 0 0 1-1.95.795c-.092 0-.187-.025-.264-.052-.107 1.113-.152 2.176-.168 2.95v.005l-.006 1.167c.02 2.334-.23 7.564 1.03 8.85 1.952.454 5.545.662 9.15.663 3.605 0 7.198-.21 9.15-.664 1.26-1.284 1.01-6.514 1.03-8.848l-.006-1.167v-.004c-.016-.775-.06-1.838-.168-2.95-.077.026-.172.052-.263.052a2.788 2.788 0 0 1-1.95-.795c-.184-.178-.36-.368-.486-.59-.127.22-.265.412-.447.59a2.786 2.786 0 0 1-1.95.794c-.76 0-1.446-.303-1.948-.793a2.74 2.74 0 0 1-.468-.602 2.738 2.738 0 0 1-.463.602 2.787 2.787 0 0 1-1.95.794h-.16a2.787 2.787 0 0 1-1.95-.793 2.738 2.738 0 0 1-.464-.602zm-2.004 2.59v.002c.795.002 1.5 0 2.373.953.687-.072 1.406-.108 2.125-.107.72 0 1.438.035 2.125.107.873-.953 1.578-.95 2.372-.953.376 0 1.876 0 2.92 2.934l1.123 4.028c.832 2.995-.266 3.068-1.636 3.07-2.03-.075-3.156-1.55-3.156-3.025-1.124.184-2.436.276-3.748.277-1.312 0-2.624-.093-3.748-.277 0 1.475-1.125 2.95-3.156 3.026-1.37-.004-2.468-.077-1.636-3.072l1.122-4.027c1.045-2.934 2.545-2.934 2.92-2.934zM12 12.714c-.002.002-2.14 1.964-2.523 2.662l1.4-.056v1.22c0 .056.56.033 1.123.007.562.026 1.124.05 1.124-.008v-1.22l1.4.055C14.138 14.677 12 12.713 12 12.713z",
392    },
393];
394
395/// Todd Griffin's web presences, emitted as `og:see_also`: his site, then [`GODDTRIFFIN_SOCIALS`] in order.
396#[cfg(feature = "preset")]
397pub const GODDTRIFFIN_SEE_ALSO: [&str; GODDTRIFFIN_SOCIALS.len() + 1] = {
398    let mut see_also: [&str; GODDTRIFFIN_SOCIALS.len() + 1] =
399        ["https://www.toddgriffin.me/"; GODDTRIFFIN_SOCIALS.len() + 1];
400    let mut index: usize = 0;
401    while index < GODDTRIFFIN_SOCIALS.len() {
402        see_also[index + 1] = GODDTRIFFIN_SOCIALS[index].href;
403        index += 1;
404    }
405    see_also
406};
407
408/// Everything [`BaseTemplateData::goddtriffin`] cannot know for you.
409///
410/// Absent on purpose: the fields the preset fills in — author, language,
411/// region, handle, `site_entity` and `see_also` — and `social_image_alt`, which
412/// is derived as `"{project}: {description}"`. Every one of them can still be
413/// changed on the returned value with a `with_*`, `replace_*` or `extend_*`
414/// method.
415#[cfg(feature = "preset")]
416#[derive(Debug, Clone, PartialEq, Eq)]
417pub struct GoddtriffinParams {
418    /// The site's name.
419    pub project: String,
420    /// The default description.
421    pub description: String,
422    /// The site's origin, without a trailing slash.
423    pub base_url: String,
424    /// The default social card image.
425    pub social_image: String,
426    /// The browser-chrome colour, and the schemes this site supports.
427    pub theme_color: ThemeColor,
428    /// Which theme to stamp when nothing states a preference.
429    pub theme_fallback: Fallback,
430    /// The first year of the copyright range.
431    pub copyright_start: String,
432    /// Stylesheets every page loads.
433    pub style_sheets: Vec<String>,
434    /// Scripts every page loads.
435    pub scripts: Vec<String>,
436}
437
438#[cfg(feature = "preset")]
439impl BaseTemplateData {
440    /// Base data pre-filled with Todd Everett Griffin's defaults.
441    ///
442    /// Fills in author, language, region, Twitter/X handle,
443    /// [`GODDTRIFFIN_SEE_ALSO`] and a `Person` site entity, and derives
444    /// `social_image_alt` as `"{project}: {description}"`. Everything else is
445    /// required, because it differs per project.
446    ///
447    /// A project that is not *him* — a band, a business — should call
448    /// [`with_site_entity`](Self::with_site_entity) with an
449    /// [`Organization`](SiteEntity::organization), because the entity node is a
450    /// factual claim search engines reconcile against.
451    ///
452    /// To change what the preset filled in, call
453    /// [`replace_see_also`](Self::replace_see_also),
454    /// [`extend_see_also`](Self::extend_see_also), or one of the `with_*`
455    /// methods on the returned value.
456    #[must_use]
457    pub fn goddtriffin(params: GoddtriffinParams) -> Self {
458        let social_image_alt: String = format!("{}: {}", params.project, params.description);
459
460        Self::new(BaseTemplateDataParams {
461            project: params.project,
462            description: params.description,
463            author: String::from("Todd Everett Griffin"),
464            base_url: params.base_url,
465            twitter_username: String::from("goddtriffin"),
466            social_image: params.social_image,
467            social_image_alt,
468            theme_color: params.theme_color,
469            theme_fallback: params.theme_fallback,
470            site_entity: SiteEntity::person("Todd Everett Griffin"),
471            language_code: String::from("en"),
472            country_code: String::from("US"),
473            see_also: GODDTRIFFIN_SEE_ALSO
474                .iter()
475                .map(|url| (*url).to_string())
476                .collect(),
477            copyright_start: params.copyright_start,
478            style_sheets: params.style_sheets,
479            scripts: params.scripts,
480        })
481    }
482}
483
484#[cfg(test)]
485mod tests {
486    use super::{
487        BaseTemplateData, BaseTemplateDataParams, Fallback, SiteEntity, SmartAppBanner, ThemeColor,
488    };
489
490    fn params() -> BaseTemplateDataParams {
491        BaseTemplateDataParams {
492            project: String::from("Test Project"),
493            description: String::from("A test."),
494            author: String::from("Someone"),
495            base_url: String::from("https://www.example.com"),
496            twitter_username: String::from("someone"),
497            social_image: String::from("static/social.webp"),
498            social_image_alt: String::from("A test image."),
499            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
500            theme_fallback: Fallback::Dark,
501            site_entity: SiteEntity::person("Someone"),
502            language_code: String::from("en"),
503            country_code: String::from("US"),
504            see_also: vec![String::from("https://www.example.com/")],
505            copyright_start: String::from("1998"),
506            style_sheets: vec![String::from("static/stylesheet/main.css")],
507            scripts: vec![String::from("static/script/main.js")],
508        }
509    }
510
511    #[test]
512    fn a_trailing_slash_is_stripped_so_urls_never_double_up() {
513        let mut with_slash: BaseTemplateDataParams = params();
514        with_slash.base_url = String::from("https://www.example.com/");
515
516        let expected: String = String::from("https://www.example.com");
517        let actual: String = BaseTemplateData::new(with_slash).base_url().to_string();
518        assert_eq!(expected, actual);
519    }
520
521    #[test]
522    fn a_leading_slash_on_the_social_image_is_stripped_so_it_can_be_cache_busted() {
523        let mut with_slash: BaseTemplateDataParams = params();
524        with_slash.social_image = String::from("/static/image/social/card.webp");
525
526        // The cache-buster map is keyed without a leading slash, exactly like
527        // `style_sheets` and `scripts`; a mismatch here is why the social card
528        // was historically the one asset that never got hashed.
529        let expected: String = String::from("static/image/social/card.webp");
530        let actual: String = BaseTemplateData::new(with_slash).social_image().to_string();
531        assert_eq!(expected, actual);
532    }
533
534    #[test]
535    fn every_field_survives_construction() {
536        let expected: BaseTemplateDataParams = params();
537        let actual: BaseTemplateData = BaseTemplateData::new(params());
538
539        assert_eq!(expected.project, actual.project());
540        assert_eq!(expected.description, actual.description());
541        assert_eq!(expected.author, actual.author());
542        assert_eq!(expected.base_url, actual.base_url());
543        assert_eq!(expected.twitter_username, actual.twitter_username());
544        assert_eq!(expected.social_image, actual.social_image());
545        assert_eq!(expected.social_image_alt, actual.social_image_alt());
546        assert_eq!(&expected.theme_color, actual.theme_color());
547        assert_eq!(expected.theme_fallback, actual.theme_fallback);
548        assert_eq!(&expected.site_entity, actual.site_entity());
549        assert_eq!(expected.language_code, actual.language_code());
550        assert_eq!(expected.country_code, actual.country_code());
551        assert_eq!(expected.see_also, actual.see_also());
552        assert_eq!(expected.copyright_start, actual.copyright_start());
553        assert_eq!(expected.style_sheets, actual.style_sheets());
554        assert_eq!(expected.scripts, actual.scripts());
555    }
556
557    #[test]
558    fn the_smart_app_banner_is_absent_until_declared() {
559        let expected: Option<SmartAppBanner> = None;
560        let actual: Option<SmartAppBanner> =
561            BaseTemplateData::new(params()).smart_app_banner().cloned();
562        assert_eq!(expected, actual);
563
564        let expected: Option<SmartAppBanner> = Some(SmartAppBanner::new("42"));
565        let actual: Option<SmartAppBanner> = BaseTemplateData::new(params())
566            .with_smart_app_banner(SmartAppBanner::new("42"))
567            .smart_app_banner()
568            .cloned();
569        assert_eq!(expected, actual);
570    }
571
572    #[test]
573    fn extend_appends_without_discarding() {
574        let base: BaseTemplateData = BaseTemplateData::new(params())
575            .extend_see_also(["https://www.youtube.com/@tripleentendreband"])
576            .extend_scripts(["static/script/home.js"]);
577
578        let expected_see_also: Vec<String> = vec![
579            String::from("https://www.example.com/"),
580            String::from("https://www.youtube.com/@tripleentendreband"),
581        ];
582        let actual_see_also: Vec<String> = base.see_also().to_vec();
583        assert_eq!(expected_see_also, actual_see_also);
584
585        let expected_scripts: Vec<String> = vec![
586            String::from("static/script/main.js"),
587            String::from("static/script/home.js"),
588        ];
589        let actual_scripts: Vec<String> = base.scripts().to_vec();
590        assert_eq!(expected_scripts, actual_scripts);
591    }
592
593    #[cfg(feature = "preset")]
594    #[test]
595    fn the_preset_fills_identity_and_leaves_the_rest_required() {
596        use super::{GODDTRIFFIN_SEE_ALSO, GoddtriffinParams};
597
598        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
599            project: String::from("Boggledygook"),
600            description: String::from("Boggle, exhaustively."),
601            base_url: String::from("https://www.boggledygook.com"),
602            social_image: String::from("/og-image.png"),
603            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
604            theme_fallback: Fallback::Dark,
605            copyright_start: String::from("2025"),
606            style_sheets: vec![],
607            scripts: vec![],
608        });
609
610        let expected_author: String = String::from("Todd Everett Griffin");
611        let actual_author: String = base.author().to_string();
612        assert_eq!(expected_author, actual_author);
613
614        let expected_handle: String = String::from("goddtriffin");
615        let actual_handle: String = base.twitter_username().to_string();
616        assert_eq!(expected_handle, actual_handle);
617
618        let expected_entity: SiteEntity = SiteEntity::person("Todd Everett Griffin");
619        assert_eq!(&expected_entity, base.site_entity());
620
621        let expected_see_also: Vec<String> = GODDTRIFFIN_SEE_ALSO
622            .iter()
623            .map(|url| (*url).to_string())
624            .collect();
625        let actual_see_also: Vec<String> = base.see_also().to_vec();
626        assert_eq!(expected_see_also, actual_see_also);
627    }
628
629    #[cfg(feature = "preset")]
630    #[test]
631    fn the_socials_are_in_todds_order_and_each_has_an_icon() {
632        use super::GODDTRIFFIN_SOCIALS;
633
634        let expected: [&str; 8] = [
635            "X",
636            "LinkedIn",
637            "GitHub",
638            "YouTube",
639            "Instagram",
640            "Facebook",
641            "Reddit",
642            "itch.io",
643        ];
644        let actual: Vec<&str> = GODDTRIFFIN_SOCIALS
645            .iter()
646            .map(|social| social.label)
647            .collect();
648        assert_eq!(expected.to_vec(), actual);
649        assert!(
650            GODDTRIFFIN_SOCIALS
651                .iter()
652                .all(|social| social.icon.starts_with('M') && social.href.starts_with("https://")),
653            "every entry draws an SVG path and links over HTTPS"
654        );
655    }
656
657    #[cfg(feature = "preset")]
658    #[test]
659    fn the_preset_see_also_is_the_site_then_the_socials_in_order() {
660        use super::GODDTRIFFIN_SEE_ALSO;
661
662        let expected: [&str; 9] = [
663            "https://www.toddgriffin.me/",
664            "https://x.com/goddtriffin",
665            "https://www.linkedin.com/in/todd-griffin/",
666            "https://github.com/goddtriffin",
667            "https://www.youtube.com/@goddtriffin",
668            "https://www.instagram.com/goddtriffin/",
669            "https://www.facebook.com/goddtriffin/",
670            "https://www.reddit.com/user/goddtriffin",
671            "https://goddtriffin.itch.io/",
672        ];
673        assert_eq!(expected, GODDTRIFFIN_SEE_ALSO);
674        assert!(
675            !GODDTRIFFIN_SEE_ALSO
676                .iter()
677                .any(|url| url.contains("twitter.com") || url.contains("stackoverflow.com")),
678            "twitter.com was replaced by x.com; Stack Overflow is dropped"
679        );
680    }
681
682    #[cfg(feature = "preset")]
683    #[test]
684    fn a_preset_field_can_still_be_overridden() {
685        use super::{GODDTRIFFIN_SEE_ALSO, GoddtriffinParams};
686
687        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
688            project: String::from("Triple Entendre"),
689            description: String::from("A band."),
690            base_url: String::from("https://www.tripleentendreband.com"),
691            social_image: String::from("/static/social.webp"),
692            theme_color: ThemeColor::dark("#000000"),
693            theme_fallback: Fallback::Dark,
694            copyright_start: String::from("2019"),
695            style_sheets: vec![],
696            scripts: vec![],
697        })
698        .with_twitter_username("tripleentendre")
699        .extend_see_also(["https://www.youtube.com/@tripleentendreband"]);
700
701        let expected_handle: String = String::from("tripleentendre");
702        let actual_handle: String = base.twitter_username().to_string();
703        assert_eq!(expected_handle, actual_handle);
704
705        let expected_len: usize = GODDTRIFFIN_SEE_ALSO.len() + 1;
706        let actual_len: usize = base.see_also().len();
707        assert_eq!(expected_len, actual_len);
708    }
709
710    #[cfg(feature = "preset")]
711    #[test]
712    fn the_preset_derives_social_image_alt_from_project_and_description() {
713        use super::GoddtriffinParams;
714
715        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
716            project: String::from("Scannable Codes"),
717            description: String::from("Every scannable code, explained."),
718            base_url: String::from("https://www.scannablecodes.com"),
719            social_image: String::from("/static/social.webp"),
720            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
721            theme_fallback: Fallback::Dark,
722            copyright_start: String::from("1998"),
723            style_sheets: vec![],
724            scripts: vec![],
725        });
726
727        let expected: String = String::from("Scannable Codes: Every scannable code, explained.");
728        let actual: String = base.social_image_alt().to_string();
729        assert_eq!(expected, actual);
730    }
731
732    #[test]
733    fn replace_see_also_discards_whatever_was_there() {
734        let base: BaseTemplateData = BaseTemplateData::new(params())
735            .replace_see_also(["https://www.youtube.com/@tripleentendreband"]);
736
737        let expected: Vec<String> =
738            vec![String::from("https://www.youtube.com/@tripleentendreband")];
739        let actual: Vec<String> = base.see_also().to_vec();
740        assert_eq!(expected, actual);
741    }
742
743    #[test]
744    fn replace_then_extend_composes_in_that_order() {
745        let base: BaseTemplateData = BaseTemplateData::new(params())
746            .replace_see_also(["https://a.example.com"])
747            .extend_see_also(["https://b.example.com"]);
748
749        let expected: Vec<String> = vec![
750            String::from("https://a.example.com"),
751            String::from("https://b.example.com"),
752        ];
753        let actual: Vec<String> = base.see_also().to_vec();
754        assert_eq!(expected, actual);
755    }
756}