diff --git a/Cargo.lock b/Cargo.lock index 9d9cd47..8c3ad03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,12 +51,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "anyhow" -version = "1.0.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" - [[package]] name = "byteorder" version = "1.5.0" @@ -186,7 +180,6 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" name = "nasm" version = "0.1.0" dependencies = [ - "anyhow", "clap", "xixanta", ] @@ -266,7 +259,6 @@ dependencies = [ name = "readrom" version = "1.0.0" dependencies = [ - "anyhow", "clap", "header", ] @@ -456,7 +448,6 @@ dependencies = [ name = "xa65" version = "0.1.0" dependencies = [ - "anyhow", "clap", "rand", ] diff --git a/crates/nasm/Cargo.toml b/crates/nasm/Cargo.toml index 110e5ad..97d5439 100644 --- a/crates/nasm/Cargo.toml +++ b/crates/nasm/Cargo.toml @@ -5,6 +5,5 @@ edition = "2021" authors = ["Miquel Sabaté Solà "] [dependencies] -anyhow = "^1" clap = { version = "^4", features = ["derive"] } xixanta.workspace = true diff --git a/crates/readrom/Cargo.toml b/crates/readrom/Cargo.toml index 98a5106..68ca68c 100644 --- a/crates/readrom/Cargo.toml +++ b/crates/readrom/Cargo.toml @@ -7,6 +7,5 @@ license.workspace = true authors.workspace = true [dependencies] -anyhow = "^1" clap = { version = "^4", features = ["derive"] } header.workspace = true diff --git a/crates/readrom/src/main.rs b/crates/readrom/src/main.rs index 56e60e0..32e648b 100644 --- a/crates/readrom/src/main.rs +++ b/crates/readrom/src/main.rs @@ -1,4 +1,3 @@ -use anyhow::{bail, Result}; use clap::Parser as ClapParser; use header::{Header, Kind}; use std::fs::File; @@ -33,23 +32,33 @@ fn print_header(header: &Header) { println!(" Mapper:\t{}", header.mapper); } -fn main() -> Result<()> { +// Print the given `message` and exit(1). +fn die(message: String) { + println!("{}", message); + std::process::exit(1); +} + +fn main() { let args = Args::parse(); - let mut input = File::open(args.file)?; + let Ok(mut input) = File::open(&args.file) else { + die(format!("failed to open the given file '{}'", &args.file)); + return; + }; let mut buf = vec![0u8; 0x10]; if let Err(e) = input.read_exact(&mut buf) { match e.kind() { - ErrorKind::UnexpectedEof => bail!("malformed ROM file."), - _ => return Err(e.into()), + ErrorKind::UnexpectedEof => die("malformed ROM file".to_string()), + _ => die(e.to_string()), } } let header = match Header::try_from(buf.as_slice()) { Ok(h) => h, - Err(e) => bail!("{}.", e), + Err(e) => { + die(e.to_string()); + return; + } }; print_header(&header); - - Ok(()) } diff --git a/crates/xa65/Cargo.toml b/crates/xa65/Cargo.toml index f268943..a6696bd 100644 --- a/crates/xa65/Cargo.toml +++ b/crates/xa65/Cargo.toml @@ -5,6 +5,5 @@ edition = "2021" authors = ["Miquel Sabaté Solà "] [dependencies] -anyhow = "^1" clap = { version = "^4", features = ["derive"] } rand = "0.8.5" diff --git a/crates/xa65/src/main.rs b/crates/xa65/src/main.rs index 9cb8a70..a937d24 100644 --- a/crates/xa65/src/main.rs +++ b/crates/xa65/src/main.rs @@ -1,4 +1,3 @@ -use anyhow::{bail, Context, Result}; use clap::Parser as ClapParser; use rand::distributions::{Alphanumeric, DistString}; use std::path::PathBuf; @@ -23,6 +22,12 @@ struct Args { out: String, } +// Print the given `message` and exit(1). +fn die(message: String) { + eprintln!("error: {}", message); + std::process::exit(1); +} + // Find the binary by `name` in "PATH". Implementation taken from: // https://stackoverflow.com/a/37499032. fn find_binary(name: &str) -> Option { @@ -41,40 +46,52 @@ fn find_binary(name: &str) -> Option { } // Returns the path for the binaries for 'nasm' and 'cl65'. -fn get_binaries() -> Result<(PathBuf, PathBuf)> { +fn get_binaries() -> Result<(PathBuf, PathBuf), String> { let nasm = match find_binary("nasm") { Some(nasm) => nasm, - None => bail!("could not find 'nasm'".to_string()), + None => return Err("could not find 'nasm'".to_string()), }; let cl65 = match find_binary("cl65") { Some(cl65) => cl65, - None => bail!("could not find 'cl65'".to_string()), + None => return Err("could not find 'cl65'".to_string()), }; Ok((nasm, cl65)) } -fn main() -> Result<()> { +fn main() { // Make sure that the binaries are there. - let (nasm, cl65) = get_binaries()?; + let (nasm, cl65) = match get_binaries() { + Ok((nasm, cl65)) => (nasm, cl65), + Err(e) => { + die(e); + return; + } + }; let args = Args::parse(); // Generate a temporary directory in which both binary files will be placed // as an intermediate step. let random_string = &Alphanumeric.sample_string(&mut rand::thread_rng(), 16); let dir = std::env::temp_dir().join(random_string); - std::fs::create_dir(&dir)?; + if let Err(e) = std::fs::create_dir(&dir) { + die(e.to_string()); + return; + } // Run 'nasm' with the given arguments. Note that we don't care whether // 'nasm' itself errors out. - let _ = Command::new(nasm) + if let Err(e) = Command::new(nasm) .arg(&args.file) .arg("-o") .arg(dir.join("nasm.nes")) .arg("-c") .arg(args.config.clone().unwrap_or("nrom65".to_string())) .status() - .with_context(|| "could not execute 'nasm'")?; + { + die(e.to_string()); + return; + } // Run 'cl65' with the given arguments. let mut cl65_command = Command::new(cl65); @@ -90,35 +107,45 @@ fn main() -> Result<()> { // Here, and in contrast with the 'nasm' execution, we do care about the // exit code of 'cl65'. - let out = cl65_command - .status() - .with_context(|| "could not execute 'cl65'")?; - if !out.success() { - std::process::exit(1); + match cl65_command.status() { + Ok(cmd) => { + if !cmd.success() { + std::process::exit(1); + } + } + Err(e) => { + die(e.to_string()); + return; + } } // Everything went fine, we should have both binaries available to be // compared. For 'diff' actually capture the output so it does not pollute // the shell. - let diff = Command::new("diff") + match Command::new("diff") .arg(dir.join("nasm.nes")) .arg(dir.join("cl65.nes")) .output() - .with_context(|| "failed to run diff")?; - - // If 'diff' failed, show it but don't error out. - if !diff.status.success() { - println!( - "xa65 (error): 'nasm' and 'ca65' have a mismatch. Check the results at {}", - dir.display() - ); + { + Ok(diff) => { + // If 'diff' failed, show it but don't error out. + if !diff.status.success() { + println!( + "xa65 (error): 'nasm' and 'ca65' have a mismatch. Check the results at {}", + dir.display() + ); + } + } + Err(e) => { + die(e.to_string()); + return; + } } // And just copy one of the binaries to where it was originally requested. // Note that the binary is the one from 'cl65' just in case 'diff' failed // (we take 'cl65' as the source of truth). - std::fs::copy(dir.join("cl65.nes"), args.out) - .with_context(|| "could not copy the resulting binary")?; - - Ok(()) + if let Err(e) = std::fs::copy(dir.join("cl65.nes"), args.out) { + die(format!("could not copy the resulting binary: {}", e)); + } }