Skip to content

Commit 9289e41

Browse files
author
Jonathan Turner
authored
Rollup merge of #37050 - frewsxcv:librustdoc, r=alexcrichton
librustdoc refactoring and cleanup.
2 parents fa1140b + e4f066f commit 9289e41

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

src/librustdoc/externalfiles.rs

+50-35
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,79 @@
1111
use std::fs::File;
1212
use std::io::prelude::*;
1313
use std::io;
14-
use std::path::{PathBuf, Path};
14+
use std::path::Path;
1515
use std::str;
1616

1717
#[derive(Clone)]
1818
pub struct ExternalHtml{
19+
/// Content that will be included inline in the <head> section of a
20+
/// rendered Markdown file or generated documentation
1921
pub in_header: String,
22+
/// Content that will be included inline between <body> and the content of
23+
/// a rendered Markdown file or generated documentation
2024
pub before_content: String,
25+
/// Content that will be included inline between the content and </body> of
26+
/// a rendered Markdown file or generated documentation
2127
pub after_content: String
2228
}
2329

2430
impl ExternalHtml {
2531
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
2632
-> Option<ExternalHtml> {
27-
match (load_external_files(in_header),
28-
load_external_files(before_content),
29-
load_external_files(after_content)) {
30-
(Some(ih), Some(bc), Some(ac)) => Some(ExternalHtml {
31-
in_header: ih,
32-
before_content: bc,
33-
after_content: ac
34-
}),
35-
_ => None
36-
}
33+
load_external_files(in_header)
34+
.and_then(|ih|
35+
load_external_files(before_content)
36+
.map(|bc| (ih, bc))
37+
)
38+
.and_then(|(ih, bc)|
39+
load_external_files(after_content)
40+
.map(|ac| (ih, bc, ac))
41+
)
42+
.map(|(ih, bc, ac)|
43+
ExternalHtml {
44+
in_header: ih,
45+
before_content: bc,
46+
after_content: ac,
47+
}
48+
)
3749
}
3850
}
3951

40-
pub fn load_string(input: &Path) -> io::Result<Option<String>> {
41-
let mut f = File::open(input)?;
42-
let mut d = Vec::new();
43-
f.read_to_end(&mut d)?;
44-
Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
52+
pub enum LoadStringError {
53+
ReadFail,
54+
BadUtf8,
4555
}
4656

47-
macro_rules! load_or_return {
48-
($input: expr, $cant_read: expr, $not_utf8: expr) => {
49-
{
50-
let input = PathBuf::from(&$input[..]);
51-
match ::externalfiles::load_string(&input) {
52-
Err(e) => {
53-
let _ = writeln!(&mut io::stderr(),
54-
"error reading `{}`: {}", input.display(), e);
55-
return $cant_read;
56-
}
57-
Ok(None) => {
58-
let _ = writeln!(&mut io::stderr(),
59-
"error reading `{}`: not UTF-8", input.display());
60-
return $not_utf8;
61-
}
62-
Ok(Some(s)) => s
63-
}
57+
pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> {
58+
let file_path = file_path.as_ref();
59+
let mut contents = vec![];
60+
let result = File::open(file_path)
61+
.and_then(|mut f| f.read_to_end(&mut contents));
62+
if let Err(e) = result {
63+
let _ = writeln!(&mut io::stderr(),
64+
"error reading `{}`: {}",
65+
file_path.display(), e);
66+
return Err(LoadStringError::ReadFail);
67+
}
68+
match str::from_utf8(&contents) {
69+
Ok(s) => Ok(s.to_string()),
70+
Err(_) => {
71+
let _ = writeln!(&mut io::stderr(),
72+
"error reading `{}`: not UTF-8",
73+
file_path.display());
74+
Err(LoadStringError::BadUtf8)
6475
}
6576
}
6677
}
6778

68-
pub fn load_external_files(names: &[String]) -> Option<String> {
79+
fn load_external_files(names: &[String]) -> Option<String> {
6980
let mut out = String::new();
7081
for name in names {
71-
out.push_str(&*load_or_return!(&name, None, None));
82+
let s = match load_string(name) {
83+
Ok(s) => s,
84+
Err(_) => return None,
85+
};
86+
out.push_str(&s);
7287
out.push('\n');
7388
}
7489
Some(out)

src/librustdoc/markdown.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use testing;
1919
use rustc::session::search_paths::SearchPaths;
2020
use rustc::session::config::Externs;
2121

22-
use externalfiles::ExternalHtml;
22+
use externalfiles::{ExternalHtml, LoadStringError, load_string};
2323

2424
use html::render::reset_ids;
2525
use html::escape::Escape;
@@ -58,7 +58,11 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
5858
css.push_str(&s)
5959
}
6060

61-
let input_str = load_or_return!(input, 1, 2);
61+
let input_str = match load_string(input) {
62+
Ok(s) => s,
63+
Err(LoadStringError::ReadFail) => return 1,
64+
Err(LoadStringError::BadUtf8) => return 2,
65+
};
6266
let playground = matches.opt_str("markdown-playground-url");
6367
if playground.is_some() {
6468
markdown::PLAYGROUND_KRATE.with(|s| { *s.borrow_mut() = Some(None); });
@@ -144,7 +148,11 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
144148
/// Run any tests/code examples in the markdown file `input`.
145149
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
146150
mut test_args: Vec<String>) -> isize {
147-
let input_str = load_or_return!(input, 1, 2);
151+
let input_str = match load_string(input) {
152+
Ok(s) => s,
153+
Err(LoadStringError::ReadFail) => return 1,
154+
Err(LoadStringError::BadUtf8) => return 2,
155+
};
148156

149157
let mut opts = TestOptions::default();
150158
opts.no_crate_inject = true;

0 commit comments

Comments
 (0)