webserver-base 0.2.4

A Rust library which contains shared logic for all of my webserver projects.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Per-render template data: what this page is, this time.

use chrono::{DateTime, Utc};
use serde::Serialize;
use serde_json::Value;

use super::robots;

/// Article metadata, for a page that is a piece of writing rather than a part
/// of the site.
///
/// Supplying this is what makes a page an `article`: `og:type` is derived from
/// its presence, so a page cannot claim to be an article without dates, nor
/// carry dates that never reach the markup.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Article {
    /// When the piece was first published.
    pub published_time: DateTime<Utc>,
    /// When it was last meaningfully revised, if ever.
    pub modified_time: Option<DateTime<Utc>>,
    /// The section it belongs to, e.g. `Engineering`.
    pub section: Option<String>,
    /// Free-form tags.
    pub tags: Vec<String>,
}

impl Article {
    /// An article published at `published_time`, with nothing else set.
    #[must_use]
    pub const fn new(published_time: DateTime<Utc>) -> Self {
        Self {
            published_time,
            modified_time: None,
            section: None,
            tags: Vec::new(),
        }
    }

    /// Records a revision date.
    #[must_use]
    pub const fn with_modified_time(mut self, modified_time: DateTime<Utc>) -> Self {
        self.modified_time = Some(modified_time);
        self
    }

    /// Sets the section.
    #[must_use]
    pub fn with_section(mut self, section: impl Into<String>) -> Self {
        self.section = Some(section.into());
        self
    }

    /// Adds tags.
    #[must_use]
    pub fn extend_tags<I, S>(mut self, tags: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.tags.extend(tags.into_iter().map(Into::into));
        self
    }
}

/// Per-render template data.
///
/// Non-generic on purpose: the data a page displays is passed alongside this to
/// the render call rather than stored on it, so every builder method below is
/// type-preserving.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageTemplateData {
    template: String,
    page_name: String,
    page_url: String,

    description: Option<String>,
    social_image: Option<String>,
    social_image_alt: Option<String>,
    robots: Option<String>,
    jsonld: Option<Value>,
    article: Option<Article>,

    style_sheets: Option<Vec<String>>,
    scripts: Option<Vec<String>>,
    extra_style_sheets: Vec<String>,
    extra_scripts: Vec<String>,
}

impl PageTemplateData {
    /// Names a page.
    ///
    /// - `template` is the registered Handlebars name, e.g. `home`.
    /// - `page_name` is the human name, e.g. `Blog`. It becomes the `<title>`
    ///   via the computed `display_name`, which is the *only* way to influence
    ///   the title.
    /// - `page_url` is this page's path from the site root, with a leading
    ///   slash, e.g. `/blog`. It becomes the canonical URL.
    #[must_use]
    pub fn new(
        template: impl Into<String>,
        page_name: impl Into<String>,
        page_url: impl Into<String>,
    ) -> Self {
        Self {
            template: template.into(),
            page_name: page_name.into(),
            page_url: normalize_page_url(&page_url.into()),
            description: None,
            social_image: None,
            social_image_alt: None,
            robots: None,
            jsonld: None,
            article: None,
            style_sheets: None,
            scripts: None,
            extra_style_sheets: Vec::new(),
            extra_scripts: Vec::new(),
        }
    }

    /// Overrides the base description for this page.
    #[must_use]
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Overrides the base social card image for this page.
    #[must_use]
    pub fn with_social_image(mut self, social_image: impl Into<String>) -> Self {
        self.social_image = Some(social_image.into());
        self
    }

    /// Overrides the base social card alt text for this page.
    #[must_use]
    pub fn with_social_image_alt(mut self, social_image_alt: impl Into<String>) -> Self {
        self.social_image_alt = Some(social_image_alt.into());
        self
    }

    /// Overrides [`robots::DEFAULT`] for this page. See [`robots`] for the
    /// common directives.
    #[must_use]
    pub fn with_robots(mut self, robots: impl Into<String>) -> Self {
        self.robots = Some(robots.into());
        self
    }

    /// Attaches a schema.org JSON-LD document.
    ///
    /// Takes a [`Value`] rather than a pre-escaped string so the escaping cannot
    /// be skipped — a `</script>` inside a string value would otherwise break
    /// out of the block. `@context` is filled in when absent.
    #[must_use]
    pub fn with_jsonld(mut self, jsonld: Value) -> Self {
        self.jsonld = Some(jsonld);
        self
    }

    /// Marks this page as an article and attaches its metadata.
    ///
    /// This is the only way to set `og:type` to `article`, so the type and the
    /// `article:*` tags can never disagree.
    #[must_use]
    pub fn with_article(mut self, article: Article) -> Self {
        self.article = Some(article);
        self
    }

    /// Replaces the base stylesheet list wholesale for this page.
    #[must_use]
    pub fn replace_style_sheets<I, S>(mut self, style_sheets: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.style_sheets = Some(style_sheets.into_iter().map(Into::into).collect());
        self
    }

