]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_driver/src/args.rs
Rollup merge of #82136 - edward-shen:mismatched-subst-and-hir, r=lcnr
[rust.git] / compiler / rustc_driver / src / args.rs
1 use std::error;
2 use std::fmt;
3 use std::fs;
4 use std::io;
5
6 pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
7     if let Some(path) = arg.strip_prefix('@') {
8         let file = match fs::read_to_string(path) {
9             Ok(file) => file,
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
21 #[derive(Debug)]
22 pub enum Error {
23     Utf8Error(Option<String>),
24     IOError(String, io::Error),
25 }
26
27 impl fmt::Display for Error {
28     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
29         match self {
30             Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
31             Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
32             Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
33         }
34     }
35 }
36
37 impl error::Error for Error {}