|
| 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::HashSet; |
| 12 | +use std::fs; |
| 13 | +use std::io::{self, BufRead, Write}; |
| 14 | +use std::path; |
| 15 | +use features::{collect_lang_features, collect_lib_features, Status}; |
| 16 | + |
| 17 | +const PATH_STR: &'static str = "doc/unstable-book/src"; |
| 18 | + |
| 19 | +const SUMMARY_FILE_NAME: &'static str = "SUMMARY.md"; |
| 20 | + |
| 21 | +static EXCLUDE: &'static [&'static str; 2] = &[SUMMARY_FILE_NAME, "the-unstable-book.md"]; |
| 22 | + |
| 23 | +/// Build the path to the Unstable Book source directory from the Rust 'src' directory |
| 24 | +fn unstable_book_path(base_src_path: &path::Path) -> path::PathBuf { |
| 25 | + base_src_path.join(PATH_STR) |
| 26 | +} |
| 27 | + |
| 28 | +/// Build the path to the Unstable Book SUMMARY file from the Rust 'src' directory |
| 29 | +fn unstable_book_summary_path(base_src_path: &path::Path) -> path::PathBuf { |
| 30 | + unstable_book_path(base_src_path).join(SUMMARY_FILE_NAME) |
| 31 | +} |
| 32 | + |
| 33 | +/// Open the Unstable Book SUMMARY file |
| 34 | +fn open_unstable_book_summary_file(base_src_path: &path::Path) -> fs::File { |
| 35 | + fs::File::open(unstable_book_summary_path(base_src_path)) |
| 36 | + .expect("could not open Unstable Book SUMMARY.md") |
| 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.file_type().expect("could not determine file type of directory entry").is_file() |
| 42 | +} |
| 43 | + |
| 44 | +/// Retrieve names of all lang-related unstable features |
| 45 | +fn collect_unstable_lang_feature_names(base_src_path: &path::Path) -> HashSet<String> { |
| 46 | + collect_lang_features(base_src_path) |
| 47 | + .into_iter() |
| 48 | + .filter(|&(_, ref f)| f.level == Status::Unstable) |
| 49 | + .map(|(ref name, _)| name.to_owned()) |
| 50 | + .collect() |
| 51 | +} |
| 52 | + |
| 53 | +/// Retrieve names of all lib-related unstable features |
| 54 | +fn collect_unstable_lib_feature_names(base_src_path: &path::Path) -> HashSet<String> { |
| 55 | + let mut bad = true; |
| 56 | + let lang_features = collect_lang_features(base_src_path); |
| 57 | + collect_lib_features(base_src_path, &mut bad, &lang_features) |
| 58 | + .into_iter() |
| 59 | + .filter(|&(_, ref f)| f.level == Status::Unstable) |
| 60 | + .map(|(ref name, _)| name.to_owned()) |
| 61 | + .collect() |
| 62 | +} |
| 63 | + |
| 64 | +/// Retrieve names of all unstable features |
| 65 | +fn collect_unstable_feature_names(base_src_path: &path::Path) -> HashSet<String> { |
| 66 | + collect_unstable_lib_feature_names(base_src_path) |
| 67 | + .union(&collect_unstable_lang_feature_names(base_src_path)) |
| 68 | + .map(|n| n.to_owned()) |
| 69 | + .collect::<HashSet<_, _>>() |
| 70 | +} |
| 71 | + |
| 72 | +/// Retrieve file names of all sections in the Unstable Book with: |
| 73 | +/// |
| 74 | +/// * hyphens replaced by underscores |
| 75 | +/// * the markdown suffix ('.md') removed |
| 76 | +fn collect_unstable_book_section_file_names(base_src_path: &path::Path) -> HashSet<String> { |
| 77 | + fs::read_dir(unstable_book_path(base_src_path)) |
| 78 | + .expect("could not read directory") |
| 79 | + .into_iter() |
| 80 | + .map(|entry| entry.expect("could not read directory entry")) |
| 81 | + .filter(dir_entry_is_file) |
| 82 | + .map(|entry| entry.file_name().into_string().unwrap()) |
| 83 | + .filter(|n| EXCLUDE.iter().all(|e| n != e)) |
| 84 | + .map(|n| n.trim_right_matches(".md").replace('-', "_")) |
| 85 | + .collect() |
| 86 | +} |
| 87 | + |
| 88 | +/// Retrieve unstable feature names that are in the Unstable Book SUMMARY file |
| 89 | +fn collect_unstable_book_summary_links(base_src_path: &path::Path) -> HashSet<String> { |
| 90 | + let summary_link_regex = |
| 91 | + ::regex::Regex::new(r"^- \[(\S+)\]\(\S+\.md\)").expect("invalid regex"); |
| 92 | + io::BufReader::new(open_unstable_book_summary_file(base_src_path)) |
| 93 | + .lines() |
| 94 | + .map(|l| l.expect("could not read line from file")) |
| 95 | + .filter_map(|line| { |
| 96 | + summary_link_regex.captures(&line).map(|c| { |
| 97 | + c.get(1) |
| 98 | + .unwrap() |
| 99 | + .as_str() |
| 100 | + .to_owned() |
| 101 | + }) |
| 102 | + }) |
| 103 | + .collect() |
| 104 | +} |
| 105 | + |
| 106 | +pub fn check(path: &path::Path, bad: &mut bool) { |
| 107 | + let unstable_feature_names = collect_unstable_feature_names(path); |
| 108 | + let unstable_book_section_file_names = collect_unstable_book_section_file_names(path); |
| 109 | + let unstable_book_links = collect_unstable_book_summary_links(path); |
| 110 | + |
| 111 | + // Check for Unstable Book section names with no corresponding SUMMARY.md link |
| 112 | + for feature_name in &unstable_book_section_file_names - &unstable_book_links { |
| 113 | + *bad = true; |
| 114 | + writeln!(io::stderr(), |
| 115 | + "The Unstable Book section '{}' needs to have a link in SUMMARY.md", |
| 116 | + feature_name) |
| 117 | + .expect("could not write to stderr") |
| 118 | + } |
| 119 | + |
| 120 | + // Check for unstable features that don't have Unstable Book sections |
| 121 | + for feature_name in &unstable_feature_names - &unstable_book_section_file_names { |
| 122 | + *bad = true; |
| 123 | + writeln!(io::stderr(), |
| 124 | + "Unstable feature '{}' needs to have a section in The Unstable Book", |
| 125 | + feature_name) |
| 126 | + .expect("could not write to stderr") |
| 127 | + } |
| 128 | + |
| 129 | + // Check for Unstable Book sections that don't have a corresponding unstable feature |
| 130 | + for feature_name in &unstable_book_section_file_names - &unstable_feature_names { |
| 131 | + *bad = true; |
| 132 | + writeln!(io::stderr(), |
| 133 | + "The Unstable Book has a section '{}' which doesn't correspond \ |
| 134 | + to an unstable feature", |
| 135 | + feature_name) |
| 136 | + .expect("could not write to stderr") |
| 137 | + } |
| 138 | +} |
0 commit comments