summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_driver_impl/src/args.rs
blob: 42c97cc6a9d7418be734945a66ed3009a5004337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::error;
use std::fmt;
use std::fs;
use std::io;

fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
    if let Some(path) = arg.strip_prefix('@') {
        let file = match fs::read_to_string(path) {
            Ok(file) => file,
            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])
    }
}

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
}

#[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}"),
        }
    }
}

impl error::Error for Error {}