Read an entire file into a string.
(path: P)
| 8 | |
| 9 | /// Read an entire file into a string. |
| 10 | pub fn read_to_string<P: AsRef<Path>>(path: P) -> anyhow::Result<String> { |
| 11 | let mut buffer = String::new(); |
| 12 | let path = path.as_ref(); |
| 13 | if path == Path::new("-") { |
| 14 | let stdin = io::stdin(); |
| 15 | let mut stdin = stdin.lock(); |
| 16 | stdin |
| 17 | .read_to_string(&mut buffer) |
| 18 | .context("failed to read stdin to string")?; |
| 19 | } else { |
| 20 | let mut file = File::open(path)?; |
| 21 | file.read_to_string(&mut buffer) |
| 22 | .with_context(|| format!("failed to read {} to string", path.display()))?; |
| 23 | } |
| 24 | Ok(buffer) |
| 25 | } |
| 26 | |
| 27 | /// Iterate over all of the files passed as arguments, recursively iterating through directories. |
| 28 | pub fn iterate_files<'a>(files: &'a [PathBuf]) -> impl Iterator<Item = PathBuf> + 'a { |