]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_driver_impl/src/args.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / compiler / rustc_driver_impl / src / args.rs
CommitLineData
e1599b0c
XL
1use std::error;
2use std::fmt;
3use std::fs;
4use std::io;
e1599b0c 5
6a06907d 6fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
1b1a35ee 7 if let Some(path) = arg.strip_prefix('@') {
e1599b0c 8 let file = match fs::read_to_string(path) {
60c5eb7d 9 Ok(file) => file,
e1599b0c
XL
10 Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
11 return Err(Error::Utf8Error(Some(path.to_string())));
12 }
13 Err(err) => return Err(Error::IOError(path.to_string(), err)),
14 };
15 Ok(file.lines().map(ToString::to_string).collect())
16 } else {
17 Ok(vec![arg])
18 }
19}
20
6a06907d
XL
21pub fn arg_expand_all(at_args: &[String]) -> Vec<String> {
22 let mut args = Vec::new();
23 for arg in at_args {
24 match arg_expand(arg.clone()) {
25 Ok(arg) => args.extend(arg),
26 Err(err) => rustc_session::early_error(
27 rustc_session::config::ErrorOutputType::default(),
9c376795 28 &format!("Failed to load argument file: {err}"),
6a06907d
XL
29 ),
30 }
31 }
32 args
33}
34
e1599b0c
XL
35#[derive(Debug)]
36pub enum Error {
37 Utf8Error(Option<String>),
38 IOError(String, io::Error),
39}
40
41impl fmt::Display for Error {
42 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match self {
44 Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
9c376795
FG
45 Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {path}"),
46 Error::IOError(path, err) => write!(fmt, "IO Error: {path}: {err}"),
e1599b0c
XL
47 }
48 }
49}
50
dfeec247 51impl error::Error for Error {}