Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Use anyhow for idiomatic error handling
Browse files Browse the repository at this point in the history
Good for CLI applications, it provides some nicer ergonomics, features
to inject context if needed, as well as non-trivial optimizations
compared to using `Box<dyn Error>`.

For the xtasks use case, I'm going with the simpler `anyhow` over
something like `color-eyre`, which supports more complicated output
formatting (but has other trade-offs).

See:
- dtolnay/anyhow#96
  • Loading branch information
elasticdog committed May 13, 2023
1 parent 9b64f9d commit 5b1a185
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.71"
xshell = "0.2.2"
26 changes: 12 additions & 14 deletions xtask/src/fixup.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use std::{
fs,
path::{Path, PathBuf},
};

use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};
use xshell::{cmd, Shell};

use crate::{project_root, verbose_cd, DynError};
use crate::{project_root, verbose_cd};

pub fn format_cue() -> Result<(), DynError> {
pub fn format_cue() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, cue_dir());
cmd!(sh, "cue fmt --simplify").run()?;
Ok(())
}

pub fn format_markdown() -> Result<(), DynError> {
pub fn format_markdown() -> Result<()> {
let sh = Shell::new()?;
let root = project_root();
verbose_cd(&sh, &root);
Expand All @@ -32,21 +30,21 @@ pub fn format_markdown() -> Result<(), DynError> {
Ok(())
}

pub fn format_rust() -> Result<(), DynError> {
pub fn format_rust() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, project_root());
cmd!(sh, "cargo fmt").run()?;
Ok(())
}

pub fn lint_cue() -> Result<(), DynError> {
pub fn lint_cue() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, cue_dir());
cmd!(sh, "cue vet --concrete").run()?;
Ok(())
}

pub fn lint_rust() -> Result<(), DynError> {
pub fn lint_rust() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, project_root());
cmd!(
Expand All @@ -67,14 +65,14 @@ pub fn lint_rust() -> Result<(), DynError> {
Ok(())
}

pub fn regenerate_ci_yaml() -> Result<(), DynError> {
pub fn regenerate_ci_yaml() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, cue_dir());
cmd!(sh, "cue cmd regen-ci-yaml").run()?;
Ok(())
}

pub fn spellcheck() -> Result<(), DynError> {
pub fn spellcheck() -> Result<()> {
let sh = Shell::new()?;
verbose_cd(&sh, project_root());
cmd!(sh, "codespell --write-changes").run()?;
Expand All @@ -85,7 +83,7 @@ fn cue_dir() -> PathBuf {
project_root().join(".github/cue")
}

fn find_markdown_files<P: AsRef<Path>>(dir: P) -> Result<Vec<PathBuf>, DynError> {
fn find_markdown_files<P: AsRef<Path>>(dir: P) -> Result<Vec<PathBuf>> {
let mut result = Vec::new();
for entry in fs::read_dir(dir)? {
let path = entry?.path();
Expand Down
28 changes: 12 additions & 16 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::{
env,
error::Error,
path::{Path, PathBuf},
};
use anyhow::Result;
use std::env;
use std::path::{Path, PathBuf};
use xshell::Shell;

mod fixup;

type DynError = Box<dyn Error>;

fn main() -> Result<(), DynError> {
fn main() -> Result<()> {
let task = env::args().nth(1);
match task {
None => tasks::print_help()?,
Expand All @@ -20,7 +16,7 @@ fn main() -> Result<(), DynError> {
"fixup.markdown" => tasks::fixup_markdown()?,
"fixup.rust" => tasks::fixup_rust()?,
"fixup.spelling" => tasks::fixup_spelling()?,
invalid => return Err(format!("Invalid task name: {}", invalid).into()),
invalid => return Err(anyhow::anyhow!("Invalid task name: {}", invalid)),
},
};
Ok(())
Expand All @@ -30,7 +26,7 @@ pub mod tasks {
use crate::fixup::{format_cue, format_markdown, format_rust};
use crate::fixup::{lint_cue, lint_rust};
use crate::fixup::{regenerate_ci_yaml, spellcheck};
use crate::DynError;
use anyhow::Result;

const HELP: &str = "\
NAME
Expand All @@ -48,33 +44,33 @@ COMMANDS
fixup.rust Fix lints and format Rust files in-place.
";

pub fn fixup() -> Result<(), DynError> {
pub fn fixup() -> Result<()> {
fixup_spelling()?; // affects all file types; run this first
fixup_github_actions()?;
fixup_markdown()?;
fixup_rust()
}

pub fn fixup_github_actions() -> Result<(), DynError> {
pub fn fixup_github_actions() -> Result<()> {
lint_cue()?;
format_cue()?;
regenerate_ci_yaml()
}

pub fn fixup_markdown() -> Result<(), DynError> {
pub fn fixup_markdown() -> Result<()> {
format_markdown()
}

pub fn fixup_rust() -> Result<(), DynError> {
pub fn fixup_rust() -> Result<()> {
lint_rust()?;
format_rust()
}

pub fn fixup_spelling() -> Result<(), DynError> {
pub fn fixup_spelling() -> Result<()> {
spellcheck()
}

pub fn print_help() -> Result<(), DynError> {
pub fn print_help() -> Result<()> {
print!("{}", HELP);
Ok(())
}
Expand Down

0 comments on commit 5b1a185

Please sign in to comment.