clap_adapters/
fs.rs

1use std::path::PathBuf;
2
3use crate::traits::FromReader;
4
5/// An adapter for automatically loading the contents of a file path
6#[derive(Debug, Clone)]
7pub struct PathTo<T> {
8    /// The path given as an argument by the user
9    pub path: PathBuf,
10
11    /// The data extracted from the file at the path
12    pub data: T,
13}
14
15impl<T: FromReader> std::str::FromStr for PathTo<T> {
16    type Err = anyhow::Error;
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        let path = PathBuf::from(s);
19        let file = std::fs::File::open(&path)?;
20        let mut reader = std::io::BufReader::new(file);
21        let data = T::from_reader(&mut reader)?;
22        let item = PathTo { path, data };
23        Ok(item)
24    }
25}