    /// Appends to whatever stylesheet list this page ends up with.
    #[must_use]
    pub fn extend_style_sheets<I, S>(mut self, style_sheets: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.extra_style_sheets
            .extend(style_sheets.into_iter().map(Into::into));
        self
    }

    /// Replaces the base script list wholesale for this page.
    #[must_use]
    pub fn replace_scripts<I, S>(mut self, scripts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.scripts = Some(scripts.into_iter().map(Into::into).collect());
        self
    }

    /// Appends to whatever script list this page ends up with.
    #[must_use]
    pub fn extend_scripts<I, S>(mut self, scripts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.extra_scripts
            .extend(scripts.into_iter().map(Into::into));
        self
    }

    /// The registered Handlebars template name.
    #[must_use]
    pub fn template(&self) -> &str {
        &self.template
    }
    /// The human page name.
    #[must_use]
    pub fn page_name(&self) -> &str {
        &self.page_name
    }
    /// This page's path from the site root.
    #[must_use]
    pub fn page_url(&self) -> &str {
        &self.page_url
    }
    /// This page's description override, if any.
    #[must_use]
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
    /// This page's social image override, if any.
    #[must_use]
    pub fn social_image(&self) -> Option<&str> {
        self.social_image.as_deref()
    }
    /// This page's social image alt override, if any.
    #[must_use]
    pub fn social_image_alt(&self) -> Option<&str> {
        self.social_image_alt.as_deref()
    }
    /// This page's robots directive, defaulting to [`robots::DEFAULT`].
    #[must_use]
    pub fn robots(&self) -> &str {
        self.robots.as_deref().unwrap_or(robots::DEFAULT)
    }
    /// This page's JSON-LD document, if any.
    #[must_use]
    pub const fn jsonld(&self) -> Option<&Value> {
        self.jsonld.as_ref()
    }
    /// This page's article metadata, if it is one.
    #[must_use]
    pub const fn article(&self) -> Option<&Article> {
        self.article.as_ref()
    }
    /// The Open Graph object type, derived from whether this page is an article.
    #[must_use]
    pub const fn og_type(&self) -> &'static str {
        if self.article.is_some() {
            "article"
        } else {
            "website"
        }
    }

    /// Every asset path this page declares for itself.
    ///
    /// Boot-time validation walks these so a typo in a page-specific stylesheet
    /// fails the deploy rather than silently 404ing for a visitor.
    #[must_use]
    pub fn declared_assets(&self) -> Vec<String> {
        let mut declared: Vec<String> = Vec::new();
        for list in [self.style_sheets.as_deref(), self.scripts.as_deref()]
            .into_iter()
            .flatten()
        {
            declared.extend(list.iter().cloned());
        }
        declared.extend(self.extra_style_sheets.iter().cloned());
        declared.extend(self.extra_scripts.iter().cloned());
        if let Some(social_image) = self.social_image.as_deref() {
            declared.push(social_image.to_string());
        }
        declared
    }

    /// Resolves the final stylesheet list: base, optionally replaced, then
    /// extended.
    #[must_use]
    pub fn resolve_style_sheets<'a>(&'a self, base: &'a [String]) -> Vec<&'a str> {
        resolve(self.style_sheets.as_deref(), base, &self.extra_style_sheets)
    }

    /// Resolves the final script list: base, optionally replaced, then
    /// extended.
    #[must_use]
    pub fn resolve_scripts<'a>(&'a self, base: &'a [String]) -> Vec<&'a str> {
        resolve(self.scripts.as_deref(), base, &self.extra_scripts)
    }
}

/// base → wholesale replacement (if any) → additions.
fn resolve<'a>(
    replacement: Option<&'a [String]>,
    base: &'a [String],
    additions: &'a [String],
) -> Vec<&'a str> {
    replacement
        .unwrap_or(base)
        .iter()
        .chain(additions.iter())
        .map(String::as_str)
        .collect()
}

/// Exactly one leading slash and no trailing one, so `base_url + page_url` is
/// always well-formed. The site root stays a bare `/`.
fn normalize_page_url(page_url: &str) -> String {
    let trimmed: &str = page_url.trim();
    if trimmed.is_empty() || trimmed == "/" {
        return String::from("/");
    }
    let without_trailing: &str = trimmed.trim_end_matches('/');
    if without_trailing.starts_with('/') {
        without_trailing.to_string()
    } else {
        format!("/{without_trailing}")
    }
}

#[cfg(test)]
mod tests {
    use chrono::{DateTime, TimeZone, Utc};
    use serde_json::json;

    use super::{Article, PageTemplateData, normalize_page_url};
    use crate::templates::robots;

    fn base_sheets() -> Vec<String> {
        vec![
            String::from("static/stylesheet/main.css"),
            String::from("static/stylesheet/theme.css"),
        ]
    }

