2019-08-19 19:02:12 -07:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
|
|
|
|
2021-02-18 16:15:24 +01:00
|
|
|
fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
|
2020-09-17 10:13:16 +02:00
|
|
|
if let Some(path) = arg.strip_prefix('@') {
|
2019-08-19 19:02:12 -07:00
|
|
|
let file = match fs::read_to_string(path) {
|
2019-11-05 17:56:37 -08:00
|
|
|
Ok(file) => file,
|
2019-08-19 19:02:12 -07:00
|
|
|
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
|
|
|
|
return Err(Error::Utf8Error(Some(path.to_string())));
|
|
|
|
}
|
|
|
|
Err(err) => return Err(Error::IOError(path.to_string(), err)),
|
|
|
|
};
|
|
|
|
Ok(file.lines().map(ToString::to_string).collect())
|
|
|
|
} else {
|
|
|
|
Ok(vec![arg])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:15:24 +01:00
|
|
|
pub fn arg_expand_all(at_args: &[String]) -> Vec<String> {
|
|
|
|
let mut args = Vec::new();
|
|
|
|
for arg in at_args {
|
|
|
|
match arg_expand(arg.clone()) {
|
|
|
|
Ok(arg) => args.extend(arg),
|
|
|
|
Err(err) => rustc_session::early_error(
|
|
|
|
rustc_session::config::ErrorOutputType::default(),
|
|
|
|
&format!("Failed to load argument file: {}", err),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
|
|
|
|
2019-08-19 19:02:12 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Utf8Error(Option<String>),
|
|
|
|
IOError(String, io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
|
|
|
|
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
|
|
|
|
Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 20:01:48 -08:00
|
|
|
impl error::Error for Error {}
|