mull 0.6.0

Organize your knowledge.
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use crate::{
    Errors,
    document::{Document, HOME_TITLE, Link},
    format::CodeStr,
    path_util::relative_path,
};
use ignore::{WalkBuilder, overrides::OverrideBuilder};
use std::{
    collections::HashSet,
    fs,
    path::{Path, PathBuf},
};

// Validate every link and ensure every walked file is referenced.
pub fn validate(document: &Document, document_path: &Path) -> Result<(), Errors> {
    // Collect errors in the parsed text-link graph.
    let mut errors = validate_text_links(document);

    // Resolve the directory to a stable path and confirm that the document is accessible.
    let original_document_directory = document_path
        .parent()
        .filter(|path| !path.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let document_directory = match fs::canonicalize(original_document_directory) {
        Ok(document_directory) => document_directory,
        Err(error) => {
            errors.push(format!(
                "Failed to resolve document directory {}: {error}",
                original_document_directory.to_string_lossy().code_str(),
            ));
            return errors_to_result(errors);
        }
    };
    if let Err(error) = fs::metadata(document_path) {
        errors.push(format!(
            "Failed to resolve {}: {error}",
            document_path.to_string_lossy().code_str(),
        ));
        return errors_to_result(errors);
    }
    let Some(document_file_name) = document_path.file_name() else {
        errors.push(format!(
            "Failed to determine the file name of {}.",
            document_path.to_string_lossy().code_str(),
        ));
        return errors_to_result(errors);
    };
    let logical_document_path = document_directory.join(document_file_name);

    // Collect filesystem-link and unreferenced-file errors.
    errors.extend(validate_filesystem_links(
        document,
        &document_directory,
        &logical_document_path,
    ));

    // Report all validation errors together.
    errors_to_result(errors)
}

// Validate text-link targets and the graph rooted at the special home node.
fn validate_text_links(document: &Document) -> Vec<String> {
    // Accumulate graph errors in deterministic order.
    let mut errors = Vec::<String>::new();

    // Require the root node from which every other node must be reachable.
    let has_home = document.text_nodes.contains_key(HOME_TITLE);
    if !has_home {
        errors.push(format!(
            "Document does not contain a {} node.",
            HOME_TITLE.code_str(),
        ));
    }

    // Validate text-link targets deterministically.
    let mut nodes = document.text_nodes.values().collect::<Vec<_>>();
    nodes.sort_by_key(|node| &node.title);
    for node in &nodes {
        let mut text_links = node
            .links
            .iter()
            .filter_map(|link| match link {
                Link::Text(title) => Some(title),
                Link::File(_) | Link::Directory(_) => None,
            })
            .collect::<Vec<_>>();
        text_links.sort();
        for text_link in text_links {
            if !document.text_nodes.contains_key(text_link) {
                errors.push(format!(
                    "Node {} links to missing node {}.",
                    node.title.code_str(),
                    text_link.code_str(),
                ));
            }
        }
    }

    // Reject every node outside the graph rooted at the home node.
    if has_home {
        let mut unreachable_titles = document
            .text_nodes
            .values()
            .filter(|node| node.depth.is_none())
            .map(|node| &node.title)
            .collect::<Vec<_>>();
        unreachable_titles.sort();
        errors.extend(unreachable_titles.into_iter().map(|title| {
            format!(
                "Node {} is not reachable from {}.",
                title.code_str(),
                HOME_TITLE.code_str(),
            )
        }));
    }

    // Return every text-link graph error.
    errors
}

// Validate filesystem links and ensure every walked file is referenced.
fn validate_filesystem_links(
    document: &Document,
    document_directory: &Path,
    document_path: &Path,
) -> Vec<String> {
    // Visit nodes and links in deterministic order.
    let mut referenced_files = HashSet::<PathBuf>::new();
    let mut referenced_directories = HashSet::<PathBuf>::new();
    let mut errors = Vec::<String>::new();
    let mut nodes = document.text_nodes.values().collect::<Vec<_>>();
    nodes.sort_by_key(|node| &node.title);
    for node in nodes {
        let mut links = node.links.iter().collect::<Vec<_>>();
        links.sort();
        for link in links {
            // Skip text links after extracting the path from each filesystem link.
            let path = match link {
                Link::Text(_) => continue,
                Link::File(path) | Link::Directory(path) => path,
            };

            // Load target metadata while following symbolic links.
            let target = document_directory.join(path);
            let metadata = match fs::metadata(&target) {
                Ok(metadata) => metadata,
                Err(error) => {
                    errors.push(format!(
                        "Node {} links to inaccessible path {}: {error}",
                        node.title.code_str(),
                        path.to_string_lossy().code_str(),
                    ));
                    continue;
                }
            };

            // Retain correctly typed targets and report links with the wrong type.
            match link {
                Link::File(_) if metadata.is_file() => {
                    referenced_files.insert(target);
                }
                Link::Directory(_) if metadata.is_dir() => {
                    referenced_directories.insert(target);
                }
                Link::File(_) => errors.push(format!(
                    "Node {} links to {}, which is not a file.",
                    node.title.code_str(),
                    path.to_string_lossy().code_str(),
                )),
                Link::Directory(_) => errors.push(format!(
                    "Node {} links to {}, which is not a directory.",
                    node.title.code_str(),
                    path.to_string_lossy().code_str(),
                )),
                Link::Text(_) => unreachable!("text links were already skipped"),
            }
        }
    }

    // Collect walk and unreferenced-file errors using the validated targets.
    errors.extend(find_unreferenced_filesystem_links(
        document_directory,
        document_path,
        &referenced_files,
        &referenced_directories,
    ));

    // Return every filesystem validation error.
    errors
}

// Find unreferenced files while pruning explicitly referenced directories.
fn find_unreferenced_filesystem_links(
    document_directory: &Path,
    document_path: &Path,
    referenced_files: &HashSet<PathBuf>,
    referenced_directories: &HashSet<PathBuf>,
) -> Vec<String> {
    // Skip the walk because the root is not subject to the entry filter below.
    if referenced_directories.contains(document_directory) {
        return Vec::new();
    }

    // Include hidden entries while retaining ignore-file behavior and excluding VCS metadata.
    let mut overrides = OverrideBuilder::new(document_directory);
    overrides
        .add("!.git/")
        .expect("the static .git override should be valid")
        .add("!.hg/")
        .expect("the static .hg override should be valid");
    let overrides = match overrides.build() {
        Ok(overrides) => overrides,
        Err(error) => return vec![format!("Failed to build filesystem ignore rules: {error}")],
    };

    // Follow directory symlinks while pruning subtrees covered by explicit directory links.
    let mut walker_builder = WalkBuilder::new(document_directory);
    walker_builder
        .current_dir(document_directory)
        .follow_links(true)
        .hidden(false)
        .parents(false)
        .require_git(false)
        .overrides(overrides)
        .filter_entry({
            let document_path = document_path.to_owned();
            let referenced_directories = referenced_directories.clone();
            move |entry| {
                // Exclude the document and prune directories already covered by their links.
                entry.path() != document_path && !referenced_directories.contains(entry.path())
            }
        });

    // Collect walk and unreferenced-file errors.
    let mut errors = Vec::<String>::new();
    for result in walker_builder.build() {
        let entry = match result {
            Ok(entry) => entry,
            Err(error) => {
                errors.push(format!("Failed to walk document directory: {error}"));
                continue;
            }
        };
        let path = entry.path();
        let Some(file_type) = entry.file_type() else {
            continue;
        };
        if file_type.is_file() && !referenced_files.contains(path) {
            errors.push(format!(
                "File {} is not referenced.",
                relative_path(document_directory, path)
                    .to_string_lossy()
                    .code_str(),
            ));
        }
    }

    // Make filesystem errors deterministic regardless of traversal order.
    errors.sort();

    // Return every deterministic walk error.
    errors
}

// Convert collected validation errors into the public result type.
fn errors_to_result(errors: Errors) -> Result<(), Errors> {
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

#[cfg(test)]
mod tests {
    use super::validate;
    use crate::parser::parse;
    use std::{
        fs,
        path::{Path, PathBuf},
        process,
        sync::atomic::{AtomicUsize, Ordering},
    };

    // Assign each test directory a unique path even when tests run concurrently.
    static NEXT_DIRECTORY: AtomicUsize = AtomicUsize::new(0);

    // This guard owns a temporary directory and removes it after a test.
    struct TestDirectory(PathBuf);

    // Create an isolated directory containing a document.
    impl TestDirectory {
        fn new() -> Self {
            let sequence = NEXT_DIRECTORY.fetch_add(1, Ordering::Relaxed);
            let path =
                std::env::temp_dir().join(format!("mull-validation-{}-{sequence}", process::id()));
            fs::create_dir(&path).unwrap();
            fs::write(path.join("document.mull"), "# Home\n").unwrap();
            Self(path)
        }

        fn path(&self) -> &Path {
            &self.0
        }

        fn document_path(&self) -> PathBuf {
            self.0.join("document.mull")
        }
    }

    // Clean up files created by a validation test.
    impl Drop for TestDirectory {
        fn drop(&mut self) {
            fs::remove_dir_all(&self.0).unwrap();
        }
    }

    // Validate referenced entries and prune the recursive contents of referenced directories.
    #[test]
    fn referenced_entries() {
        let directory = TestDirectory::new();
        fs::write(directory.path().join(".gitignore"), "ignored.txt\n").unwrap();
        fs::write(directory.path().join(".secret"), "secret").unwrap();
        fs::write(directory.path().join("ignored.txt"), "ignored").unwrap();
        fs::create_dir(directory.path().join("images")).unwrap();
        fs::write(directory.path().join("images/photo.jpg"), "photo").unwrap();
        let document = parse(concat!(
            "# Home\n[",
            "file:.gitignore] [",
            "file:.secret] [",
            "dir:images]",
        ))
        .unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Allow a document-directory link to cover every surrounding filesystem entry.
    #[test]
    fn document_directory_link() {
        let directory = TestDirectory::new();
        fs::write(directory.path().join("unmanaged.txt"), "content").unwrap();
        let document = parse(concat!("# Home\n[", "dir:.]")).unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Preserve graph errors when the document path cannot be resolved.
    #[test]
    fn missing_document_path() {
        let directory = TestDirectory::new();
        let document_path = directory.document_path();
        fs::remove_file(&document_path).unwrap();
        let document = parse("# Elsewhere").unwrap();

        let errors = validate(&document, &document_path).unwrap_err();
        assert_eq!(errors[0], "Document does not contain a `Home` node.");
        assert!(
            errors[1].starts_with(&format!("Failed to resolve `{}`:", document_path.display())),
        );
        assert_eq!(errors.len(), 2);
    }

    // Report unreferenced files within directories instead of requiring directory links.
    #[test]
    fn unreferenced_entries() {
        let directory = TestDirectory::new();
        fs::create_dir(directory.path().join("images")).unwrap();
        fs::write(directory.path().join("images/photo.jpg"), "photo").unwrap();
        let document = parse("# Home").unwrap();

        let errors = validate(&document, &directory.document_path()).unwrap_err();
        let photo_path = Path::new("images").join("photo.jpg");
        assert_eq!(
            errors,
            vec![format!(
                "File `{}` is not referenced.",
                photo_path.display(),
            )],
        );
    }

    // Infer references to directories whose files are all explicitly referenced.
    #[test]
    fn implicitly_referenced_directories() {
        let directory = TestDirectory::new();
        fs::create_dir(directory.path().join("notes")).unwrap();
        fs::create_dir(directory.path().join("notes/archive")).unwrap();
        fs::write(directory.path().join("notes/current.txt"), "current").unwrap();
        fs::write(directory.path().join("notes/archive/old.txt"), "old").unwrap();
        let document = parse(concat!(
            "# Home\n[",
            "file:notes/current.txt] [",
            "file:notes/archive/old.txt]",
        ))
        .unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Consider empty directories referenced because all their contents are referenced.
    #[test]
    fn empty_directories() {
        let directory = TestDirectory::new();
        fs::create_dir(directory.path().join("empty")).unwrap();
        fs::create_dir(directory.path().join("empty/nested")).unwrap();
        let document = parse("# Home").unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Preserve symlink aliases as distinct filesystem paths while following their targets.
    #[cfg(unix)]
    #[test]
    fn symlink_aliases() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        fs::write(directory.path().join("target.txt"), "content").unwrap();
        symlink("target.txt", directory.path().join("first.txt")).unwrap();
        symlink("target.txt", directory.path().join("second.txt")).unwrap();
        let document = parse(concat!(
            "# Home\n[",
            "file:target.txt] [",
            "file:first.txt]",
        ))
        .unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec!["File `second.txt` is not referenced.".to_owned()],
        );
    }

    // Follow an unlinked directory symlink and validate files through its logical path.
    #[cfg(unix)]
    #[test]
    fn directory_symlink() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        fs::create_dir(directory.path().join("target")).unwrap();
        fs::write(directory.path().join("target/file.txt"), "content").unwrap();
        symlink("target", directory.path().join("alias")).unwrap();
        let document = parse(concat!(
            "# Home\n[",
            "dir:target] [",
            "file:alias/file.txt]",
        ))
        .unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Allow directory symlinks outside the document tree and validate their logical contents.
    #[cfg(unix)]
    #[test]
    fn external_directory_symlink() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        let external_directory = TestDirectory::new();
        symlink(external_directory.path(), directory.path().join("external")).unwrap();
        let document = parse(concat!("# Home\n[", "file:external/document.mull]")).unwrap();

        assert_eq!(validate(&document, &directory.document_path()), Ok(()));
    }

    // Exclude a document symlink by its logical path instead of its resolved target.
    #[cfg(unix)]
    #[test]
    fn document_symlink() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        let document_path = directory.document_path();
        let target_path = directory.path().join("document.txt");
        fs::rename(&document_path, &target_path).unwrap();
        fs::write(&target_path, concat!("# Home\n[", "file:document.txt]")).unwrap();
        symlink("document.txt", &document_path).unwrap();
        let document = parse(concat!("# Home\n[", "file:document.txt]")).unwrap();

        assert_eq!(validate(&document, &document_path), Ok(()));
    }

    // Report a broken symlink because its target cannot be classified.
    #[cfg(unix)]
    #[test]
    fn broken_symlink() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        symlink("missing", directory.path().join("broken")).unwrap();
        let document = parse("# Home").unwrap();

        assert!(
            validate(&document, &directory.document_path())
                .unwrap_err()
                .iter()
                .any(|error| error.starts_with("Failed to walk document directory:")),
        );
    }

    // Report a directory symlink cycle instead of recursing indefinitely.
    #[cfg(unix)]
    #[test]
    fn symlink_cycle() {
        use std::os::unix::fs::symlink;

        let directory = TestDirectory::new();
        symlink(".", directory.path().join("cycle")).unwrap();
        let document = parse("# Home").unwrap();

        assert!(
            validate(&document, &directory.document_path())
                .unwrap_err()
                .iter()
                .any(|error| error.starts_with("Failed to walk document directory:")),
        );
    }

    // Reject a filesystem link whose target has the wrong type.
    #[test]
    fn wrong_target_type() {
        let directory = TestDirectory::new();
        fs::create_dir(directory.path().join("images")).unwrap();
        fs::write(directory.path().join("images/photo.jpg"), "photo").unwrap();
        let document = parse(concat!("# Home\n[", "file:images]")).unwrap();
        let photo_path = Path::new("images").join("photo.jpg");

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec![
                "Node `Home` links to `images`, which is not a file.".to_owned(),
                format!("File `{}` is not referenced.", photo_path.display()),
            ],
        );
    }

    // Reject text links that do not correspond to any node in the document.
    #[test]
    fn missing_text_link() {
        let directory = TestDirectory::new();
        let document = parse("# Home\nSee [Zulu] and [Alpha].").unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec![
                "Node `Home` links to missing node `Alpha`.".to_owned(),
                "Node `Home` links to missing node `Zulu`.".to_owned(),
            ],
        );
    }

    // Report independent graph, filesystem-link, and unreferenced-entry errors together.
    #[test]
    fn multiple_validation_errors() {
        let directory = TestDirectory::new();
        fs::write(directory.path().join("unreferenced.txt"), "content").unwrap();
        let document = parse(concat!(
            "# Home\nSee [Missing] and [",
            "file:missing.txt].\n",
            "# Orphan",
        ))
        .unwrap();

        let errors = validate(&document, &directory.document_path()).unwrap_err();
        assert!(
            errors
                .iter()
                .any(|error| error == "Node `Home` links to missing node `Missing`."),
        );
        assert!(
            errors
                .iter()
                .any(|error| error == "Node `Orphan` is not reachable from `Home`."),
        );
        assert!(errors.iter().any(|error| {
            error.starts_with("Node `Home` links to inaccessible path `missing.txt`:")
        }));
        assert!(
            errors
                .iter()
                .any(|error| error == "File `unreferenced.txt` is not referenced."),
        );
        assert_eq!(errors.len(), 4);
    }

    // Reject an empty text link because node titles cannot be empty.
    #[test]
    fn empty_text_link() {
        let directory = TestDirectory::new();
        let document = parse("# Home\nSee [].").unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec!["Node `Home` links to missing node ``.".to_owned()],
        );
    }

    // Require every document to contain its special root node.
    #[test]
    fn missing_home() {
        let directory = TestDirectory::new();
        let document = parse("# Elsewhere").unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec!["Document does not contain a `Home` node.".to_owned()],
        );
    }

    // Reject nodes that cannot be reached transitively from Home.
    #[test]
    fn unreachable_nodes() {
        let directory = TestDirectory::new();
        let document = parse("# Home\nSee [Middle].\n# Middle\n# Zulu\n# Alpha").unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            vec![
                "Node `Alpha` is not reachable from `Home`.".to_owned(),
                "Node `Zulu` is not reachable from `Home`.".to_owned(),
            ],
        );
    }
}