    #[test]
    fn a_page_defaults_to_maximum_indexability() {
        let page: PageTemplateData = PageTemplateData::new("home", "Home", "/");

        let expected: String = String::from(robots::DEFAULT);
        let actual: String = page.robots().to_string();
        assert_eq!(expected, actual);
    }

    #[test]
    fn a_page_can_opt_out_of_indexing() {
        let page: PageTemplateData =
            PageTemplateData::new("404", "404", "/404").with_robots(robots::NOINDEX_FOLLOW);

        let expected: String = String::from("noindex, follow");
        let actual: String = page.robots().to_string();
        assert_eq!(expected, actual);
    }

    #[test]
    fn page_urls_are_normalized_to_one_leading_and_no_trailing_slash() {
        let cases: [(&str, &str); 6] = [
            ("/", "/"),
            ("", "/"),
            ("blog", "/blog"),
            ("/blog", "/blog"),
            ("/blog/", "/blog"),
            ("  /blog/tips/  ", "/blog/tips"),
        ];
        for (input, expected) in cases {
            let actual: String = normalize_page_url(input);
            assert_eq!(expected, actual, "input was {input:?}");
        }
    }

    #[test]
    fn assets_default_to_the_base_list() {
        let page: PageTemplateData = PageTemplateData::new("home", "Home", "/");

        let base: Vec<String> = base_sheets();
        let expected: Vec<&str> = vec!["static/stylesheet/main.css", "static/stylesheet/theme.css"];
        let actual: Vec<&str> = page.resolve_style_sheets(&base);
        assert_eq!(expected, actual);
    }

    #[test]
    fn extend_appends_to_the_base_list() {
        let page: PageTemplateData = PageTemplateData::new("home", "Home", "/")
            .extend_style_sheets(["static/stylesheet/home.css"]);

        let expected: Vec<&str> = vec![
            "static/stylesheet/main.css",
            "static/stylesheet/theme.css",
            "static/stylesheet/home.css",
        ];
        let base: Vec<String> = base_sheets();
        let actual: Vec<&str> = page.resolve_style_sheets(&base);
        assert_eq!(expected, actual);
    }

    #[test]
    fn replace_discards_the_base_list_then_extend_still_appends() {
        let page: PageTemplateData = PageTemplateData::new("play", "Play", "/play")
            .replace_style_sheets(["static/stylesheet/play.css"])
            .extend_style_sheets(["static/stylesheet/tiles.css"]);

        let base: Vec<String> = base_sheets();
        let expected: Vec<&str> = vec!["static/stylesheet/play.css", "static/stylesheet/tiles.css"];
        let actual: Vec<&str> = page.resolve_style_sheets(&base);
        assert_eq!(expected, actual);
    }

    #[test]
    fn overrides_are_absent_until_set() {
        let bare: PageTemplateData = PageTemplateData::new("home", "Home", "/");

        assert_eq!(None, bare.description());
        assert_eq!(None, bare.social_image());
        assert_eq!(None, bare.social_image_alt());
        assert_eq!(None, bare.jsonld());
        assert_eq!(None, bare.article());

        let overridden: PageTemplateData = bare
            .with_description("A specific page.")
            .with_social_image("/static/image/social/blog.webp")
            .with_social_image_alt("The blog card")
            .with_jsonld(json!({ "@type": "BlogPosting" }));

        assert_eq!(Some("A specific page."), overridden.description());
        assert_eq!(
            Some("/static/image/social/blog.webp"),
            overridden.social_image()
        );
        assert_eq!(Some("The blog card"), overridden.social_image_alt());
        assert!(overridden.jsonld().is_some());
    }

    #[test]
    fn a_page_is_a_website_until_it_is_given_article_metadata() {
        let page: PageTemplateData = PageTemplateData::new("home", "Home", "/");

        let expected: &str = "website";
        let actual: &str = page.og_type();
        assert_eq!(expected, actual);
    }

    #[test]
    fn attaching_article_metadata_is_what_makes_the_og_type_an_article() {
        let published: DateTime<Utc> = Utc.with_ymd_and_hms(2026, 1, 15, 0, 0, 0).unwrap();
        let page: PageTemplateData = PageTemplateData::new("blog-post", "A Post", "/blog/a-post")
            .with_article(
                Article::new(published)
                    .with_section("Engineering")
                    .extend_tags(["rust", "axum"]),
            );

        let expected: &str = "article";
        let actual: &str = page.og_type();
        assert_eq!(expected, actual);

        let article: &Article = page.article().expect("set above");
        assert_eq!(published, article.published_time);
        assert_eq!(None, article.modified_time);
        assert_eq!(Some(String::from("Engineering")), article.section);
        assert_eq!(
            vec![String::from("rust"), String::from("axum")],
            article.tags
        );
    }
}