devpage-core 0.3.0

devpage's core: documents, rendering, link checking, comments and designs, with no I/O runtime
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Comments: threads readers leave on a page, kept by a comment store.
//! [`Comments`] is the only way a thread is written — from a page, from the
//! API, or by a publish carrying threads onto a new version — and it holds
//! every rule of a write: the page key, a new thread's page being a file in
//! the served or version folder, replies and resolves going by thread id,
//! where a carry moves each thread, and the one announcement each write
//! makes on the change bus. A carry is the one write it hands over: the
//! publish commits it together with the version, so both land or neither
//! does. [`Identity`] says who writes. The [`Store`] port is only where
//! threads are kept, by thread id: the YAML files under `.devpage/comments/`
//! ([`yaml`]), server mode's SQLite store, and memory for tests
//! ([`memory`]). [`anchor`] finds a carried thread's quote in the new
//! version's text.

pub mod anchor;
pub mod yaml;

#[cfg(any(test, feature = "testing"))]
pub mod memory;

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::changes::{Change, Publish};
use crate::folder::{Folder, ServedFolder};
use crate::widgets::{Modules, Widgets};

/// What a thread is attached to: rendered text, with a little context.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Anchor {
    /// The text the thread is attached to, as the page shows it.
    pub quote: String,
    /// The text just before `quote`, which tells repeats of it apart.
    #[serde(default)]
    pub prefix: String,
    /// The text just after `quote`, which tells repeats of it apart.
    #[serde(default)]
    pub suffix: String,
}

/// One message in a thread: who wrote it, when, and what.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Message {
    /// Who wrote it, as [`Identity::author`] named them.
    pub author: String,
    /// When it was written: RFC 3339 UTC, to the second.
    pub at: String,
    /// What it says, as written.
    pub text: String,
}

/// A thread as stored: its anchor and its messages, oldest first.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Thread {
    /// What the thread is attached to.
    pub anchor: Anchor,
    /// Its messages, oldest first.
    pub thread: Vec<Message>,
}

/// A thread with its id, as the page receives it.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Listed {
    /// The thread's id, which replies and resolves go by.
    pub id: String,
    /// The thread itself; its fields sit beside `id` on the wire.
    #[serde(flatten)]
    pub thread: Thread,
    /// Its text is gone from the version shown: the page lists it among the
    /// detached comments wherever its quote may still appear. Only a store
    /// that keeps threads across versions sets it.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub detached: bool,
}

/// Where a publish carries a document's threads: each one that moves, and
/// the pages they are on. Made by [`Comments::carry`].
#[derive(Debug, Default, PartialEq)]
pub struct Carry {
    /// Each thread that moves, once.
    moved: Vec<Moved>,
    /// The page key of each page a moved thread is on: what
    /// [`Comments::carried`] announces.
    pages: std::collections::BTreeSet<String>,
}

impl Carry {
    /// The threads that move, each once.
    pub fn moved(&self) -> &[Moved] {
        &self.moved
    }
}

/// A thread carried onto a new version: to `anchor`, or detached — `None` —
/// when its text is gone.
#[derive(Debug, PartialEq)]
pub struct Moved {
    /// The thread's id, the same on the new version.
    pub id: String,
    /// Its anchor in the new version; `None` when it is detached.
    pub anchor: Option<Anchor>,
}

