diff --git a/src/lib.rs b/src/lib.rs index 4b34a4e..8ef97cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ #![forbid(unsafe_code)] use clap::ArgMatches; -use color_eyre::eyre::Result; +use color_eyre::eyre::{bail, eyre, Result, WrapErr}; pub mod cmd_line; mod logging; @@ -114,8 +114,11 @@ pub fn run(options: &Options, cmdline_args: &ArgMatches) -> Result<()> { .map(|module| module.include_statement) .collect(); - // The include_statements should never be empty thanks to the required group in clap - assert!(!include_statements.is_empty()); + // The include_statements should never be empty thanks to the required group in clap. + // Make sure once more, though. + if include_statements.is_empty() { + bail!("The populated assembly includes no other files."); + } // Generate the populated assembly module let populated: Module = Input::new(ContentType::Assembly, title, options) @@ -128,7 +131,8 @@ pub fn run(options: &Options, cmdline_args: &ArgMatches) -> Result<()> { // Validate all file names specified on the command line if let Some(files_iterator) = cmdline_args.values_of("validate") { for file in files_iterator { - validation::validate(file)?; + validation::validate(file) + .wrap_err_with(|| eyre!("Failed to validate file {:?}", file))?; } } diff --git a/src/validation.rs b/src/validation.rs index dc5cfe9..b2f8d37 100644 --- a/src/validation.rs +++ b/src/validation.rs @@ -291,8 +291,12 @@ mod title { let attribute_regex = Regex::new(r"\{((?:[[:alnum:]]|[-_])+)\}").expect(REGEX_ERROR); let attribute = attribute_regex.captures(mod_id)?; + let attribute_name = attribute + .get(1) + .expect("Failed to extract an attribute name. Please report this as a bug") + .as_str(); - if attribute.get(1).unwrap().as_str() == "context" { + if attribute_name == "context" { // The context attribute is allowed None } else { diff --git a/src/write.rs b/src/write.rs index b17fffd..83d2947 100644 --- a/src/write.rs +++ b/src/write.rs @@ -2,7 +2,7 @@ use std::fs; use std::io; use std::path::PathBuf; -use color_eyre::eyre::{Context, Result}; +use color_eyre::eyre::{eyre, Result, WrapErr}; use crate::module::Module; use crate::Options; @@ -26,7 +26,7 @@ impl Module { io::stdin() .read_line(&mut answer) - .context("Failed to read your response")?; + .wrap_err_with(|| eyre!("Failed to read your response: {:?}", answer))?; match answer.trim().to_lowercase().as_str() { "y" | "yes" => { @@ -42,10 +42,8 @@ impl Module { } // If the target file doesn't exist, try to write to it - fs::write(full_path, &self.text).context(format!( - "Failed to write the `{}` file.", - &full_path.display() - ))?; + fs::write(full_path, &self.text) + .wrap_err_with(|| eyre!("Failed to write the `{}` file.", &full_path.display()))?; // If the write succeeds, print the include statement log::debug!("Successfully written file `{}`", &full_path.display());