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::theme::{Fallback, ThemeColor, ThemeScript};
8
9/// Everything [`BaseTemplateData::new`] requires.
10///
11/// A named-field struct rather than sixteen positional arguments: every field
12/// is mandatory either way, but this one cannot be built with two of them
13/// transposed.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct BaseTemplateDataParams {
16    /// The site's name, e.g. `Boggledygook`.
17    pub project: String,
18    /// The default `<meta name="description">`.
19    pub description: String,
20    /// Who wrote it.
21    pub author: String,
22    /// The site's origin, without a trailing slash — `https://www.example.com`.
23    pub base_url: String,
24    /// The Twitter/X handle, without the leading `@`.
25    pub twitter_username: String,
26    /// The default Open Graph / Twitter card image.
27    pub social_image: String,
28    /// The default alt text for that image.
29    pub social_image_alt: String,
30    /// The browser-chrome colour, and the schemes this site supports.
31    pub theme_color: ThemeColor,
32    /// Which theme to stamp when neither the reader nor their OS states a
33    /// preference. Design data, so it lives beside the colours it chooses
34    /// between.
35    pub theme_fallback: Fallback,
36    /// Who or what the site represents, for schema.org.
37    pub site_entity: SiteEntity,
38    /// The `<html lang>` language subtag, e.g. `en`.
39    pub language_code: String,
40    /// The region subtag, e.g. `US`.
41    pub country_code: String,
42    /// Other places this author exists, emitted as `og:see_also`.
43    pub see_also: Vec<String>,
44    /// The first year of the copyright range. The last is computed per render.
45    pub copyright_start: String,
46    /// Stylesheets every page loads.
47    pub style_sheets: Vec<String>,
48    /// Scripts every page loads.
49    pub scripts: Vec<String>,
50}
51
52/// Per-server template data.
53///
54/// Built once at boot and held in the server's state. Everything on it can be
55/// overridden for a single page by [`PageTemplateData`](super::PageTemplateData),
56/// except the fields that identify the site itself.
57#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
58pub struct BaseTemplateData {
59    project: String,
60    description: String,
61    author: String,
62    base_url: String,
63    twitter_username: String,
64    social_image: String,
65    social_image_alt: String,
66    theme_color: ThemeColor,
67    theme_fallback: Fallback,
68    site_entity: SiteEntity,
69    language_code: String,
70    country_code: String,
71    see_also: Vec<String>,
72    copyright_start: String,
73    style_sheets: Vec<String>,
74    scripts: Vec<String>,
75    #[serde(default)]
76    site: Value,
77}
78
79impl BaseTemplateData {
80    /// Builds base data with every field supplied.
81    ///
82    /// Two paths are normalised on the way in. A trailing slash on `base_url`
83    /// is stripped, so joining it with a page URL cannot produce
84    /// `https://example.com//blog`. A *leading* slash on `social_image` is
85    /// stripped, so it keys into the cache-buster map exactly like the
86    /// stylesheet and script paths beside it — without that, the social card is
87    /// the one asset on the site that can never be content-hashed.
88    #[must_use]
89    pub fn new(params: BaseTemplateDataParams) -> Self {
90        Self {
91            project: params.project,
92            description: params.description,
93            author: params.author,
94            base_url: normalize_base_url(&params.base_url),
95            twitter_username: params.twitter_username,
96            social_image: normalize_asset_path(&params.social_image),
97            social_image_alt: params.social_image_alt,
98            theme_color: params.theme_color,
99            theme_fallback: params.theme_fallback,
100            site_entity: params.site_entity,
101            language_code: params.language_code,
102            country_code: params.country_code,
103            see_also: params.see_also,
104            copyright_start: params.copyright_start,
105            style_sheets: params.style_sheets,
106            scripts: params.scripts,
107            site: Value::Null,
108        }
109    }
110
111    /// The project's own site-wide data, reachable on every page — the 404
112    /// included — as `{{site.…}}`.
113    ///
114    /// For what every page shares but the library has no field for: a nav
115    /// tree, a phone number, opening hours. Supplying it once here is what
116    /// lets a partial read it on a page whose `{{app}}` knows nothing about it.
117    ///
118    /// # Panics
119    ///
120    /// If `site` cannot be serialized — at boot, never per request.
121    #[must_use]
122    pub fn with_site<A>(mut self, site: A) -> Self
123    where
124        A: Serialize,
125    {
126        self.site = serde_json::to_value(site)
127            .unwrap_or_else(|error| panic!("the site data is unserializable: {error}"));
128        self
129    }
130
131    /// Overrides the author.
132    #[must_use]
133    pub fn with_author(mut self, author: impl Into<String>) -> Self {
134        self.author = author.into();
135        self
136    }
137
138    /// Overrides the Twitter/X handle.
139    #[must_use]
140    pub fn with_twitter_username(mut self, twitter_username: impl Into<String>) -> Self {
141        self.twitter_username = twitter_username.into();
142        self
143    }
144
145    /// Overrides who or what the site represents.
146    #[must_use]
147    pub fn with_site_entity(mut self, site_entity: SiteEntity) -> Self {
148        self.site_entity = site_entity;
149        self
150    }
151
152    /// Overrides the language subtag.
153    #[must_use]
154    pub fn with_language_code(mut self, language_code: impl Into<String>) -> Self {
155        self.language_code = language_code.into();
156        self
157    }
158
159    /// Overrides the region subtag.
160    #[must_use]
161    pub fn with_country_code(mut self, country_code: impl Into<String>) -> Self {
162        self.country_code = country_code.into();
163        self
164    }
165
166    /// Replaces the `og:see_also` list wholesale.
167    ///
168    /// For a project that should not carry the author's personal links at all.
169    #[must_use]
170    pub fn replace_see_also<I, S>(mut self, see_also: I) -> Self
171    where
172        I: IntoIterator<Item = S>,
173        S: Into<String>,
174    {
175        self.see_also = see_also.into_iter().map(Into::into).collect();
176        self
177    }
178
179    /// Appends to the `og:see_also` list.
180    ///
181    /// For a project with its own presence alongside the author's — a band's
182    /// `YouTube` channel next to a personal one.
183    #[must_use]
184    pub fn extend_see_also<I, S>(mut self, see_also: I) -> Self
185    where
186        I: IntoIterator<Item = S>,
187        S: Into<String>,
188    {
189        self.see_also.extend(see_also.into_iter().map(Into::into));
190        self
191    }
192
193    /// Appends to the site-wide stylesheet list.
194    #[must_use]
195    pub fn extend_style_sheets<I, S>(mut self, style_sheets: I) -> Self
196    where
197        I: IntoIterator<Item = S>,
198        S: Into<String>,
199    {
200        self.style_sheets
201            .extend(style_sheets.into_iter().map(Into::into));
202        self
203    }
204
205    /// Appends to the site-wide script list.
206    #[must_use]
207    pub fn extend_scripts<I, S>(mut self, scripts: I) -> Self
208    where
209        I: IntoIterator<Item = S>,
210        S: Into<String>,
211    {
212        self.scripts.extend(scripts.into_iter().map(Into::into));
213        self
214    }
215
216    /// The project's site-wide data; `null` unless
217    /// [`with_site`](Self::with_site) was called.
218    #[must_use]
219    pub const fn site(&self) -> &Value {
220        &self.site
221    }
222
223    /// The site's name.
224    #[must_use]
225    pub fn project(&self) -> &str {
226        &self.project
227    }
228    /// The default description.
229    #[must_use]
230    pub fn description(&self) -> &str {
231        &self.description
232    }
233    /// The author.
234    #[must_use]
235    pub fn author(&self) -> &str {
236        &self.author
237    }
238    /// The site origin, with no trailing slash.
239    #[must_use]
240    pub fn base_url(&self) -> &str {
241        &self.base_url
242    }
243    /// The Twitter/X handle.
244    #[must_use]
245    pub fn twitter_username(&self) -> &str {
246        &self.twitter_username
247    }
248    /// The default social image.
249    #[must_use]
250    pub fn social_image(&self) -> &str {
251        &self.social_image
252    }
253    /// The default social image alt text.
254    #[must_use]
255    pub fn social_image_alt(&self) -> &str {
256        &self.social_image_alt
257    }
258    /// The browser-chrome colour and supported schemes.
259    #[must_use]
260    pub const fn theme_color(&self) -> &ThemeColor {
261        &self.theme_color
262    }
263    /// Who or what the site represents.
264    #[must_use]
265    pub const fn site_entity(&self) -> &SiteEntity {
266        &self.site_entity
267    }
268    /// The pre-paint theme script this site's design implies.
269    #[must_use]
270    pub const fn theme_script(&self) -> ThemeScript {
271        ThemeScript::new(self.theme_fallback)
272    }
273    /// The language subtag.
274    #[must_use]
275    pub fn language_code(&self) -> &str {
276        &self.language_code
277    }
278    /// The region subtag.
279    #[must_use]
280    pub fn country_code(&self) -> &str {
281        &self.country_code
282    }
283    /// The `og:see_also` list.
284    #[must_use]
285    pub fn see_also(&self) -> &[String] {
286        &self.see_also
287    }
288    /// The first year of the copyright range.
289    #[must_use]
290    pub fn copyright_start(&self) -> &str {
291        &self.copyright_start
292    }
293    /// The site-wide stylesheets.
294    #[must_use]
295    pub fn style_sheets(&self) -> &[String] {
296        &self.style_sheets
297    }
298    /// The site-wide scripts.
299    #[must_use]
300    pub fn scripts(&self) -> &[String] {
301        &self.scripts
302    }
303}
304
305/// Strips any trailing slashes from a site origin.
306fn normalize_base_url(base_url: &str) -> String {
307    base_url.trim().trim_end_matches('/').to_string()
308}
309
310/// Strips a leading slash, so an asset path matches its cache-buster key.
311fn normalize_asset_path(path: &str) -> String {
312    path.trim().trim_start_matches('/').to_string()
313}
314
315// ─────────────────────────────────────────────────────────────────────────────
316// preset
317// ─────────────────────────────────────────────────────────────────────────────
318
319/// One of Todd Griffin's social profiles: what a project's icon row renders.
320#[cfg(feature = "preset")]
321#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
322pub struct Social {
323    /// The network's name, for `aria-label` and `title`.
324    pub label: &'static str,
325    /// The profile URL.
326    pub href: &'static str,
327    /// SVG path data for a `0 0 24 24` view box, drawn in `currentColor` (Simple Icons 16.36.0, CC0).
328    pub icon: &'static str,
329}
330
331/// Todd Griffin's social profiles, in the order every project shows them. The single source for both
332/// [`GODDTRIFFIN_SEE_ALSO`] and a site's icon row: add, drop or reorder a network here only.
333#[cfg(feature = "preset")]
334pub const GODDTRIFFIN_SOCIALS: [Social; 8] = [
335    Social {
336        label: "X",
337        href: "https://x.com/goddtriffin",
338        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",
339    },
340    Social {
341        label: "LinkedIn",
342        href: "https://www.linkedin.com/in/todd-griffin/",
343        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",
344    },
345    Social {
346        label: "GitHub",
347        href: "https://github.com/goddtriffin",
348        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",
349    },
350    Social {
351        label: "YouTube",
352        href: "https://www.youtube.com/@goddtriffin",
353        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",
354    },
355    Social {
356        label: "Instagram",
357        href: "https://www.instagram.com/goddtriffin/",
358        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",
359    },
360    Social {
361        label: "Facebook",
362        href: "https://www.facebook.com/goddtriffin/",
363        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",
364    },
365    Social {
366        label: "Reddit",
367        href: "https://www.reddit.com/user/goddtriffin",
368        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",
369    },
370    Social {
371        label: "itch.io",
372        href: "https://goddtriffin.itch.io/",
373        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",
374    },
375];
376
377/// Todd Griffin's web presences, emitted as `og:see_also`: his site, then [`GODDTRIFFIN_SOCIALS`] in order.
378#[cfg(feature = "preset")]
379pub const GODDTRIFFIN_SEE_ALSO: [&str; GODDTRIFFIN_SOCIALS.len() + 1] = {
380    let mut see_also: [&str; GODDTRIFFIN_SOCIALS.len() + 1] =
381        ["https://www.toddgriffin.me/"; GODDTRIFFIN_SOCIALS.len() + 1];
382    let mut index: usize = 0;
383    while index < GODDTRIFFIN_SOCIALS.len() {
384        see_also[index + 1] = GODDTRIFFIN_SOCIALS[index].href;
385        index += 1;
386    }
387    see_also
388};
389
390/// Everything [`BaseTemplateData::goddtriffin`] cannot know for you.
391///
392/// Absent on purpose: the fields the preset fills in — author, language,
393/// region, handle, `site_entity` and `see_also` — and `social_image_alt`, which
394/// is derived as `"{project}: {description}"`. Every one of them can still be
395/// changed on the returned value with a `with_*`, `replace_*` or `extend_*`
396/// method.
397#[cfg(feature = "preset")]
398#[derive(Debug, Clone, PartialEq, Eq)]
399pub struct GoddtriffinParams {
400    /// The site's name.
401    pub project: String,
402    /// The default description.
403    pub description: String,
404    /// The site's origin, without a trailing slash.
405    pub base_url: String,
406    /// The default social card image.
407    pub social_image: String,
408    /// The browser-chrome colour, and the schemes this site supports.
409    pub theme_color: ThemeColor,
410    /// Which theme to stamp when nothing states a preference.
411    pub theme_fallback: Fallback,
412    /// The first year of the copyright range.
413    pub copyright_start: String,
414    /// Stylesheets every page loads.
415    pub style_sheets: Vec<String>,
416    /// Scripts every page loads.
417    pub scripts: Vec<String>,
418}
419
420#[cfg(feature = "preset")]
421impl BaseTemplateData {
422    /// Base data pre-filled with Todd Everett Griffin's defaults.
423    ///
424    /// Fills in author, language, region, Twitter/X handle,
425    /// [`GODDTRIFFIN_SEE_ALSO`] and a `Person` site entity, and derives
426    /// `social_image_alt` as `"{project}: {description}"`. Everything else is
427    /// required, because it differs per project.
428    ///
429    /// A project that is not *him* — a band, a business — should call
430    /// [`with_site_entity`](Self::with_site_entity) with an
431    /// [`Organization`](SiteEntity::organization), because the entity node is a
432    /// factual claim search engines reconcile against.
433    ///
434    /// To change what the preset filled in, call
435    /// [`replace_see_also`](Self::replace_see_also),
436    /// [`extend_see_also`](Self::extend_see_also), or one of the `with_*`
437    /// methods on the returned value.
438    #[must_use]
439    pub fn goddtriffin(params: GoddtriffinParams) -> Self {
440        let social_image_alt: String = format!("{}: {}", params.project, params.description);
441
442        Self::new(BaseTemplateDataParams {
443            project: params.project,
444            description: params.description,
445            author: String::from("Todd Everett Griffin"),
446            base_url: params.base_url,
447            twitter_username: String::from("goddtriffin"),
448            social_image: params.social_image,
449            social_image_alt,
450            theme_color: params.theme_color,
451            theme_fallback: params.theme_fallback,
452            site_entity: SiteEntity::person("Todd Everett Griffin"),
453            language_code: String::from("en"),
454            country_code: String::from("US"),
455            see_also: GODDTRIFFIN_SEE_ALSO
456                .iter()
457                .map(|url| (*url).to_string())
458                .collect(),
459            copyright_start: params.copyright_start,
460            style_sheets: params.style_sheets,
461            scripts: params.scripts,
462        })
463    }
464}
465
466#[cfg(test)]
467mod tests {
468    use super::{BaseTemplateData, BaseTemplateDataParams, Fallback, SiteEntity, ThemeColor};
469
470    fn params() -> BaseTemplateDataParams {
471        BaseTemplateDataParams {
472            project: String::from("Test Project"),
473            description: String::from("A test."),
474            author: String::from("Someone"),
475            base_url: String::from("https://www.example.com"),
476            twitter_username: String::from("someone"),
477            social_image: String::from("static/social.webp"),
478            social_image_alt: String::from("A test image."),
479            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
480            theme_fallback: Fallback::Dark,
481            site_entity: SiteEntity::person("Someone"),
482            language_code: String::from("en"),
483            country_code: String::from("US"),
484            see_also: vec![String::from("https://www.example.com/")],
485            copyright_start: String::from("1998"),
486            style_sheets: vec![String::from("static/stylesheet/main.css")],
487            scripts: vec![String::from("static/script/main.js")],
488        }
489    }
490
491    #[test]
492    fn a_trailing_slash_is_stripped_so_urls_never_double_up() {
493        let mut with_slash: BaseTemplateDataParams = params();
494        with_slash.base_url = String::from("https://www.example.com/");
495
496        let expected: String = String::from("https://www.example.com");
497        let actual: String = BaseTemplateData::new(with_slash).base_url().to_string();
498        assert_eq!(expected, actual);
499    }
500
501    #[test]
502    fn a_leading_slash_on_the_social_image_is_stripped_so_it_can_be_cache_busted() {
503        let mut with_slash: BaseTemplateDataParams = params();
504        with_slash.social_image = String::from("/static/image/social/card.webp");
505
506        // The cache-buster map is keyed without a leading slash, exactly like
507        // `style_sheets` and `scripts`; a mismatch here is why the social card
508        // was historically the one asset that never got hashed.
509        let expected: String = String::from("static/image/social/card.webp");
510        let actual: String = BaseTemplateData::new(with_slash).social_image().to_string();
511        assert_eq!(expected, actual);
512    }
513
514    #[test]
515    fn every_field_survives_construction() {
516        let expected: BaseTemplateDataParams = params();
517        let actual: BaseTemplateData = BaseTemplateData::new(params());
518
519        assert_eq!(expected.project, actual.project());
520        assert_eq!(expected.description, actual.description());
521        assert_eq!(expected.author, actual.author());
522        assert_eq!(expected.base_url, actual.base_url());
523        assert_eq!(expected.twitter_username, actual.twitter_username());
524        assert_eq!(expected.social_image, actual.social_image());
525        assert_eq!(expected.social_image_alt, actual.social_image_alt());
526        assert_eq!(&expected.theme_color, actual.theme_color());
527        assert_eq!(expected.theme_fallback, actual.theme_fallback);
528        assert_eq!(&expected.site_entity, actual.site_entity());
529        assert_eq!(expected.language_code, actual.language_code());
530        assert_eq!(expected.country_code, actual.country_code());
531        assert_eq!(expected.see_also, actual.see_also());
532        assert_eq!(expected.copyright_start, actual.copyright_start());
533        assert_eq!(expected.style_sheets, actual.style_sheets());
534        assert_eq!(expected.scripts, actual.scripts());
535    }
536
537    #[test]
538    fn extend_appends_without_discarding() {
539        let base: BaseTemplateData = BaseTemplateData::new(params())
540            .extend_see_also(["https://www.youtube.com/@tripleentendreband"])
541            .extend_scripts(["static/script/home.js"]);
542
543        let expected_see_also: Vec<String> = vec![
544            String::from("https://www.example.com/"),
545            String::from("https://www.youtube.com/@tripleentendreband"),
546        ];
547        let actual_see_also: Vec<String> = base.see_also().to_vec();
548        assert_eq!(expected_see_also, actual_see_also);
549
550        let expected_scripts: Vec<String> = vec![
551            String::from("static/script/main.js"),
552            String::from("static/script/home.js"),
553        ];
554        let actual_scripts: Vec<String> = base.scripts().to_vec();
555        assert_eq!(expected_scripts, actual_scripts);
556    }
557
558    #[cfg(feature = "preset")]
559    #[test]
560    fn the_preset_fills_identity_and_leaves_the_rest_required() {
561        use super::{GODDTRIFFIN_SEE_ALSO, GoddtriffinParams};
562
563        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
564            project: String::from("Boggledygook"),
565            description: String::from("Boggle, exhaustively."),
566            base_url: String::from("https://www.boggledygook.com"),
567            social_image: String::from("/og-image.png"),
568            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
569            theme_fallback: Fallback::Dark,
570            copyright_start: String::from("2025"),
571            style_sheets: vec![],
572            scripts: vec![],
573        });
574
575        let expected_author: String = String::from("Todd Everett Griffin");
576        let actual_author: String = base.author().to_string();
577        assert_eq!(expected_author, actual_author);
578
579        let expected_handle: String = String::from("goddtriffin");
580        let actual_handle: String = base.twitter_username().to_string();
581        assert_eq!(expected_handle, actual_handle);
582
583        let expected_entity: SiteEntity = SiteEntity::person("Todd Everett Griffin");
584        assert_eq!(&expected_entity, base.site_entity());
585
586        let expected_see_also: Vec<String> = GODDTRIFFIN_SEE_ALSO
587            .iter()
588            .map(|url| (*url).to_string())
589            .collect();
590        let actual_see_also: Vec<String> = base.see_also().to_vec();
591        assert_eq!(expected_see_also, actual_see_also);
592    }
593
594    #[cfg(feature = "preset")]
595    #[test]
596    fn the_socials_are_in_todds_order_and_each_has_an_icon() {
597        use super::GODDTRIFFIN_SOCIALS;
598
599        let expected: [&str; 8] = [
600            "X",
601            "LinkedIn",
602            "GitHub",
603            "YouTube",
604            "Instagram",
605            "Facebook",
606            "Reddit",
607            "itch.io",
608        ];
609        let actual: Vec<&str> = GODDTRIFFIN_SOCIALS
610            .iter()
611            .map(|social| social.label)
612            .collect();
613        assert_eq!(expected.to_vec(), actual);
614        assert!(
615            GODDTRIFFIN_SOCIALS
616                .iter()
617                .all(|social| social.icon.starts_with('M') && social.href.starts_with("https://")),
618            "every entry draws an SVG path and links over HTTPS"
619        );
620    }
621
622    #[cfg(feature = "preset")]
623    #[test]
624    fn the_preset_see_also_is_the_site_then_the_socials_in_order() {
625        use super::GODDTRIFFIN_SEE_ALSO;
626
627        let expected: [&str; 9] = [
628            "https://www.toddgriffin.me/",
629            "https://x.com/goddtriffin",
630            "https://www.linkedin.com/in/todd-griffin/",
631            "https://github.com/goddtriffin",
632            "https://www.youtube.com/@goddtriffin",
633            "https://www.instagram.com/goddtriffin/",
634            "https://www.facebook.com/goddtriffin/",
635            "https://www.reddit.com/user/goddtriffin",
636            "https://goddtriffin.itch.io/",
637        ];
638        assert_eq!(expected, GODDTRIFFIN_SEE_ALSO);
639        assert!(
640            !GODDTRIFFIN_SEE_ALSO
641                .iter()
642                .any(|url| url.contains("twitter.com") || url.contains("stackoverflow.com")),
643            "twitter.com was replaced by x.com; Stack Overflow is dropped"
644        );
645    }
646
647    #[cfg(feature = "preset")]
648    #[test]
649    fn a_preset_field_can_still_be_overridden() {
650        use super::{GODDTRIFFIN_SEE_ALSO, GoddtriffinParams};
651
652        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
653            project: String::from("Triple Entendre"),
654            description: String::from("A band."),
655            base_url: String::from("https://www.tripleentendreband.com"),
656            social_image: String::from("/static/social.webp"),
657            theme_color: ThemeColor::dark("#000000"),
658            theme_fallback: Fallback::Dark,
659            copyright_start: String::from("2019"),
660            style_sheets: vec![],
661            scripts: vec![],
662        })
663        .with_twitter_username("tripleentendre")
664        .extend_see_also(["https://www.youtube.com/@tripleentendreband"]);
665
666        let expected_handle: String = String::from("tripleentendre");
667        let actual_handle: String = base.twitter_username().to_string();
668        assert_eq!(expected_handle, actual_handle);
669
670        let expected_len: usize = GODDTRIFFIN_SEE_ALSO.len() + 1;
671        let actual_len: usize = base.see_also().len();
672        assert_eq!(expected_len, actual_len);
673    }
674
675    #[cfg(feature = "preset")]
676    #[test]
677    fn the_preset_derives_social_image_alt_from_project_and_description() {
678        use super::GoddtriffinParams;
679
680        let base: BaseTemplateData = BaseTemplateData::goddtriffin(GoddtriffinParams {
681            project: String::from("Scannable Codes"),
682            description: String::from("Every scannable code, explained."),
683            base_url: String::from("https://www.scannablecodes.com"),
684            social_image: String::from("/static/social.webp"),
685            theme_color: ThemeColor::light_dark("#fafafa", "#121212"),
686            theme_fallback: Fallback::Dark,
687            copyright_start: String::from("1998"),
688            style_sheets: vec![],
689            scripts: vec![],
690        });
691
692        let expected: String = String::from("Scannable Codes: Every scannable code, explained.");
693        let actual: String = base.social_image_alt().to_string();
694        assert_eq!(expected, actual);
695    }
696
697    #[test]
698    fn replace_see_also_discards_whatever_was_there() {
699        let base: BaseTemplateData = BaseTemplateData::new(params())
700            .replace_see_also(["https://www.youtube.com/@tripleentendreband"]);
701
702        let expected: Vec<String> =
703            vec![String::from("https://www.youtube.com/@tripleentendreband")];
704        let actual: Vec<String> = base.see_also().to_vec();
705        assert_eq!(expected, actual);
706    }
707
708    #[test]
709    fn replace_then_extend_composes_in_that_order() {
710        let base: BaseTemplateData = BaseTemplateData::new(params())
711            .replace_see_also(["https://a.example.com"])
712            .extend_see_also(["https://b.example.com"]);
713
714        let expected: Vec<String> = vec![
715            String::from("https://a.example.com"),
716            String::from("https://b.example.com"),
717        ];
718        let actual: Vec<String> = base.see_also().to_vec();
719        assert_eq!(expected, actual);
720    }
721}