use clap::Parser;
use std::path::PathBuf;
#[derive(Debug, PartialEq, Eq)]
pub struct FileInfo {
pub path: PathBuf,
pub context: String,
}
#[derive(Parser)]
pub struct CLIArgs {
#[arg(help = "The file you want to edit")]
pub path: PathBuf,
}
impl CLIArgs {
pub fn read_fileinfo(&self) -> FileInfo {
FileInfo {
path: self.path.clone(),
context: std::fs::read_to_string(self.path.clone()).unwrap_or_else(|_| {
eprintln!("your file cannot find. create a new buffer to continue this process.");
String::new()
}),
}
}
}
#[cfg(test)]
mod tests {
use super::CLIArgs;
use super::FileInfo;
use std::path::PathBuf;
#[test]
fn how_to_read_fileinfo() -> anyhow::Result<()> {
let args: CLIArgs = CLIArgs {
path: PathBuf::from("test.txt"),
};
let fileinfo: FileInfo = args.read_fileinfo();
assert_eq!(String::new(), fileinfo.context);
Ok(())
}
}