]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/args.rs
Rollup merge of #65263 - mbStavola:dedup-raw-item-fns, r=Centril
[rust.git] / src / librustc_driver / args.rs
1 use std::error;
2 use std::fmt;
3 use std::fs;
4 use std::io;
5 use std::str;
6 use std::sync::atomic::{AtomicBool, Ordering};
7
8 static USED_ARGSFILE_FEATURE: AtomicBool = AtomicBool::new(false);
9
10 pub fn used_unstable_argsfile() -> bool {
11     USED_ARGSFILE_FEATURE.load(Ordering::Relaxed)
12 }
13
14 pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
15     if arg.starts_with("@") {
16         let path = &arg[1..];
17         let file = match fs::read_to_string(path) {
18             Ok(file) => {
19                 USED_ARGSFILE_FEATURE.store(true, Ordering::Relaxed);
20                 file
21             }
22             Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
23                 return Err(Error::Utf8Error(Some(path.to_string())));
24             }
25             Err(err) => return Err(Error::IOError(path.to_string(), err)),
26         };
27         Ok(file.lines().map(ToString::to_string).collect())
28     } else {
29         Ok(vec![arg])
30     }
31 }
32
33 #[derive(Debug)]
34 pub enum Error {
35     Utf8Error(Option<String>),
36     IOError(String, io::Error),
37 }
38
39 impl fmt::Display for Error {
40     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
41         match self {
42             Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
43             Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
44             Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
45         }
46     }
47 }
48
49 impl error::Error for Error {
50     fn description(&self) -> &'static str {
51         "argument error"
52     }
53 }