Skip to content

Commit

Permalink
feat(cli): Output runtime errors pretty-printed for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Dec 5, 2024
1 parent 1618d3d commit fb45160
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/bin/sile.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
use sile::cli::Cli;

use snafu::prelude::*;

use clap::{CommandFactory, FromArgMatches};

use sile::cli::Cli;
use sile::Result;
#[derive(Snafu)]
enum Error {
#[snafu(display("{}", source))]
Args { source: clap::error::Error },

#[snafu(display("{}", source))]
Runtime { source: anyhow::Error },

#[snafu(display("{}", source))]
Version { source: anyhow::Error },
}

// Deeper error types are reported using the Debug trait, but we handle them via Snafu and the Display trait.
// So we delegate. c.f. https://github.com/shepmaster/snafu/issues/110
impl std::fmt::Debug for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self, fmt)
}
}

type Result<T, E = Error> = std::result::Result<T, E>;

fn main() -> Result<()> {
let version = option_env!("VERGEN_GIT_DESCRIBE").unwrap_or_else(|| env!("CARGO_PKG_VERSION"));
let version = version.replacen('-', ".r", 1);
let long_version = sile::version()?
let long_version = sile::version()
.context(VersionSnafu)?
.strip_prefix("SILE ")
.unwrap_or("")
.to_string();
let app = Cli::command().version(version).long_version(long_version);
let matches = app.get_matches();
let args = Cli::from_arg_matches(&matches).expect("Unable to parse arguments");
let args = Cli::from_arg_matches(&matches).context(ArgsSnafu)?;
sile::run(
args.input,
args.backend,
Expand All @@ -29,6 +53,7 @@ fn main() -> Result<()> {
args.r#use,
args.quiet,
args.traceback,
)?;
)
.context(RuntimeSnafu)?;
Ok(())
}

0 comments on commit fb45160

Please sign in to comment.