devpage-core 0.3.4

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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Link for Rust: every file's declarations into the items they make,
//! named as Rust names them — crate, modules, item. Resolves what no single
//! file can: the crate and module a file is (`Cargo.toml`, `lib.rs`, `mod.rs`),
//! the type an `impl` in another file belongs to, what `pub use` makes
//! reachable, and whether an item is reachable from its crate's root.

use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};

use super::extract::{Declaration, Extracted, Visibility};
use super::{Item, Items, Member};

/// One file of a crate, with what it declares.
pub struct Unit<'a> {
    /// The file's path: what each item and member declared in it records
    /// as its file.
    pub file: &'a Path,
    /// The declarations extraction found in it.
    pub extracted: &'a Extracted,
}

/// The crate a file belongs to and the module it is, from the nearest
/// `Cargo.toml` above it: `None` for a file outside its crate's `src`, which
/// no crate root reaches.
pub fn module_of(
    file: &Path,
    crates: &mut HashMap<PathBuf, Option<String>>,
) -> Option<Vec<String>> {
    let mut at = file.parent();
    while let Some(folder) = at {
        let manifest = folder.join("Cargo.toml");
        let name = crates
            .entry(folder.to_path_buf())
            .or_insert_with(|| crate_name(&manifest))
            .clone();
        if let Some(name) = name {
            let relative = file.strip_prefix(folder.join("src")).ok()?;
            let mut path = vec![name];
            let parts: Vec<String> = relative
                .components()
                .map(|part| part.as_os_str().to_string_lossy().into_owned())
                .collect();
            let (last, folders) = parts.split_last()?;
            path.extend(folders.iter().cloned());
            let stem = last.strip_suffix(".rs")?;
            if !(folders.is_empty() && (stem == "lib" || stem == "main")) && stem != "mod" {
                path.push(stem.to_string());
            }
            return Some(path);
        }
        at = folder.parent();
    }
    None
}

/// A crate's name as Rust code names it: its `[lib] name`, or its
/// `[package] name` with `-` as `_`. `None` when there is no manifest, or it
/// names no package.
fn crate_name(manifest: &Path) -> Option<String> {
    let text = std::fs::read_to_string(manifest).ok()?;
    let mut section = "";
    let mut package = None;
    let mut lib = None;
    for line in text.lines() {
        let line = line.trim();
        if line.starts_with('[') {
            section = line;
            continue;
        }
        let Some(value) = line
            .strip_prefix("name")
            .map(str::trim_start)
            .and_then(|rest| rest.strip_prefix('='))
            .map(|value| value.trim().trim_matches('"').to_string())
        else {
            continue;
        };
        match section {
            "[package]" => package = Some(value),
            "[lib]" => lib = Some(value),
            _ => {}
        }
    }
    lib.or(package).map(|name| name.replace('-', "_"))
}

/// Kinds a member of a type is; every other kind is an item of its module.
fn is_type(kind: &str) -> bool {
    matches!(kind, "struct" | "enum" | "union" | "trait")
}

