]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/args.rs
Deprecate Error::description for real
[rust.git] / src / librustc_driver / 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 arg.starts_with("@") {
8         let path = &arg[1..];
9         let file = match fs::read_to_string(path) {
10             Ok(file) => file,
11             Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
12                 return Err(Error::Utf8Error(Some(path.to_string())));
13             }
14             Err(err) => return Err(Error::IOError(path.to_string(), err)),
15         };
16         Ok(file.lines().map(ToString::to_string).collect())
17     } else {
18         Ok(vec![arg])
19     }
20 }
21
22 #[derive(Debug)]
23 pub enum Error {
24     Utf8Error(Option<String>),
25     IOError(String, io::Error),
26 }
27
28 impl fmt::Display for Error {
29     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
30         match self {
31             Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
32             Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
33             Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
34         }
35     }
36 }
37
38 impl error::Error for Error {}