Refactor away load_or_return
macro.
This commit is contained in:
parent
f410da5cbe
commit
7be14eea94
@ -43,30 +43,29 @@ impl ExternalHtml {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_string(input: &Path) -> io::Result<Option<String>> {
|
pub enum LoadStringError {
|
||||||
let mut f = File::open(input)?;
|
ReadFail,
|
||||||
let mut d = Vec::new();
|
BadUtf8,
|
||||||
f.read_to_end(&mut d)?;
|
|
||||||
Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! load_or_return {
|
pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> {
|
||||||
($input: expr, $cant_read: expr, $not_utf8: expr) => {
|
let file_path = file_path.as_ref();
|
||||||
{
|
let mut contents = vec![];
|
||||||
let input = Path::new(&$input[..]);
|
let result = File::open(file_path)
|
||||||
match ::externalfiles::load_string(input) {
|
.and_then(|mut f| f.read_to_end(&mut contents));
|
||||||
Err(e) => {
|
if let Err(e) = result {
|
||||||
let _ = writeln!(&mut io::stderr(),
|
let _ = writeln!(&mut io::stderr(),
|
||||||
"error reading `{}`: {}", input.display(), e);
|
"error reading `{}`: {}",
|
||||||
return $cant_read;
|
file_path.display(), e);
|
||||||
}
|
return Err(LoadStringError::ReadFail);
|
||||||
Ok(None) => {
|
}
|
||||||
let _ = writeln!(&mut io::stderr(),
|
match str::from_utf8(&contents) {
|
||||||
"error reading `{}`: not UTF-8", input.display());
|
Ok(s) => Ok(s.to_string()),
|
||||||
return $not_utf8;
|
Err(_) => {
|
||||||
}
|
let _ = writeln!(&mut io::stderr(),
|
||||||
Ok(Some(s)) => s
|
"error reading `{}`: not UTF-8",
|
||||||
}
|
file_path.display());
|
||||||
|
Err(LoadStringError::BadUtf8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,7 +73,11 @@ macro_rules! load_or_return {
|
|||||||
pub fn load_external_files(names: &[String]) -> Option<String> {
|
pub fn load_external_files(names: &[String]) -> Option<String> {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
for name in names {
|
for name in names {
|
||||||
out.push_str(&*load_or_return!(&name, None, None));
|
let s = match load_string(name) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => return None,
|
||||||
|
};
|
||||||
|
out.push_str(&s);
|
||||||
out.push('\n');
|
out.push('\n');
|
||||||
}
|
}
|
||||||
Some(out)
|
Some(out)
|
||||||
|
@ -19,7 +19,7 @@ use testing;
|
|||||||
use rustc::session::search_paths::SearchPaths;
|
use rustc::session::search_paths::SearchPaths;
|
||||||
use rustc::session::config::Externs;
|
use rustc::session::config::Externs;
|
||||||
|
|
||||||
use externalfiles::ExternalHtml;
|
use externalfiles::{ExternalHtml, LoadStringError, load_string};
|
||||||
|
|
||||||
use html::render::reset_ids;
|
use html::render::reset_ids;
|
||||||
use html::escape::Escape;
|
use html::escape::Escape;
|
||||||
@ -58,7 +58,11 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
|
|||||||
css.push_str(&s)
|
css.push_str(&s)
|
||||||
}
|
}
|
||||||
|
|
||||||
let input_str = load_or_return!(input, 1, 2);
|
let input_str = match load_string(input) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(LoadStringError::ReadFail) => return 1,
|
||||||
|
Err(LoadStringError::BadUtf8) => return 2,
|
||||||
|
};
|
||||||
let playground = matches.opt_str("markdown-playground-url");
|
let playground = matches.opt_str("markdown-playground-url");
|
||||||
if playground.is_some() {
|
if playground.is_some() {
|
||||||
markdown::PLAYGROUND_KRATE.with(|s| { *s.borrow_mut() = Some(None); });
|
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,
|
|||||||
/// Run any tests/code examples in the markdown file `input`.
|
/// Run any tests/code examples in the markdown file `input`.
|
||||||
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
|
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
|
||||||
mut test_args: Vec<String>) -> isize {
|
mut test_args: Vec<String>) -> isize {
|
||||||
let input_str = load_or_return!(input, 1, 2);
|
let input_str = match load_string(input) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(LoadStringError::ReadFail) => return 1,
|
||||||
|
Err(LoadStringError::BadUtf8) => return 2,
|
||||||
|
};
|
||||||
|
|
||||||
let mut opts = TestOptions::default();
|
let mut opts = TestOptions::default();
|
||||||
opts.no_crate_inject = true;
|
opts.no_crate_inject = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user