/// The items of `units`: every module and item, with members on their
/// types. With `private`, an item no `pub` path reaches is kept too.
pub fn link(units: &[(Vec<String>, Unit)], private: bool) -> Items {
    let mut items: BTreeMap<Vec<String>, Item> = BTreeMap::new();
    // Whether each module's own declaration makes it public; a crate root is.
    let mut module_public: HashMap<Vec<String>, bool> = HashMap::new();
    // Modules declared hidden — `#[cfg(test)] mod tests;` — whose files,
    // read on their own, are left out with them.
    let mut hidden: Vec<Vec<String>> = Vec::new();
    // Members of `impl` blocks, by the module they are written in and the
    // type they name, attached once every type is known.
    let mut attached = Vec::new();

    for (module, unit) in units {
        let entry = items.entry(module.clone()).or_insert_with(|| Item {
            path: module.clone(),
            kind: "module".into(),
            file: unit.file.to_path_buf(),
            line: 1,
            ..Item::default()
        });
        // A file's `//!` documents the module it is; a `mod x;` may document
        // it instead, whichever is read first.
        if !unit.extracted.doc.is_empty() {
            entry.doc = unit.extracted.doc.clone();
        }
        entry.file = unit.file.to_path_buf();
        entry.line = 1;
        if module.len() == 1 {
            module_public.insert(module.clone(), true);
        }
        let declarations = &unit.extracted.declarations;
        // The path each declaration gives its children: a module's own, or
        // its parent's for anything else.
        let mut paths: Vec<Option<Vec<String>>> = Vec::with_capacity(declarations.len());
        for declaration in declarations.iter() {
            let parent = declaration.parent.map(|at| &declarations[at]);
            let inside = match declaration.parent {
                None => Some(module.clone()),
                Some(at) => paths[at].clone(),
            };
            let Some(inside) = inside else {
                paths.push(None);
                continue;
            };
            // Hidden, and everything inside it.
            if declaration.hidden {
                if declaration.kind == "module" {
                    let mut path = inside.clone();
                    path.push(declaration.name.clone());
                    hidden.push(path);
                }
                paths.push(None);
                continue;
            }
            match parent {
                // Inside a function's body: local, not part of the module.
                Some(parent) if parent.kind == "function" => paths.push(None),
                Some(parent) if is_type(&parent.kind) => {
                    if let Some(item) = items.get_mut(&inside) {
                        let kind = if declaration.kind == "function" {
                            "method".to_string()
                        } else {
                            declaration.kind.clone()
                        };
                        // A trait's items and an enum's variants are as
                        // visible as the trait or the enum.
                        let public = declaration.visibility == Visibility::Public
                            || parent.kind == "trait"
                            || declaration.kind == "variant";
                        item.members
                            .push(member(declaration, &kind, unit.file, public));
                    }
                    paths.push(None);
                }
                Some(parent) if parent.kind == "attach" => {
                    attached.push((
                        module.clone(),
                        parent.attach.clone(),
                        declaration.clone(),
                        unit.file.to_path_buf(),
                    ));
                    paths.push(None);
                }
                _ if declaration.kind == "attach" => {
                    if declaration
                        .attach
                        .as_ref()
                        .is_some_and(|a| a.implements.is_some())
                    {
                        attached.push((
                            module.clone(),
                            declaration.attach.clone(),
                            declaration.clone(),
                            unit.file.to_path_buf(),
                        ));
                    }
                    paths.push(Some(inside));
                }
                _ => {
                    let mut path = inside.clone();
                    path.push(declaration.name.clone());
                    if declaration.kind == "module" {
                        module_public
                            .insert(path.clone(), declaration.visibility == Visibility::Public);
                        let item = items.entry(path.clone()).or_insert_with(|| Item {
                            path: path.clone(),
                            kind: "module".into(),
                            file: unit.file.to_path_buf(),
                            line: declaration.line,
                            ..Item::default()
                        });
                        // `mod x;` documents the module its own file is.
                        if !declaration.doc.is_empty() && item.doc.is_empty() {
                            item.doc = declaration.doc.clone();
                        }
                        if declaration.has_body {
                            item.file = unit.file.to_path_buf();
                            item.line = declaration.line;
                            item.doc = declaration.doc.clone();
                        }
                    } else {
                        items.insert(
                            path.clone(),
                            Item {
                                path: path.clone(),
                                kind: declaration.kind.clone(),
                                signature: declaration.signature.clone(),
                                doc: declaration.doc.clone(),
                                file: unit.file.to_path_buf(),
                                line: declaration.line,
                                public: declaration.visibility == Visibility::Public,
                                ..Item::default()
                            },
                        );
                    }
                    paths.push(Some(path));
                }
            }
        }
    }

    items.retain(|path, _| !hidden.iter().any(|module| path.starts_with(module)));

    // `impl` members onto the types they name: in the same module first,
    // then the one type of that name in the crate.
    for (module, attach, declaration, file) in attached {
        let Some(attach) = attach else { continue };
        let name = type_name(&attach.to);
        let Some(item) = find_type(&items, &module, &name) else {
            continue;
        };
        let item = items.get_mut(&item).unwrap();
        if declaration.kind == "attach" {
            if let Some(implements) = &attach.implements {
                let implements = type_name(implements);
                if !item.implements.contains(&implements) {
                    item.implements.push(implements);
                }
            }
        } else if attach.implements.is_none() {
            let kind = if declaration.kind == "function" {
                "method"
            } else {
                declaration.kind.as_str()
            };
            let public = declaration.visibility == Visibility::Public;
            item.members.push(member(&declaration, kind, &file, public));
        }
    }

    // Modules are public when every module down to them is declared `pub`.
    let reachable = |path: &[String]| {
        (1..=path.len()).all(|at| {
            let module = &path[..at];
            match items.get(module) {
                Some(item) if item.kind == "module" => {
                    module_public.get(module).copied().unwrap_or(false)
                }
                _ => true,
            }
        })
    };
    let public: Vec<Vec<String>> = items
        .iter()
        .filter(|(path, item)| reachable(path) && (item.kind == "module" || item.public))
        .map(|(path, _)| path.clone())
        .collect();
    for item in items.values_mut() {
        item.public = false;
    }
    for path in &public {
        items.get_mut(path).unwrap().public = true;
    }

    // `pub use` makes an item reachable where it is written: an alias, and
    // the item's address when no `pub` path reached it otherwise.
    let mut aliases = BTreeMap::new();
    for (module, unit) in units {
        for reexport in &unit.extracted.reexports {
            if reexport.visibility != Visibility::Public || reexport.parent.is_some() {
                continue;
            }
            for (path, alias) in use_paths(&reexport.path) {
                let Some(resolved) = resolve(&items, module, &path) else {
                    continue;
                };
                let mut at = module.clone();
                at.push(alias);
                if at == resolved {
                    continue;
                }
                let module_reachable = items.get(module).is_some_and(|t| t.public);
                let item = &items[&resolved];
                if module_reachable && !item.public && item.kind != "module" {
                    let mut moved = items.remove(&resolved).unwrap();
                    moved.path = at.clone();
                    moved.public = true;
                    items.insert(at.clone(), moved);
                    aliases.insert(resolved, at);
                } else {
                    aliases.insert(at, resolved);
                }
            }
        }
    }

    if !private {
        items.retain(|_, item| item.public);
        for item in items.values_mut() {
            item.members.retain(|member| member.public);
        }
        aliases.retain(|_, to| items.contains_key(to));
    }
    Items { items, aliases }
}

