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