hrtor/
cli.rs

1use clap::Parser;
2use std::path::PathBuf;
3
4#[derive(Debug, PartialEq, Eq)]
5pub struct FileInfo {
6    pub path: PathBuf,
7    pub context: String,
8}
9
10#[derive(Parser)]
11pub struct CLIArgs {
12    /// File's Path
13    #[arg(help = "The file you want to edit")]
14    pub path: PathBuf,
15}
16
17impl CLIArgs {
18    pub fn read_fileinfo(&self) -> FileInfo {
19        FileInfo {
20            path: self.path.clone(),
21            context: std::fs::read_to_string(self.path.clone()).unwrap_or_else(|_| {
22                eprintln!("your file cannot find. create a new buffer to continue this process.");
23                String::new()
24            }),
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::CLIArgs;
32    use super::FileInfo;
33    use std::path::PathBuf;
34
35    #[test]
36    /// How to read FileInfo from CLIArgs struct.
37    fn how_to_read_fileinfo() -> anyhow::Result<()> {
38        let args: CLIArgs = CLIArgs {
39            path: PathBuf::from("test.txt"),
40        };
41
42        let fileinfo: FileInfo = args.read_fileinfo();
43
44        assert_eq!(String::new(), fileinfo.context);
45
46        Ok(())
47    }
48}