xtask/codegen/
parser_lists.rs

1use std::{fs, path::Path};
2
3use anyhow::Result;
4
5use crate::schema::{Group, Language};
6
7const HEADER: &str = "
8<!-- Everything under here is autogenerated by running `cargo xtask codegen` -->
9<!-- DO NOT EDIT! -->
10
11## List of included parsers
12";
13
14pub fn write() -> Result<()> {
15    let readme_path = crate::WORKSPACE_DIR.join("syntastica-parsers-git/README.md");
16    let mut readme = read(&readme_path)?;
17    readme += &parser_list(|_| true, git_url);
18    fs::write(&readme_path, readme)?;
19
20    let readme_path = crate::WORKSPACE_DIR.join("syntastica-parsers-gitdep/README.md");
21    let mut readme = read(&readme_path)?;
22    readme += &parser_list(|lang| lang.parser.supports_gitdep(), git_url);
23    fs::write(&readme_path, readme)?;
24
25    let readme_path = crate::WORKSPACE_DIR.join("syntastica-parsers/README.md");
26    let mut readme = read(&readme_path)?;
27    readme += &parser_list(|lang| lang.parser.supports_dep(), crates_io_url);
28    fs::write(&readme_path, readme)?;
29
30    Ok(())
31}
32
33fn read(path: &Path) -> Result<String> {
34    let mut readme = fs::read_to_string(path)?;
35    if let Some((preserve, _)) = readme.split_once(HEADER) {
36        readme.truncate(preserve.len());
37    }
38    readme += HEADER;
39    Ok(readme)
40}
41
42fn git_url(lang: &Language) -> String {
43    format!("{}/tree/{}", lang.parser.git.url, lang.parser.git.rev)
44}
45
46fn crates_io_url(lang: &Language) -> String {
47    match &lang.parser.crates_io {
48        Some(version) => format!("https://docs.rs/{}/{version}/", lang.parser.package),
49        None => lang.parser.git.url.clone(),
50    }
51}
52
53fn parser_list(filter: impl Fn(&Language) -> bool, url: impl Fn(&Language) -> String) -> String {
54    let mut some_list = String::new();
55    let mut most_list = String::new();
56    let mut all_list = String::new();
57    for lang in &crate::LANGUAGE_CONFIG.languages {
58        let str = format!(
59            "- [{}]({}){}\n",
60            lang.name,
61            url(lang),
62            if filter(lang) {
63                ""
64            } else {
65                " (not supported by this collection)"
66            }
67        );
68        match lang.group {
69            Group::Some => some_list += &str,
70            Group::Most => most_list += &str,
71            Group::All => all_list += &str,
72        }
73    }
74
75    format!(
76        r##"
77<!-- dprint-ignore-start -->
78
79<details>
80<summary>List of parsers included in the <span class="stab portability"><code>some</code></span> feature</summary>
81
82{some_list}
83</details>
84
85<details>
86<summary>List of parsers additionally included in the <span class="stab portability"><code>most</code></span> feature</summary>
87
88{most_list}
89</details>
90
91<details>
92<summary>List of parsers additionally included in the <span class="stab portability"><code>all</code></span> feature</summary>
93
94{all_list}
95</details>
96
97<!-- dprint-ignore-end -->
98"##
99    )
100}