use std::fs::File; use std::io::Read; use std::fs; use std::path::{Path, PathBuf}; use clap::Parser; fn get_file_as_byte_vec(filename: &PathBuf) -> Vec<u8> { let mut f = File::open(&filename).expect("no file found"); let metadata = fs::metadata(&filename).expect("unable to read metadata"); let mut buffer = vec![0; metadata.len() as usize]; f.read(&mut buffer).expect("buffer overflow"); buffer } /// Simple program to greet a person #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { /// Name of the person to greet file: PathBuf, #[arg(short, long, default_value_t=("bin".to_string()))] view: String } fn main() { let args = Args::parse(); let view = args.view; if view == "bin".to_string() { let bytes = get_file_as_byte_vec(&args.file); let mut name_in_binary = String::new(); for character in bytes { name_in_binary += &format!("byte: {:08b} ", character); } println!("{}", name_in_binary); } else if view == "hex".to_string() { let bytes = get_file_as_byte_vec(&args.file); let mut name_in_binary = String::new(); for character in bytes { name_in_binary += &format!("hex: {:x} ", character); } println!("{}", name_in_binary); } }