/// A declaration as a member of its item: `kind` names what it is, `public`
/// whether it is reachable.
fn member(declaration: &Declaration, kind: &str, file: &Path, public: bool) -> Member {
    Member {
        name: declaration.name.clone(),
        kind: kind.to_string(),
        signature: declaration.signature.clone(),
        doc: declaration.doc.clone(),
        file: file.to_path_buf(),
        line: declaration.line,
        public,
    }
}

/// A type as written, as its bare name: `crate::a::Foo<T>` is `Foo`.
fn type_name(written: &str) -> String {
    let bare = written.split('<').next().unwrap_or(written);
    let bare = bare.rsplit("::").next().unwrap_or(bare);
    bare.trim().trim_start_matches('&').trim().to_string()
}

/// The type named `name` that code in `module` means: one in the module,
/// or the only one of that name in the crate.
fn find_type(
    items: &BTreeMap<Vec<String>, Item>,
    module: &[String],
    name: &str,
) -> Option<Vec<String>> {
    let mut here = module.to_vec();
    here.push(name.to_string());
    if items.get(&here).is_some_and(|t| is_type(&t.kind)) {
        return Some(here);
    }
    let mut found = items
        .iter()
        .filter(|(path, item)| {
            path.first() == module.first()
                && path.last().is_some_and(|last| last == name)
                && is_type(&item.kind)
        })
        .map(|(path, _)| path.clone());
    let first = found.next()?;
    found.next().is_none().then_some(first)
}

/// What a `use` argument names, each with the name it is used as:
/// `a::{B, C as D}` is `a::B` as `B` and `a::C` as `D`. A glob names nothing
/// to alias.
fn use_paths(written: &str) -> Vec<(Vec<String>, String)> {
    let written: String = written.split_whitespace().collect::<Vec<_>>().join(" ");
    let mut out = Vec::new();
    expand(&written, &[], &mut out);
    out
}

