]> git.lizzy.rs Git - rust.git/blob - src/error-index-generator/main.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[rust.git] / src / error-index-generator / main.rs
1 // Copyright 2015 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 #![feature(rustc_private, rustdoc)]
12
13 extern crate syntax;
14 extern crate rustdoc;
15 extern crate serialize as rustc_serialize;
16
17 use std::collections::BTreeMap;
18 use std::fs::{read_dir, File};
19 use std::io::{Read, Write};
20 use std::env;
21 use std::path::Path;
22 use std::error::Error;
23
24 use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap};
25
26 use rustdoc::html::markdown::Markdown;
27 use rustc_serialize::json;
28
29 /// Load all the metadata files from `metadata_dir` into an in-memory map.
30 fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<Error>> {
31     let mut all_errors = BTreeMap::new();
32
33     for entry in try!(read_dir(metadata_dir)) {
34         let path = try!(entry).path();
35
36         let mut metadata_str = String::new();
37         try!(File::open(&path).and_then(|mut f| f.read_to_string(&mut metadata_str)));
38
39         let some_errors: ErrorMetadataMap = try!(json::decode(&metadata_str));
40
41         for (err_code, info) in some_errors {
42             all_errors.insert(err_code, info);
43         }
44     }
45
46     Ok(all_errors)
47 }
48
49 /// Output an HTML page for the errors in `err_map` to `output_path`.
50 fn render_error_page(err_map: &ErrorMetadataMap, output_path: &Path) -> Result<(), Box<Error>> {
51     let mut output_file = try!(File::create(output_path));
52
53     try!(write!(&mut output_file,
54 r##"<!DOCTYPE html>
55 <html>
56 <head>
57 <title>Rust Compiler Error Index</title>
58 <meta charset="utf-8">
59 <!-- Include rust.css after main.css so its rules take priority. -->
60 <link rel="stylesheet" type="text/css" href="main.css"/>
61 <link rel="stylesheet" type="text/css" href="rust.css"/>
62 <style>
63 .error-undescribed {{
64     display: none;
65 }}
66 </style>
67 </head>
68 <body>
69 "##
70     ));
71
72     try!(write!(&mut output_file, "<h1>Rust Compiler Error Index</h1>\n"));
73
74     for (err_code, info) in err_map {
75         // Enclose each error in a div so they can be shown/hidden en masse.
76         let desc_desc = match info.description {
77             Some(_) => "error-described",
78             None => "error-undescribed",
79         };
80         let use_desc = match info.use_site {
81             Some(_) => "error-used",
82             None => "error-unused",
83         };
84         try!(write!(&mut output_file, "<div class=\"{} {}\">", desc_desc, use_desc));
85
86         // Error title (with self-link).
87         try!(write!(&mut output_file,
88                     "<h2 id=\"{0}\" class=\"section-header\"><a href=\"#{0}\">{0}</a></h2>\n",
89                     err_code));
90
91         // Description rendered as markdown.
92         match info.description {
93             Some(ref desc) => try!(write!(&mut output_file, "{}", Markdown(desc))),
94             None => try!(write!(&mut output_file, "<p>No description.</p>\n")),
95         }
96
97         try!(write!(&mut output_file, "</div>\n"));
98     }
99
100     try!(write!(&mut output_file, "</body>\n</html>"));
101
102     Ok(())
103 }
104
105 fn main_with_result() -> Result<(), Box<Error>> {
106     let build_arch = try!(env::var("CFG_BUILD"));
107     let metadata_dir = get_metadata_dir(&build_arch);
108     let err_map = try!(load_all_errors(&metadata_dir));
109     try!(render_error_page(&err_map, Path::new("doc/error-index.html")));
110     Ok(())
111 }
112
113 fn main() {
114     if let Err(e) = main_with_result() {
115         panic!("{}", e.description());
116     }
117 }