mull 0.5.1

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
use crate::{
    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 filesystem entry is referenced.
pub fn validate(document: &Document, document_path: &Path) -> Result<(), String> {
    // Collect errors in the parsed text-link graph.
    let mut errors = validate_text_links(document);

    // Resolve the directory and document to stable absolute paths.
    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);
        }
    };
    let resolved_document_path = match fs::canonicalize(document_path) {
        Ok(document_path) => document_path,
        Err(error) => {
            errors.push(format!(
                "Failed to resolve {}: {error}",
                document_path.to_string_lossy().code_str(),
            ));
            return errors_to_result(&errors);
        }
    };

    // Validate filesystem links and collect their canonical targets.
    let (referenced_files, referenced_directories, filesystem_link_errors) =
        validate_filesystem_links(document, &document_directory);
    errors.extend(filesystem_link_errors);

    // Collect walk and unreferenced-entry errors for deterministic reporting.
    errors.extend(find_unreferenced_entries(
        &document_directory,
        &resolved_document_path,
        &referenced_files,
        &referenced_directories,
    ));

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

// Validate filesystem links and collect their targets and errors.
fn validate_filesystem_links(
    document: &Document,
    document_directory: &Path,
) -> (HashSet<PathBuf>, HashSet<PathBuf>, 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 {
            let (path, expect_directory) = match link {
                Link::Text(_) => continue,
                Link::File(path) => (path, false),
                Link::Directory(path) => (path, true),
            };

            // Retain valid targets and collect failures without stopping validation.
            match validate_target(document_directory, path, expect_directory, &node.title) {
                Ok(target) => {
                    if expect_directory {
                        referenced_directories.insert(target);
                    } else {
                        referenced_files.insert(target);
                    }
                }
                Err(error) => {
                    errors.push(error);

                    // Prevent a wrong-type link from also producing an unreferenced-entry error.
                    let target = document_directory.join(path);
                    if let Ok(metadata) = fs::metadata(&target)
                        && let Ok(target) = fs::canonicalize(target)
                    {
                        if metadata.is_file() {
                            referenced_files.insert(target);
                        } else if metadata.is_dir() {
                            referenced_directories.insert(target);
                        }
                    }
                }
            }
        }
    }

    // Return every collected target and link error.
    (referenced_files, referenced_directories, errors)
}

// Find every walked filesystem entry that is not covered by a link.
fn find_unreferenced_entries(
    document_directory: &Path,
    document_path: &Path,
    referenced_files: &HashSet<PathBuf>,
    referenced_directories: &HashSet<PathBuf>,
) -> Vec<String> {
    // 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}")],
    };
    let pruned_directories = referenced_directories.clone();
    let mut walker_builder = WalkBuilder::new(document_directory);
    walker_builder
        .current_dir(document_directory)
        .hidden(false)
        .parents(false)
        .require_git(false)
        .overrides(overrides)
        .filter_entry(move |entry| !pruned_directories.contains(entry.path()));

    // Collect walk and unreferenced-entry 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();
        if path == document_directory || path == document_path {
            continue;
        }
        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(),
            ));
        } else if file_type.is_dir() {
            errors.push(format!(
                "Directory {} is not referenced.",
                relative_path(document_directory, path)
                    .to_string_lossy()
                    .code_str(),
            ));
        }
    }
    errors.sort();

    // Return every deterministic walk error.
    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
}

// Convert collected validation errors into the public result type.
fn errors_to_result(errors: &[String]) -> Result<(), String> {
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors.join("\n"))
    }
}

// Validate one filesystem target and return its canonical path.
fn validate_target(
    document_directory: &Path,
    path: &Path,
    expect_directory: bool,
    node_title: &str,
) -> Result<PathBuf, String> {
    // Resolve the link relative to the document directory.
    let target = document_directory.join(path);
    let metadata = fs::metadata(&target).map_err(|error| {
        format!(
            "Node {} links to inaccessible path {}: {error}",
            node_title.code_str(),
            path.to_string_lossy().code_str(),
        )
    })?;
    let has_expected_type = if expect_directory {
        metadata.is_dir()
    } else {
        metadata.is_file()
    };
    if !has_expected_type {
        let expected_type = if expect_directory {
            "directory"
        } else {
            "file"
        };
        return Err(format!(
            "Node {} links to {}, which is not a {expected_type}.",
            node_title.code_str(),
            path.to_string_lossy().code_str(),
        ));
    }

    // Canonicalize the validated target for comparison with walked entries.
    fs::canonicalize(&target).map_err(|error| {
        format!(
            "Failed to resolve path {} linked from node {}: {error}",
            path.to_string_lossy().code_str(),
            node_title.code_str(),
        )
    })
}

#[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(()));
    }

    // 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 error = validate(&document, &document_path).unwrap_err();
        let mut errors = error.lines();
        assert_eq!(
            errors.next(),
            Some("Document does not contain a `Home` node."),
        );
        assert!(
            errors
                .next()
                .unwrap()
                .starts_with(&format!("Failed to resolve `{}`:", document_path.display())),
        );
        assert_eq!(errors.next(), None);
    }

    // Report unreferenced entries throughout the document directory tree.
    #[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 error = validate(&document, &directory.document_path()).unwrap_err();
        assert!(error.contains("Directory `images` is not referenced."));
        assert!(error.contains(&format!(
            "`{}`",
            Path::new("images").join("photo.jpg").display(),
        )));
    }

    // 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();
        let document = parse(concat!("# Home\n[", "file:images]")).unwrap();

        assert_eq!(
            validate(&document, &directory.document_path()).unwrap_err(),
            "Node `Home` links to `images`, which is not a file.",
        );
    }

    // 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(),
            concat!(
                "Node `Home` links to missing node `Alpha`.\n",
                "Node `Home` links to missing node `Zulu`.",
            ),
        );
    }

    // 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 error = validate(&document, &directory.document_path()).unwrap_err();
        assert!(error.contains("Node `Home` links to missing node `Missing`."));
        assert!(error.contains("Node `Orphan` is not reachable from `Home`."));
        assert!(error.contains("Node `Home` links to inaccessible path `missing.txt`:"));
        assert!(error.contains("File `unreferenced.txt` is not referenced."));
        assert_eq!(error.lines().count(), 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(),
            "Node `Home` links to missing node ``.",
        );
    }

    // 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(),
            "Document does not contain a `Home` node.",
        );
    }

    // 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(),
            concat!(
                "Node `Alpha` is not reachable from `Home`.\n",
                "Node `Zulu` is not reachable from `Home`.",
            ),
        );
    }
}