/// Adds to `out` the paths one `use` tree names under `prefix`, each with its
/// alias: a `{…}` group is expanded part by part, at its own depth.
fn expand(written: &str, prefix: &[String], out: &mut Vec<(Vec<String>, String)>) {
    let written = written.trim();
    if let Some(open) = written.find('{') {
        let head: Vec<String> = written[..open]
            .trim_end_matches("::")
            .split("::")
            .filter(|s| !s.is_empty())
            .map(str::to_string)
            .collect();
        let inner = written[open + 1..].trim_end().trim_end_matches('}');
        let mut path = prefix.to_vec();
        path.extend(head);
        let mut depth = 0;
        let mut start = 0;
        for (at, c) in inner.char_indices() {
            match c {
                '{' => depth += 1,
                '}' => depth -= 1,
                ',' if depth == 0 => {
                    expand(&inner[start..at], &path, out);
                    start = at + 1;
                }
                _ => {}
            }
        }
        expand(&inner[start..], &path, out);
        return;
    }
    let (path, alias) = match written.split_once(" as ") {
        Some((path, alias)) => (path.trim(), Some(alias.trim().to_string())),
        None => (written, None),
    };
    if path.ends_with('*') || path.is_empty() || path == "self" {
        return;
    }
    let mut full = prefix.to_vec();
    full.extend(path.split("::").map(str::to_string));
    let alias = alias.unwrap_or_else(|| full.last().cloned().unwrap_or_default());
    if alias != "_" {
        out.push((full, alias));
    }
}

