tidy/
unstable_book.rs

1use std::collections::BTreeSet;
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use crate::features::{CollectedFeatures, Features, Status};
6
7pub const PATH_STR: &str = "doc/unstable-book";
8
9pub const COMPILER_FLAGS_DIR: &str = "src/compiler-flags";
10
11pub const LANG_FEATURES_DIR: &str = "src/language-features";
12
13pub const LIB_FEATURES_DIR: &str = "src/library-features";
14
15/// Builds the path to the Unstable Book source directory from the Rust 'src' directory.
16pub fn unstable_book_path(base_src_path: &Path) -> PathBuf {
17    base_src_path.join(PATH_STR)
18}
19
20/// Builds the path to the directory where the features are documented within the Unstable Book
21/// source directory.
22pub fn unstable_book_lang_features_path(base_src_path: &Path) -> PathBuf {
23    unstable_book_path(base_src_path).join(LANG_FEATURES_DIR)
24}
25
26/// Builds the path to the directory where the features are documented within the Unstable Book
27/// source directory.
28pub fn unstable_book_lib_features_path(base_src_path: &Path) -> PathBuf {
29    unstable_book_path(base_src_path).join(LIB_FEATURES_DIR)
30}
31
32/// Tests whether `DirEntry` is a file.
33fn dir_entry_is_file(dir_entry: &fs::DirEntry) -> bool {
34    dir_entry.file_type().expect("could not determine file type of directory entry").is_file()
35}
36
37/// Retrieves names of all unstable features.
38pub fn collect_unstable_feature_names(features: &Features) -> BTreeSet<String> {
39    features
40        .iter()
41        .filter(|&(_, ref f)| f.level == Status::Unstable)
42        .map(|(name, _)| name.replace('_', "-"))
43        .collect()
44}
45
46pub fn collect_unstable_book_section_file_names(dir: &Path) -> BTreeSet<String> {
47    fs::read_dir(dir)
48        .expect("could not read directory")
49        .map(|entry| entry.expect("could not read directory entry"))
50        .filter(dir_entry_is_file)
51        .map(|entry| entry.path())
52        .filter(|path| path.extension().map(|e| e.to_str().unwrap()) == Some("md"))
53        .map(|path| path.file_stem().unwrap().to_str().unwrap().into())
54        .collect()
55}
56
57/// Retrieves file names of all library feature sections in the Unstable Book with:
58///
59/// * hyphens replaced by underscores,
60/// * the markdown suffix ('.md') removed.
61fn collect_unstable_book_lang_features_section_file_names(
62    base_src_path: &Path,
63) -> BTreeSet<String> {
64    collect_unstable_book_section_file_names(&unstable_book_lang_features_path(base_src_path))
65}
66
67/// Retrieves file names of all language feature sections in the Unstable Book with:
68///
69/// * hyphens replaced by underscores,
70/// * the markdown suffix ('.md') removed.
71fn collect_unstable_book_lib_features_section_file_names(base_src_path: &Path) -> BTreeSet<String> {
72    collect_unstable_book_section_file_names(&unstable_book_lib_features_path(base_src_path))
73}
74
75pub fn check(path: &Path, features: CollectedFeatures, bad: &mut bool) {
76    let lang_features = features.lang;
77    let lib_features = features
78        .lib
79        .into_iter()
80        .filter(|&(ref name, _)| !lang_features.contains_key(name))
81        .collect::<Features>();
82
83    // Library features
84    let unstable_lib_feature_names = collect_unstable_feature_names(&lib_features);
85    let unstable_book_lib_features_section_file_names =
86        collect_unstable_book_lib_features_section_file_names(path);
87
88    // Language features
89    let unstable_lang_feature_names = collect_unstable_feature_names(&lang_features);
90    let unstable_book_lang_features_section_file_names =
91        collect_unstable_book_lang_features_section_file_names(path);
92
93    // Check for Unstable Book sections that don't have a corresponding unstable feature
94    for feature_name in &unstable_book_lib_features_section_file_names - &unstable_lib_feature_names
95    {
96        if !unstable_lang_feature_names.contains(&feature_name) {
97            tidy_error!(
98                bad,
99                "The Unstable Book has a 'library feature' section '{}' which doesn't \
100                         correspond to an unstable library feature",
101                feature_name
102            );
103        }
104    }
105
106    // Check for Unstable Book sections that don't have a corresponding unstable feature.
107    for feature_name in
108        &unstable_book_lang_features_section_file_names - &unstable_lang_feature_names
109    {
110        tidy_error!(
111            bad,
112            "The Unstable Book has a 'language feature' section '{}' which doesn't \
113                     correspond to an unstable language feature",
114            feature_name
115        )
116    }
117
118    // List unstable features that don't have Unstable Book sections.
119    // Remove the comment marker if you want the list printed.
120    /*
121    println!("Lib features without unstable book sections:");
122    for feature_name in &unstable_lang_feature_names -
123                        &unstable_book_lang_features_section_file_names {
124        println!("    * {} {:?}", feature_name, lib_features[&feature_name].tracking_issue);
125    }
126
127    println!("Lang features without unstable book sections:");
128    for feature_name in &unstable_lib_feature_names-
129                        &unstable_book_lib_features_section_file_names {
130        println!("    * {} {:?}", feature_name, lang_features[&feature_name].tracking_issue);
131    }
132    // */
133}