/// The comment store port: where one served folder's — or one published
/// document's — threads are kept, by thread id. A `page` here is always a
/// page key, `/a.md`, made by [`Comments`]; an `id` is always one that
/// [`is_id`] accepts. Writes are silent — announcing them is [`Comments`]'s
/// job.
pub trait Store: Send + Sync {
    /// A page's threads, oldest first.
    fn list(&self, page: &str) -> Result<Vec<Listed>>;
    /// Starts a thread on `page` with its first message; answers its id.
    fn create(&self, page: &str, anchor: Anchor, first: Message) -> Result<String>;
    /// Appends `message` to thread `id`: the page it is on, or `None` when
    /// there is no such thread.
    fn reply(&self, id: &str, message: Message) -> Result<Option<String>>;
    /// Resolves thread `id` — it is gone: the page it was on, or `None` when
    /// there was no such thread.
    fn resolve(&self, id: &str) -> Result<Option<String>>;
    /// Every thread still attached, each with its page: what a publish
    /// carries onto the new version.
    fn attached(&self) -> Result<Vec<(String, Listed)>>;
    /// The pages whose threads changed among `paths` by a hand other than
    /// this store's — a thread file edited in an editor. Asking marks them
    /// heard: whoever asks announces them. A store with no files hears
    /// nothing.
    fn heard(&self, paths: &[PathBuf]) -> Vec<String>;
}

/// Why a comment request is refused.
#[derive(Debug)]
pub enum Refused {
    /// The request names no author: the header [`Identity::Header`] names
    /// is missing or blank.
    Unidentified(String),
    /// The page is not a file in the served or version folder.
    NotAPage(String),
    /// There is no such thread.
    NoThread(String),
    /// The store failed.
    Failed(anyhow::Error),
}

impl std::fmt::Display for Refused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Refused::Unidentified(header) => write!(f, "a write needs the {header} header"),
            Refused::NotAPage(page) => write!(f, "{page} is not a page in the served folder"),
            Refused::NoThread(id) => write!(f, "no thread {id}"),
            Refused::Failed(error) => write!(f, "{error:#}"),
        }
    }
}

impl From<anyhow::Error> for Refused {
    fn from(error: anyhow::Error) -> Self {
        Refused::Failed(error)
    }
}

/// Who writes: one local author — the git name locally, `anonymous` on a
/// server with no header — or, deployed, whoever the auth proxy names in a
/// header. devpage authenticates no one itself.
#[derive(Debug, Clone, PartialEq)]
pub enum Identity {
    /// One local author for every write: their name.
    Local(String),
    /// Whoever the auth proxy names in a request: the header's name.
    Header(String),
}

impl Identity {
    /// The author of a write whose request carries `header`'s value by name:
    /// the local author, or the header's value trimmed — refused when it is
    /// missing or blank.
    pub fn author<'a>(
        &self,
        header: impl FnOnce(&str) -> Option<&'a str>,
    ) -> Result<String, Refused> {
        match self {
            Identity::Local(name) => Ok(name.clone()),
            Identity::Header(name) => header(name)
                .map(str::trim)
                .filter(|value| !value.is_empty())
                .map(str::to_string)
                .ok_or_else(|| Refused::Unidentified(name.clone())),
        }
    }
}

/// The comments of one served or version folder: its store, the bus every
/// successful write is announced on — `comments <page>`, once, as the write
/// lands — so open pages fetch that page's threads again, and the folder a
/// new thread's page must be a file in.
#[derive(Clone)]
pub struct Comments {
    /// Where the threads are kept.
    store: Arc<dyn Store>,
    /// The bus each successful write is announced on.
    changes: Arc<dyn Publish>,
    /// The folder a new thread's page must be a file in.
    folder: ServedFolder,
}

impl Comments {
    /// The comments kept by `store`, announced on `changes`, for the pages of
    /// `folder`.
    pub fn new(store: Arc<dyn Store>, changes: Arc<dyn Publish>, folder: ServedFolder) -> Self {
        Self {
            store,
            changes,
            folder,
        }
    }

    /// A page's threads, oldest first.
    pub fn list(&self, page: &str) -> Result<Vec<Listed>, Refused> {
        Ok(self.store.list(&page_key(page)?)?)
    }

    /// Starts a thread on `page`, which must be a file in the folder.
    pub fn create(
        &self,
        page: &str,
        anchor: Anchor,
        author: &str,
        text: &str,
    ) -> Result<String, Refused> {
        let page = page_key(page)?;
        if self.folder.file_at(&page).is_none() {
            return Err(Refused::NotAPage(page));
        }
        let id = self.store.create(&page, anchor, message(author, text))?;
        self.announce(page);
        Ok(id)
    }