/// The item `path`, written in `module`, names: from the crate root
/// (`crate::`), the module (`self::`), its parent (`super::`), another
/// crate by name, or the module itself.
fn resolve(
    items: &BTreeMap<Vec<String>, Item>,
    module: &[String],
    path: &[String],
) -> Option<Vec<String>> {
    let (first, rest) = path.split_first()?;
    let start: Vec<String> = match first.as_str() {
        "crate" => module[..1].to_vec(),
        "self" => module.to_vec(),
        "super" => module[..module.len().saturating_sub(1).max(1)].to_vec(),
        _ => {
            let mut here = module.to_vec();
            here.extend(path.iter().cloned());
            if items.contains_key(&here) {
                return Some(here);
            }
            let other = path.to_vec();
            return items.contains_key(&other).then_some(other);
        }
    };
    let mut full = start;
    full.extend(rest.iter().cloned());
    items.contains_key(&full).then_some(full)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::code::extract::{Docs, Grammar};

    fn grammar() -> Grammar {
        Grammar::new(
            tree_sitter_rust::LANGUAGE.into(),
            include_str!("rust.scm"),
            Docs::Rust,
        )
        .unwrap()
    }

    fn crate_dir(name: &str, files: &[(&str, &str)]) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("devpage-code-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"my-crate\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        for (file, source) in files {
            let path = dir.join("src").join(file);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, source).unwrap();
        }
        dir
    }

    fn linked(dir: &Path, files: &[&str], private: bool) -> Items {
        let grammar = grammar();
        let mut crates = HashMap::new();
        let extracted: Vec<(PathBuf, Extracted)> = files
            .iter()
            .map(|file| {
                let path = dir.join("src").join(file);
                let source = std::fs::read_to_string(&path).unwrap();
                (path, grammar.extract(&source))
            })
            .collect();
        let units: Vec<(Vec<String>, Unit)> = extracted
            .iter()
            .map(|(file, extracted)| {
                (
                    module_of(file, &mut crates).unwrap(),
                    Unit { file, extracted },
                )
            })
            .collect();
        link(&units, private)
    }

    fn path(text: &str) -> Vec<String> {
        text.split("::").map(str::to_string).collect()
    }

    #[test]
    fn a_crate_is_named_by_its_manifest_and_its_files_are_its_modules() {
        let dir = crate_dir(
            "modules",
            &[
                ("lib.rs", "//! The crate.\npub mod a;\nmod hidden;\n"),
                ("a/mod.rs", "//! A.\npub mod b;\npub fn top() {}\n"),
                ("a/b.rs", "pub struct Deep;\n"),
                ("hidden.rs", "pub fn secret() {}\n"),
            ],
        );
        let items = linked(&dir, &["lib.rs", "a/mod.rs", "a/b.rs", "hidden.rs"], false);
        let paths: Vec<String> = items.items.keys().map(|p| p.join("::")).collect();
        assert_eq!(
            paths,
            [
                "my_crate",
                "my_crate::a",
                "my_crate::a::b",
                "my_crate::a::b::Deep",
                "my_crate::a::top"
            ]
        );
        assert_eq!(items.items[&path("my_crate")].doc, "The crate.");
        assert_eq!(items.items[&path("my_crate::a")].doc, "A.");
        let all = linked(&dir, &["lib.rs", "a/mod.rs", "a/b.rs", "hidden.rs"], true);
        assert!(all.items.contains_key(&path("my_crate::hidden::secret")));
    }

    #[test]
    fn a_test_module_in_its_own_file_is_left_out() {
        let dir = crate_dir(
            "hidden",
            &[
                ("lib.rs", "pub mod a;\n#[cfg(test)]\nmod tests;\n"),
                ("a.rs", "pub fn shown() {}\n"),
                ("tests.rs", "pub fn hidden() {}\n"),
            ],
        );
        let items = linked(&dir, &["lib.rs", "a.rs", "tests.rs"], true);
        let paths: Vec<String> = items.items.keys().map(|p| p.join("::")).collect();
        assert_eq!(paths, ["my_crate", "my_crate::a", "my_crate::a::shown"]);
    }

    #[test]
    fn an_impl_in_another_file_adds_its_methods_and_traits_to_the_type() {
        let dir = crate_dir(
            "impls",
            &[
                ("lib.rs", "pub mod store;\nmod read;\n"),
                (
                    "store.rs",
                    "/// Holds entries.\npub struct Store {\n    /// How many.\n    pub count: usize,\n    secret: u8,\n}\nimpl std::fmt::Display for Store {\n    fn fmt(&self) {}\n}\n",
                ),
                (
                    "read.rs",
                    "use crate::store::Store;\nimpl Store {\n    /// Reads one.\n    pub fn read(&self) -> u8 { 0 }\n    fn private(&self) {}\n}\n",
                ),
            ],
        );
        let items = linked(&dir, &["lib.rs", "store.rs", "read.rs"], false);
        let store = &items.items[&path("my_crate::store::Store")];
        let members: Vec<(&str, &str)> = store
            .members
            .iter()
            .map(|m| (m.kind.as_str(), m.name.as_str()))
            .collect();
        assert_eq!(members, [("field", "count"), ("method", "read")]);
        assert_eq!(store.implements, ["Display"]);
        assert_eq!(store.members[1].doc, "Reads one.");
        assert!(store.members[1].file.ends_with("read.rs"));
    }

    #[test]
    fn pub_use_gives_an_item_in_a_private_module_its_public_address() {
        let dir = crate_dir(
            "reexports",
            &[
                (
                    "lib.rs",
                    "mod decimal;\npub use decimal::{Decimal, parse as parse_decimal};\npub mod open;\n",
                ),
                ("decimal.rs", "pub struct Decimal;\npub fn parse() {}\n"),
                ("open.rs", "pub struct Open;\n"),
            ],
        );
        let items = linked(&dir, &["lib.rs", "decimal.rs", "open.rs"], false);
        assert!(items.items.contains_key(&path("my_crate::Decimal")));
        assert!(items.items.contains_key(&path("my_crate::parse_decimal")));
        assert!(
            !items
                .items
                .contains_key(&path("my_crate::decimal::Decimal"))
        );
        assert_eq!(
            items.aliases.get(&path("my_crate::decimal::Decimal")),
            Some(&path("my_crate::Decimal"))
        );
    }

    #[test]
    fn use_lists_expand_to_each_name_and_alias() {
        assert_eq!(
            use_paths("crate::a::{B, c::{D, E as F}, *}"),
            [
                (path("crate::a::B"), "B".to_string()),
                (path("crate::a::c::D"), "D".to_string()),
                (path("crate::a::c::E"), "F".to_string()),
            ]
        );
    }
}