From e1a0584936b3aa5ce971e875dec750d2ae937d2e Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 31 Mar 2021 12:51:11 -0600 Subject: massively upgrade error handling --- src/main.rs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 1c2faf5..d560048 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ #![warn( - missing_docs, unreachable_pub, unsafe_code, unused_crate_dependencies, @@ -10,17 +9,20 @@ )] #![allow(clippy::redundant_pub_crate, clippy::non_ascii_literal)] +use std::env::current_dir; use std::fs::metadata; use std::io::stdin; use std::path::PathBuf; +use anyhow::bail; + mod args; mod makefile; use args::Args; use makefile::Makefile; -fn main() { +fn main() -> anyhow::Result<()> { let mut args = Args::from_env_and_args(); // If no makefile is specified, try some options. if args.makefile.is_empty() { @@ -29,8 +31,17 @@ fn main() { } else if metadata("./Makefile").is_ok() { "./Makefile".into() } else { - // TODO handle error gracefully - panic!("no makefile found"); + bail!( + "no makefile specified and neither {} nor {} was found.", + current_dir().map_or_else( + |_| "./makefile".to_string(), + |path| path.join("makefile").display().to_string() + ), + current_dir().map_or_else( + |_| "./Makefile".to_string(), + |path| path.join("Makefile").display().to_string() + ), + ); }]; } // Read in the makefile(s) specified. @@ -40,9 +51,9 @@ fn main() { let mut makefile = Makefile::new(&args); for filename in &args.makefile { if filename == &PathBuf::from("-") { - makefile.and_read(stdin().lock()); + makefile.and_read(stdin().lock())?; } else { - makefile.and_read_file(filename); + makefile.and_read_file(filename)?; }; } @@ -51,15 +62,18 @@ fn main() { } let targets = if args.targets().count() == 0 { - vec![makefile - .first_non_special_target - .clone() - .expect("couldn't find a target!")] + let first_target = makefile.first_non_special_target.clone(); + match first_target { + Some(x) => vec![x], + None => bail!("no targets given on command line or found in makefile."), + } } else { args.targets().cloned().collect() }; for target in targets { - makefile.update_target(&target); + makefile.update_target(&target)?; } + + Ok(()) } -- cgit v1.2.3