    /// Appends a reply to thread `id`, wherever it is — detached included.
    pub fn reply(&self, id: &str, author: &str, text: &str) -> Result<(), Refused> {
        let page = self.found(id, |id| self.store.reply(id, message(author, text)))?;
        self.announce(page);
        Ok(())
    }

    /// Resolves thread `id`: it is gone.
    pub fn resolve(&self, id: &str) -> Result<(), Refused> {
        let page = self.found(id, |id| self.store.resolve(id))?;
        self.announce(page);
        Ok(())
    }

    /// Where every attached thread goes on `version`, this folder opened on
    /// the version's entry document: each finds its quote in its
    /// page's text as the page shows it — front matter left out, widget
    /// tags expanded — by its context where it can, and is detached when
    /// the quote, or the page, is gone. Nothing is written: the publish
    /// commits the [`Carry`] together with its version, so both land or
    /// neither does, and then [`Comments::carried`] announces it. It renders
    /// pages, which draws their diagrams: it blocks.
    pub fn carry(&self, version: &Folder) -> Result<Carry> {
        let modules = Modules::default();
        let mut pages: std::collections::BTreeMap<String, Vec<Listed>> = Default::default();
        for (page, thread) in self.store.attached()? {
            pages.entry(page).or_default().push(thread);
        }
        let mut carry = Carry::default();
        for (page, threads) in pages {
            let text = self
                .content(version, &modules, &page)
                .map(|html| anchor::text(&html));
            for listed in threads {
                let found = text
                    .as_deref()
                    .and_then(|text| anchor::reanchor(&listed.thread.anchor, text));
                if found.as_ref() != Some(&listed.thread.anchor) {
                    carry.moved.push(Moved {
                        id: listed.id,
                        anchor: found,
                    });
                    carry.pages.insert(page.clone());
                }
            }
        }
        Ok(carry)
    }

    /// A carry committed: each page whose threads moved is announced once.
    pub fn carried(&self, carry: Carry) {
        for page in carry.pages {
            self.announce(page);
        }
    }

    /// The changes open pages should hear for `paths`, changed by another
    /// hand: a page's comments for each page whose threads changed. Nothing
    /// is published — the watcher publishes what it heard together.
    pub fn heard(&self, paths: &[PathBuf]) -> Vec<Change> {
        self.store
            .heard(paths)
            .into_iter()
            .map(Change::Comments)
            .collect()
    }

    /// The page thread `id` was on after `write`, or why there is none.
    fn found(
        &self,
        id: &str,
        write: impl FnOnce(&str) -> Result<Option<String>>,
    ) -> Result<String, Refused> {
        if !is_id(id) {
            return Err(Refused::NoThread(id.into()));
        }
        write(id)?.ok_or_else(|| Refused::NoThread(id.into()))
    }

    /// The HTML of `page`'s content as its page shows it: rendered with the
    /// widgets of the document that shows it — its owner, or else the
    /// version's entry document. `None` when the page is gone.
    fn content(&self, version: &Folder, modules: &Modules, page: &str) -> Option<String> {
        let file = self.folder.file_at(page)?;
        let source = std::fs::read_to_string(&file).ok()?;
        let site = version.owner(&file).and_then(|owner| owner.document.ok());
        let entry = version.entry();
        let widgets = Widgets::of(site.as_ref().unwrap_or(&entry), modules);
        Some(
            crate::page::content(&widgets, &file, source)
                .html()
                .to_string(),
        )
    }

    /// Announces that `page`'s threads changed: `comments <page>` on the bus.
    fn announce(&self, page: String) {
        self.changes.publish(Change::Comments(page));
    }
}

/// A page's key: its address with exactly one leading `/` — or a refusal
/// when it is empty or climbs out of the folder.
fn page_key(page: &str) -> Result<String, Refused> {
    let name = page.trim_start_matches('/');
    let plain = Path::new(name)
        .components()
        .all(|part| matches!(part, std::path::Component::Normal(_)));
    if name.is_empty() || !plain {
        return Err(Refused::NotAPage(page.into()));
    }
    Ok(format!("/{name}"))
}

