]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/unstable_book.rs
6b573908de9025b9da5b90d9964145baeb05e22a
[rust.git] / src / tools / tidy / src / unstable_book.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::collections::BTreeSet;
12 use std::fs;
13 use std::path;
14 use features::{collect_lang_features, collect_lib_features, Features, Status};
15
16 pub const PATH_STR: &str = "doc/unstable-book/src";
17
18 pub const COMPILER_FLAGS_DIR: &str = "compiler-flags";
19
20 pub const LANG_FEATURES_DIR: &str = "language-features";
21
22 pub const LIB_FEATURES_DIR: &str = "library-features";
23
24 /// Build the path to the Unstable Book source directory from the Rust 'src' directory
25 pub fn unstable_book_path(base_src_path: &path::Path) -> path::PathBuf {
26     base_src_path.join(PATH_STR)
27 }
28
29 /// Directory where the features are documented within the Unstable Book source directory
30 pub fn unstable_book_lang_features_path(base_src_path: &path::Path) -> path::PathBuf {
31     unstable_book_path(base_src_path).join(LANG_FEATURES_DIR)
32 }
33
34 /// Directory where the features are documented within the Unstable Book source directory
35 pub fn unstable_book_lib_features_path(base_src_path: &path::Path) -> path::PathBuf {
36     unstable_book_path(base_src_path).join(LIB_FEATURES_DIR)
37 }
38
39 /// Test to determine if DirEntry is a file
40 fn dir_entry_is_file(dir_entry: &fs::DirEntry) -> bool {
41     dir_entry
42         .file_type()
43         .expect("could not determine file type of directory entry")
44         .is_file()
45 }
46
47 /// Retrieve names of all unstable features
48 pub fn collect_unstable_feature_names(features: &Features) -> BTreeSet<String> {
49     features
50         .iter()
51         .filter(|&(_, ref f)| f.level == Status::Unstable)
52         .map(|(name, _)| name.replace('_', "-"))
53         .collect()
54 }
55
56 pub fn collect_unstable_book_section_file_names(dir: &path::Path) -> BTreeSet<String> {
57     fs::read_dir(dir)
58         .expect("could not read directory")
59         .into_iter()
60         .map(|entry| entry.expect("could not read directory entry"))
61         .filter(dir_entry_is_file)
62         .map(|entry| entry.file_name().into_string().unwrap())
63         .map(|n| n.trim_right_matches(".md").to_owned())
64         .collect()
65 }
66
67 /// Retrieve file names of all library feature sections in the Unstable Book with:
68 ///
69 /// * hyphens replaced by underscores
70 /// * the markdown suffix ('.md') removed
71 fn collect_unstable_book_lang_features_section_file_names(base_src_path: &path::Path)
72                                                           -> BTreeSet<String> {
73     collect_unstable_book_section_file_names(&unstable_book_lang_features_path(base_src_path))
74 }
75
76 /// Retrieve file names of all language feature sections in the Unstable Book with:
77 ///
78 /// * hyphens replaced by underscores
79 /// * the markdown suffix ('.md') removed
80 fn collect_unstable_book_lib_features_section_file_names(base_src_path: &path::Path)
81                                                          -> BTreeSet<String> {
82     collect_unstable_book_section_file_names(&unstable_book_lib_features_path(base_src_path))
83 }
84
85 pub fn check(path: &path::Path, bad: &mut bool) {
86
87     // Library features
88
89     let lang_features = collect_lang_features(path, bad);
90     let lib_features = collect_lib_features(path).into_iter().filter(|&(ref name, _)| {
91         !lang_features.contains_key(name)
92     }).collect();
93
94     let unstable_lib_feature_names = collect_unstable_feature_names(&lib_features);
95     let unstable_book_lib_features_section_file_names =
96         collect_unstable_book_lib_features_section_file_names(path);
97
98     // Check for Unstable Book sections that don't have a corresponding unstable feature
99     for feature_name in &unstable_book_lib_features_section_file_names -
100                         &unstable_lib_feature_names {
101         tidy_error!(bad,
102                     "The Unstable Book has a 'library feature' section '{}' which doesn't \
103                      correspond to an unstable library feature",
104                     feature_name)
105     }
106
107     // Language features
108
109     let unstable_lang_feature_names = collect_unstable_feature_names(&lang_features);
110     let unstable_book_lang_features_section_file_names =
111         collect_unstable_book_lang_features_section_file_names(path);
112
113     // Check for Unstable Book sections that don't have a corresponding unstable feature
114     for feature_name in &unstable_book_lang_features_section_file_names -
115                         &unstable_lang_feature_names {
116         tidy_error!(bad,
117                     "The Unstable Book has a 'language feature' section '{}' which doesn't \
118                      correspond to an unstable language feature",
119                     feature_name)
120     }
121
122     // List unstable features that don't have Unstable Book sections
123     // Remove the comment marker if you want the list printed
124     /*
125     println!("Lib features without unstable book sections:");
126     for feature_name in &unstable_lang_feature_names -
127                         &unstable_book_lang_features_section_file_names {
128         println!("    * {} {:?}", feature_name, lib_features[&feature_name].tracking_issue);
129     }
130
131     println!("Lang features without unstable book sections:");
132     for feature_name in &unstable_lib_feature_names-
133                         &unstable_book_lib_features_section_file_names {
134         println!("    * {} {:?}", feature_name, lang_features[&feature_name].tracking_issue);
135     }
136     // */
137 }