Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up rustdoc source code #71900

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/librustdoc/docfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use std::fs;
use std::io;
use std::path::Path;
use std::string::ToString;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToString is in the prelude so doesn't need to be imported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuillaumeGomez did you miss this comment?

use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;

Expand All @@ -25,7 +26,9 @@ macro_rules! try_err {
}

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

pub struct ErrorStorage {
Expand Down
17 changes: 10 additions & 7 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, VecDeque};
use std::default::Default;
use std::error;

use std::ffi::OsStr;
use std::fmt::{self, Formatter, Write};
use std::fs::{self, File};
Expand All @@ -40,6 +39,7 @@ use std::io::{self, BufReader};
use std::path::{Component, Path, PathBuf};
use std::rc::Rc;
use std::str;
use std::string::ToString;
use std::sync::Arc;

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

impl error::Error for Error {}
Expand All @@ -109,8 +109,11 @@ impl std::fmt::Display for Error {
}

impl PathError for Error {
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Error {
Error { file: path.as_ref().to_path_buf(), error: e }
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Error
where
S: ToString + Sized,
{
Error { file: path.as_ref().to_path_buf(), error: e.to_string() }
}
}

Expand Down Expand Up @@ -557,7 +560,7 @@ pub fn run(

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

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

crate::markdown::render(index_page, md_opts, diag, cx.shared.edition);
crate::markdown::render(&index_page, md_opts, cx.shared.edition)
.map_err(|e| Error::new(e, &index_page))?;
} else {
let dst = cx.dst.join("index.html");
let page = layout::Page {
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ fn main_options(options: config::Options) -> i32 {
(true, true) => return markdown::test(options, &diag),
(true, false) => return test::run(options),
(false, true) => {
return markdown::render(options.input, options.render_options, &diag, options.edition);
match markdown::render(&options.input, options.render_options, options.edition) {
Ok(()) => return 0,
Err(err) => {
diag.struct_err(&err).emit();
return 1;
}
}
}
(false, false) => {}
}
Expand Down
39 changes: 12 additions & 27 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::{create_dir_all, File};
use std::fs::{create_dir_all, read_to_string, File};
use std::io::prelude::*;
use std::path::PathBuf;
use std::path::Path;

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

/// Render `input` (e.g., "foo.md") into an HTML file in `output`
/// (e.g., output = "bar" => "bar/foo.html").
pub fn render(
input: PathBuf,
pub fn render<P: AsRef<Path>>(
input: P,
options: RenderOptions,
diag: &rustc_errors::Handler,
edition: Edition,
) -> i32 {
) -> Result<(), String> {
if let Err(e) = create_dir_all(&options.output) {
diag.struct_err(&format!("{}: {}", options.output.display(), e)).emit();
return 4;
return Err(format!("{}: {}", options.output.display(), e));
}

let input = input.as_ref();
let mut output = options.output;
output.push(input.file_name().unwrap());
output.set_extension("html");
Expand All @@ -55,26 +54,15 @@ pub fn render(
css.push_str(&s)
}

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

let mut out = match File::create(&output) {
Err(e) => {
diag.struct_err(&format!("{}: {}", output.display(), e)).emit();
return 4;
}
Ok(f) => f,
};
let mut out = File::create(&output).map_err(|e| format!("{}: {}", output.display(), e))?;

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

Expand Down Expand Up @@ -122,11 +110,8 @@ pub fn render(
);

match err {
Err(e) => {
diag.struct_err(&format!("cannot write to `{}`: {}", output.display(), e)).emit();
6
}
Ok(_) => 0,
Err(e) => Err(format!("cannot write to `{}`: {}", output.display(), e)),
Ok(_) => Ok(()),
}
}

Expand Down