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.
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.
11 #![feature(rustc_private)]
15 extern crate serialize as rustc_serialize;
17 use std::collections::BTreeMap;
19 use std::error::Error;
20 use std::fs::{read_dir, File};
21 use std::io::{Read, Write};
23 use std::path::PathBuf;
25 use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};
27 use rustdoc::html::markdown::{Markdown, PLAYGROUND};
28 use rustc_serialize::json;
32 Markdown(MarkdownFormatter),
37 fn from(format: &str) -> OutputFormat {
38 match &*format.to_lowercase() {
39 "html" => OutputFormat::HTML(HTMLFormatter),
40 "markdown" => OutputFormat::Markdown(MarkdownFormatter),
41 s => OutputFormat::Unknown(s.to_owned()),
47 fn header(&self, output: &mut Write) -> Result<(), Box<Error>>;
48 fn title(&self, output: &mut Write) -> Result<(), Box<Error>>;
49 fn error_code_block(&self, output: &mut Write, info: &ErrorMetadata,
50 err_code: &str) -> Result<(), Box<Error>>;
51 fn footer(&self, output: &mut Write) -> Result<(), Box<Error>>;
55 struct MarkdownFormatter;
57 impl Formatter for HTMLFormatter {
58 fn header(&self, output: &mut Write) -> Result<(), Box<Error>> {
59 write!(output, r##"<!DOCTYPE html>
62 <title>Rust Compiler Error Index</title>
63 <meta charset="utf-8">
64 <!-- Include rust.css after light.css so its rules take priority. -->
65 <link rel="stylesheet" type="text/css" href="light.css"/>
66 <link rel="stylesheet" type="text/css" href="rust.css"/>
78 fn title(&self, output: &mut Write) -> Result<(), Box<Error>> {
79 write!(output, "<h1>Rust Compiler Error Index</h1>\n")?;
83 fn error_code_block(&self, output: &mut Write, info: &ErrorMetadata,
84 err_code: &str) -> Result<(), Box<Error>> {
85 // Enclose each error in a div so they can be shown/hidden en masse.
86 let desc_desc = match info.description {
87 Some(_) => "error-described",
88 None => "error-undescribed",
90 let use_desc = match info.use_site {
91 Some(_) => "error-used",
92 None => "error-unused",
94 write!(output, "<div class=\"{} {}\">", desc_desc, use_desc)?;
96 // Error title (with self-link).
98 "<h2 id=\"{0}\" class=\"section-header\"><a href=\"#{0}\">{0}</a></h2>\n",
101 // Description rendered as markdown.
102 match info.description {
103 Some(ref desc) => write!(output, "{}", Markdown(desc, &[]))?,
104 None => write!(output, "<p>No description.</p>\n")?,
107 write!(output, "</div>\n")?;
111 fn footer(&self, output: &mut Write) -> Result<(), Box<Error>> {
112 write!(output, r##"<script>
113 function onEach(arr, func) {{
114 if (arr && arr.length > 0 && func) {{
115 for (var i = 0; i < arr.length; i++) {{
121 function hasClass(elem, className) {{
122 if (elem && className && elem.className) {{
123 var elemClass = elem.className;
124 var start = elemClass.indexOf(className);
127 }} else if (elemClass.length === className.length) {{
130 if (start > 0 && elemClass[start - 1] !== ' ') {{
133 var end = start + className.length;
134 if (end < elemClass.length && elemClass[end] !== ' ') {{
139 if (start > 0 && elemClass[start - 1] !== ' ') {{
142 var end = start + className.length;
143 if (end < elemClass.length && elemClass[end] !== ' ') {{
151 onEach(document.getElementsByClassName('rust-example-rendered'), function(e) {{
152 if (hasClass(e, 'compile_fail')) {{
153 e.addEventListener("mouseover", function(event) {{
154 e.previousElementSibling.childNodes[0].style.color = '#f00';
156 e.addEventListener("mouseout", function(event) {{
157 e.previousElementSibling.childNodes[0].style.color = '';
159 }} else if (hasClass(e, 'ignore')) {{
160 e.addEventListener("mouseover", function(event) {{
161 e.previousElementSibling.childNodes[0].style.color = '#ff9200';
163 e.addEventListener("mouseout", function(event) {{
164 e.previousElementSibling.childNodes[0].style.color = '';
175 impl Formatter for MarkdownFormatter {
176 #[allow(unused_variables)]
177 fn header(&self, output: &mut Write) -> Result<(), Box<Error>> {
181 fn title(&self, output: &mut Write) -> Result<(), Box<Error>> {
182 write!(output, "# Rust Compiler Error Index\n")?;
186 fn error_code_block(&self, output: &mut Write, info: &ErrorMetadata,
187 err_code: &str) -> Result<(), Box<Error>> {
188 Ok(match info.description {
189 Some(ref desc) => write!(output, "## {}\n{}\n", err_code, desc)?,
194 #[allow(unused_variables)]
195 fn footer(&self, output: &mut Write) -> Result<(), Box<Error>> {
200 /// Load all the metadata files from `metadata_dir` into an in-memory map.
201 fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<Error>> {
202 let mut all_errors = BTreeMap::new();
204 for entry in read_dir(metadata_dir)? {
205 let path = entry?.path();
207 let mut metadata_str = String::new();
208 File::open(&path).and_then(|mut f| f.read_to_string(&mut metadata_str))?;
210 let some_errors: ErrorMetadataMap = json::decode(&metadata_str)?;
212 for (err_code, info) in some_errors {
213 all_errors.insert(err_code, info);
220 /// Output an HTML page for the errors in `err_map` to `output_path`.
221 fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Path,
222 formatter: T) -> Result<(), Box<Error>> {
223 let mut output_file = File::create(output_path)?;
225 formatter.header(&mut output_file)?;
226 formatter.title(&mut output_file)?;
228 for (err_code, info) in err_map {
229 formatter.error_code_block(&mut output_file, info, err_code)?;
232 formatter.footer(&mut output_file)
235 fn main_with_result(format: OutputFormat, dst: &Path) -> Result<(), Box<Error>> {
236 let build_arch = env::var("CFG_BUILD")?;
237 let metadata_dir = get_metadata_dir(&build_arch);
238 let err_map = load_all_errors(&metadata_dir)?;
240 OutputFormat::Unknown(s) => panic!("Unknown output format: {}", s),
241 OutputFormat::HTML(h) => render_error_page(&err_map, dst, h)?,
242 OutputFormat::Markdown(m) => render_error_page(&err_map, dst, m)?,
247 fn parse_args() -> (OutputFormat, PathBuf) {
248 let mut args = env::args().skip(1);
249 let format = args.next().map(|a| OutputFormat::from(&a))
250 .unwrap_or(OutputFormat::from("html"));
251 let dst = args.next().map(PathBuf::from).unwrap_or_else(|| {
253 OutputFormat::HTML(..) => PathBuf::from("doc/error-index.html"),
254 OutputFormat::Markdown(..) => PathBuf::from("doc/error-index.md"),
255 OutputFormat::Unknown(..) => PathBuf::from("<nul>"),
262 PLAYGROUND.with(|slot| {
263 *slot.borrow_mut() = Some((None, String::from("https://play.rust-lang.org/")));
265 let (format, dst) = parse_args();
266 let result = syntax::with_globals(move || {
267 main_with_result(format, &dst)
269 if let Err(e) = result {
270 panic!("{}", e.description());