/// A message by `author`, written now.
fn message(author: &str, text: &str) -> Message {
    Message {
        author: author.into(),
        at: now().0,
        text: text.into(),
    }
}

/// A new thread's id: the UTC time it was started, to the second, and four
/// hex digits from the clock's nanoseconds — so a page's threads list oldest
/// first.
pub fn new_id() -> String {
    let (stamp, nanos) = now();
    format!("{}-{:04x}", stamp.replace(['-', ':'], ""), nanos & 0xffff)
}

/// Whether `id` can be a thread's: letters, digits and dashes, never empty.
pub fn is_id(id: &str) -> bool {
    !id.is_empty() && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
}

/// Now, as RFC 3339 UTC to the second, and the sub-second nanoseconds.
fn now() -> (String, u32) {
    let since = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    let secs = since.as_secs() as i64;
    let (days, rest) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
    // Civil date from days since 1970-01-01 (Howard Hinnant's algorithm).
    let z = days + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = doy - (153 * mp + 2) / 5 + 1;
    let month = if mp < 10 { mp + 3 } else { mp - 9 };
    let year = yoe + era * 400 + i64::from(month <= 2);
    (
        format!(
            "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
            rest / 3600,
            rest % 3600 / 60,
            rest % 60
        ),
        since.subsec_nanos(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    fn anchor() -> Anchor {
        Anchor {
            quote: "retries".into(),
            prefix: "The client ".into(),
            suffix: ".".into(),
        }
    }

    /// A served folder holding `files`, its comments in memory announcing
    /// on a recorder.
    fn folder(
        name: &str,
        files: &[(&str, &str)],
    ) -> (
        Comments,
        Arc<crate::changes::Recorder>,
        Arc<memory::Memory>,
        ServedFolder,
    ) {
        let root =
            std::env::temp_dir().join(format!("devpage-comments-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        for (file, text) in files {
            let path = root.join(file);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, text).unwrap();
        }
        std::fs::create_dir_all(&root).unwrap();
        let served = ServedFolder::new(&root).unwrap();
        let changes = Arc::new(crate::changes::Recorder::default());
        let store = Arc::new(memory::Memory::default());
        let comments = Comments::new(store.clone(), changes.clone(), served.clone());
        (comments, changes, store, served)
    }

    #[test]
    fn every_write_announces_its_page_once_and_a_failed_one_nothing() {
        let (comments, changes, _, _) = folder("announce", &[("a.md", "# A\n")]);
        let id = comments
            .create("a.md", anchor(), "Ana", "Every write?")
            .unwrap();
        comments.reply(&id, "Bo", "Only the outbox.").unwrap();
        comments.resolve(&id).unwrap();
        assert!(matches!(
            comments.reply(&id, "Bo", "gone"),
            Err(Refused::NoThread(_))
        ));
        assert!(matches!(
            comments.create("/../x.md", anchor(), "Ana", "no"),
            Err(Refused::NotAPage(_))
        ));
        let comments_of_a = Change::Comments("/a.md".into());
        assert_eq!(
            changes.drain(),
            [comments_of_a.clone(), comments_of_a.clone(), comments_of_a],
            "one per write that landed, keyed `/a.md` however it was named, none for the two refused"
        );
    }

    #[test]
    fn a_thread_starts_only_on_a_file_in_the_folder() {
        let (comments, changes, _, _) = folder(
            "create",
            &[
                ("a.md", "# A\n"),
                ("notes/lone.md", "# Lone\n"),
                ("docs/x", ""),
            ],
        );
        // A lone page no document links to is still a file in the folder.
        assert!(
            comments
                .create("/notes/lone.md", anchor(), "Ana", "ok")
                .is_ok()
        );
        for page in ["/missing.md", "/notes", "/../escape.md", "", "/"] {
            assert!(
                matches!(
                    comments.create(page, anchor(), "Ana", "no"),
                    Err(Refused::NotAPage(_))
                ),
                "{page}"
            );
        }
        assert_eq!(changes.drain(), [Change::Comments("/notes/lone.md".into())]);
    }

    #[test]
    fn a_reply_and_a_resolve_go_by_id_whatever_page_it_is_on() {
        let (comments, _, store, _) = folder("by-id", &[("a.md", "# A\n")]);
        let id = comments.create("/a.md", anchor(), "Ana", "x").unwrap();
        // Detached: its text is gone from the version shown.
        store.detach(&id);
        comments.reply(&id, "Bo", "still here").unwrap();
        let listed = comments.list("/a.md").unwrap();
        assert!(listed[0].detached);
        assert_eq!(listed[0].thread.thread[1].text, "still here");
        comments.resolve(&id).unwrap();
        assert!(comments.list("/a.md").unwrap().is_empty());
        assert!(matches!(
            comments.resolve("../x"),
            Err(Refused::NoThread(_))
        ));
    }

    #[test]
    fn carry_reads_the_page_as_it_is_shown() {
        let page = "---\ntitle: Launch\n---\n\n# Launch\n\n<Callout>\nThe client **retries** every write.\n</Callout>\n";
        let (comments, changes, store, served) = folder("carry", &[("a.md", page)]);
        let kept = comments
            .create(
                "/a.md",
                Anchor {
                    quote: "retries every write".into(),
                    prefix: "The client ".into(),
                    suffix: ".".into(),
                },
                "Ana",
                "kept",
            )
            .unwrap();
        let gone = comments
            .create(
                "/a.md",
                Anchor {
                    quote: "title: Launch".into(),
                    prefix: String::new(),
                    suffix: String::new(),
                },
                "Ana",
                "front matter is never shown",
            )
            .unwrap();
        changes.drain();
        let entry = crate::document::Document::load(&served.path().join("a.md"), &served).unwrap();
        let carry = comments.carry(&Folder::new(Arc::new(entry))).unwrap();
        assert_eq!(
            carry.moved(),
            [Moved {
                id: gone,
                anchor: None
            }],
            "the widget's text is found as the page shows it; front matter never is"
        );
        assert!(
            store.list("/a.md").unwrap().iter().all(|t| !t.detached),
            "nothing written: the publish commits the carry"
        );
        let _ = kept;
        assert!(
            changes.drain().is_empty(),
            "nothing written, nothing announced"
        );
        comments.carried(carry);
        assert_eq!(changes.drain(), [Change::Comments("/a.md".into())]);
    }

    #[test]
    fn a_header_author_is_trimmed_and_a_blank_one_refused() {
        let deployed = Identity::Header("X-Forwarded-Email".into());
        for blank in [None, Some(""), Some("   ")] {
            assert!(
                matches!(deployed.author(|_| blank), Err(Refused::Unidentified(_))),
                "{blank:?}"
            );
        }
        assert_eq!(
            deployed
                .author(|name| (name == "X-Forwarded-Email").then_some(" ana@example.com "))
                .unwrap(),
            "ana@example.com"
        );
        let local = Identity::Local("Andrei".into());
        assert_eq!(local.author(|_| None).unwrap(), "Andrei");
    }

    #[test]
    fn timestamps_are_rfc3339_utc() {
        let (stamp, _) = now();
        assert_eq!(stamp.len(), 20);
        assert!(
            stamp.ends_with('Z') && stamp.as_bytes()[10] == b'T',
            "{stamp}"
        );
    }

    #[test]
    fn a_page_that_climbs_out_and_a_made_up_id_are_refused() {
        assert_eq!(page_key("docs/a.md").unwrap(), "/docs/a.md");
        assert!(page_key("/../x.md").is_err());
        assert!(is_id("20261025T181502Z-7f3a"));
        assert!(!is_id("../x") && !is_id(""));
    }
}