binhex-rs 0.1.0

Crate to read BinHex 4 encoded files
Documentation
use clap::Parser;
use humansize::{format_size, DECIMAL};

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
    file: std::path::PathBuf,
    #[arg(short, long)]
    verbose: bool,
}

pub fn main() {
    pretty_env_logger::init();
    let cli = Cli::parse();

    let file = match std::fs::File::open(&cli.file) {
        Ok(file) => file,
        Err(e) => {
            eprintln!("Could not open file {:?}", cli.file.display());
            if cli.verbose {
                eprintln!("{:?}", e);
            }
            std::process::exit(-1);
        }
    };

    let mut reader = match binhex::Archive::try_from(file) {
        Ok(reader) => reader,
        Err(e) => {
            eprintln!("Could not find HQX header in {}", cli.file.display());
            if cli.verbose {
                eprintln!("{:?}", e);
            }
            std::process::exit(-2);
        }
    };

    println!("Info for hqx file at {}", cli.file.display());
    println!("Filename:  {}", reader.name());
    println!("Type:      {}", reader.file_code());
    println!("Creator:   {}", reader.creator_code());
    println!("Flags:     {:?}", reader.finder_flags());
    println!("Data:      {}", format_size(reader.data_len(), DECIMAL));
    println!("Rsrc Fork: {}", format_size(reader.resource_len(), DECIMAL));
    println!(
        "Checksums: {}",
        match reader.verify() {
            Ok(()) => "valid",
            Err(binhex::VerificationError::ChecksumMismatch(_)) => "invalid",
            Err(_) => "calculation failed",
        }
    );
}