aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_diagnostic_docs.rs
blob: a2561817b9d323a19dc73a65ba812f146a03f242 (plain)
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
//! Generates `assists.md` documentation.

use std::{fmt, path::PathBuf};

use crate::{
    codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE},
    project_root, rust_files, Result,
};

pub(crate) fn generate_diagnostic_docs(mode: Mode) -> Result<()> {
    let diagnostics = Diagnostic::collect()?;
    let contents =
        diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
    let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
    let dst = project_root().join("docs/user/generated_diagnostic.adoc");
    codegen::update(&dst, &contents, mode)?;
    Ok(())
}

#[derive(Debug)]
struct Diagnostic {
    id: String,
    location: Location,
    doc: String,
}

impl Diagnostic {
    fn collect() -> Result<Vec<Diagnostic>> {
        let mut res = Vec::new();
        for path in rust_files() {
            collect_file(&mut res, path)?;
        }
        res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
        return Ok(res);

        fn collect_file(acc: &mut Vec<Diagnostic>, path: PathBuf) -> Result<()> {
            let text = xshell::read_file(&path)?;
            let comment_blocks = extract_comment_blocks_with_empty_lines("Diagnostic", &text);

            for block in comment_blocks {
                let id = block.id;
                if let Err(msg) = is_valid_diagnostic_name(&id) {
                    panic!("invalid diagnostic name: {:?}:\n  {}", id, msg)
                }
                let doc = block.contents.join("\n");
                let location = Location::new(path.clone(), block.line);
                acc.push(Diagnostic { id, location, doc })
            }

            Ok(())
        }
    }
}

fn is_valid_diagnostic_name(diagnostic: &str) -> Result<(), String> {
    let diagnostic = diagnostic.trim();
    if diagnostic.find(char::is_whitespace).is_some() {
        return Err("Diagnostic names can't contain whitespace symbols".into());
    }
    if diagnostic.chars().any(|c| c.is_ascii_uppercase()) {
        return Err("Diagnostic names can't contain uppercase symbols".into());
    }
    if diagnostic.chars().any(|c| !c.is_ascii()) {
        return Err("Diagnostic can't contain non-ASCII symbols".into());
    }

    Ok(())
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)
    }
}