Skip to content

Commit 7be14ee

Browse files
committed
Refactor away load_or_return macro.
1 parent f410da5 commit 7be14ee

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/librustdoc/externalfiles.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,41 @@ impl ExternalHtml {
4343
}
4444
}
4545

46-
pub fn load_string(input: &Path) -> io::Result<Option<String>> {
47-
let mut f = File::open(input)?;
48-
let mut d = Vec::new();
49-
f.read_to_end(&mut d)?;
50-
Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
46+
pub enum LoadStringError {
47+
ReadFail,
48+
BadUtf8,
5149
}
5250

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

7473
pub fn load_external_files(names: &[String]) -> Option<String> {
7574
let mut out = String::new();
7675
for name in names {
77-
out.push_str(&*load_or_return!(&name, None, None));
76+
let s = match load_string(name) {
77+
Ok(s) => s,
78+
Err(_) => return None,
79+
};
80+
out.push_str(&s);
7881
out.push('\n');
7982
}
8083
Some(out)

src/librustdoc/markdown.rs

Lines changed: 11 additions & 3 deletions
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)