binhex-rs 0.1.0

Crate to read BinHex 4 encoded files
Documentation
use clap::Parser;

#[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);
        }
    };

    match reader.verify() {
        Ok(()) => {
            println!("{} is valid", cli.file.display());
            std::process::exit(0);
        }
        Err(binhex::VerificationError::ChecksumMismatch(binhex::Checksum::Header)) => {
            println!("{} has a corrupted header", cli.file.display());
            std::process::exit(1);
        }
        Err(binhex::VerificationError::ChecksumMismatch(binhex::Checksum::DataFork)) => {
            println!("{} has a corrupted data fork", cli.file.display());
            std::process::exit(1);
        }
        Err(binhex::VerificationError::ChecksumMismatch(binhex::Checksum::ResourceFork)) => {
            println!("{} has a corrupted resource fork", cli.file.display());
            std::process::exit(1);
        }
        Err(other) => {
            eprintln!("Could not validate file at {}", cli.file.display());
            if cli.verbose {
                eprintln!("{:?}", other);
            }
            std::process::exit(-3);
        }
    }
}