Skip to content

Commit 7e855b5

Browse files
Clean up rustdoc source code
1 parent 97f3eee commit 7e855b5

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

src/librustdoc/docfs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use std::fs;
1313
use std::io;
1414
use std::path::Path;
15+
use std::string::ToString;
1516
use std::sync::mpsc::{channel, Receiver, Sender};
1617
use std::sync::Arc;
1718

@@ -25,7 +26,9 @@ macro_rules! try_err {
2526
}
2627

2728
pub trait PathError {
28-
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Self;
29+
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Self
30+
where
31+
S: ToString + Sized;
2932
}
3033

3134
pub struct ErrorStorage {

src/librustdoc/html/render.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use std::cmp::Ordering;
3131
use std::collections::{BTreeMap, VecDeque};
3232
use std::default::Default;
3333
use std::error;
34-
3534
use std::ffi::OsStr;
3635
use std::fmt::{self, Formatter, Write};
3736
use std::fs::{self, File};
@@ -40,6 +39,7 @@ use std::io::{self, BufReader};
4039
use std::path::{Component, Path, PathBuf};
4140
use std::rc::Rc;
4241
use std::str;
42+
use std::string::ToString;
4343
use std::sync::Arc;
4444

4545
use rustc_ast_pretty::pprust;
@@ -92,7 +92,7 @@ crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
9292
#[derive(Debug)]
9393
pub struct Error {
9494
pub file: PathBuf,
95-
pub error: io::Error,
95+
pub error: String,
9696
}
9797

9898
impl error::Error for Error {}
@@ -109,8 +109,11 @@ impl std::fmt::Display for Error {
109109
}
110110

111111
impl PathError for Error {
112-
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Error {
113-
Error { file: path.as_ref().to_path_buf(), error: e }
112+
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Error
113+
where
114+
S: ToString + Sized,
115+
{
116+
Error { file: path.as_ref().to_path_buf(), error: e.to_string() }
114117
}
115118
}
116119

@@ -557,7 +560,7 @@ pub fn run(
557560

558561
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
559562
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
560-
write_shared(&cx, &krate, index, &md_opts, diag)?;
563+
write_shared(&cx, &krate, index, &md_opts)?;
561564
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
562565

563566
// And finally render the whole crate's documentation
@@ -577,7 +580,6 @@ fn write_shared(
577580
krate: &clean::Crate,
578581
search_index: String,
579582
options: &RenderOptions,
580-
diag: &rustc_errors::Handler,
581583
) -> Result<(), Error> {
582584
// Write out the shared files. Note that these are shared among all rustdoc
583585
// docs placed in the output directory, so this needs to be a synchronized
@@ -960,7 +962,8 @@ themePicker.onblur = handleThemeButtonsBlur;
960962
md_opts.output = cx.dst.clone();
961963
md_opts.external_html = (*cx.shared).layout.external_html.clone();
962964

963-
crate::markdown::render(index_page, md_opts, diag, cx.shared.edition);
965+
crate::markdown::render(&index_page, md_opts, cx.shared.edition)
966+
.map_err(|e| Error::new(e, &index_page))?;
964967
} else {
965968
let dst = cx.dst.join("index.html");
966969
let page = layout::Page {

src/librustdoc/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,13 @@ fn main_options(options: config::Options) -> i32 {
457457
(true, true) => return markdown::test(options, &diag),
458458
(true, false) => return test::run(options),
459459
(false, true) => {
460-
return markdown::render(options.input, options.render_options, &diag, options.edition);
460+
match markdown::render(&options.input, options.render_options, options.edition) {
461+
Ok(()) => return 0,
462+
Err(err) => {
463+
diag.struct_err(&err).emit();
464+
return 1;
465+
}
466+
}
461467
}
462468
(false, false) => {}
463469
}

src/librustdoc/markdown.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fs::{create_dir_all, File};
1+
use std::fs::{create_dir_all, read_to_string, File};
22
use std::io::prelude::*;
3-
use std::path::PathBuf;
3+
use std::path::Path;
44

55
use rustc_feature::UnstableFeatures;
66
use rustc_span::edition::Edition;
@@ -34,17 +34,16 @@ fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
3434

3535
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
3636
/// (e.g., output = "bar" => "bar/foo.html").
37-
pub fn render(
38-
input: PathBuf,
37+
pub fn render<P: AsRef<Path>>(
38+
input: P,
3939
options: RenderOptions,
40-
diag: &rustc_errors::Handler,
4140
edition: Edition,
42-
) -> i32 {
41+
) -> Result<(), String> {
4342
if let Err(e) = create_dir_all(&options.output) {
44-
diag.struct_err(&format!("{}: {}", options.output.display(), e)).emit();
45-
return 4;
43+
return Err(format!("{}: {}", options.output.display(), e));
4644
}
4745

46+
let input = input.as_ref();
4847
let mut output = options.output;
4948
output.push(input.file_name().unwrap());
5049
output.set_extension("html");
@@ -55,26 +54,15 @@ pub fn render(
5554
css.push_str(&s)
5655
}
5756

58-
let input_str = match load_string(&input, diag) {
59-
Ok(s) => s,
60-
Err(LoadStringError::ReadFail) => return 1,
61-
Err(LoadStringError::BadUtf8) => return 2,
62-
};
57+
let input_str = read_to_string(input).map_err(|err| format!("{}: {}", input.display(), err))?;
6358
let playground_url = options.markdown_playground_url.or(options.playground_url);
6459
let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
6560

66-
let mut out = match File::create(&output) {
67-
Err(e) => {
68-
diag.struct_err(&format!("{}: {}", output.display(), e)).emit();
69-
return 4;
70-
}
71-
Ok(f) => f,
72-
};
61+
let mut out = File::create(&output).map_err(|e| format!("{}: {}", output.display(), e))?;
7362

7463
let (metadata, text) = extract_leading_metadata(&input_str);
7564
if metadata.is_empty() {
76-
diag.struct_err("invalid markdown file: no initial lines starting with `# ` or `%`").emit();
77-
return 5;
65+
return Err("invalid markdown file: no initial lines starting with `# ` or `%`".to_owned());
7866
}
7967
let title = metadata[0];
8068

@@ -122,11 +110,8 @@ pub fn render(
122110
);
123111

124112
match err {
125-
Err(e) => {
126-
diag.struct_err(&format!("cannot write to `{}`: {}", output.display(), e)).emit();
127-
6
128-
}
129-
Ok(_) => 0,
113+
Err(e) => Err(format!("cannot write to `{}`: {}", output.display(), e)),
114+
Ok(_) => Ok(()),
130115
}
131116
}
132117

0 commit comments

Comments
 (0)