Skip to content

Commit fb45160

Browse files
committed
feat(cli): Output runtime errors pretty-printed for readability
1 parent 1618d3d commit fb45160

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/bin/sile.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
1+
use sile::cli::Cli;
2+
3+
use snafu::prelude::*;
4+
15
use clap::{CommandFactory, FromArgMatches};
26

3-
use sile::cli::Cli;
4-
use sile::Result;
7+
#[derive(Snafu)]
8+
enum Error {
9+
#[snafu(display("{}", source))]
10+
Args { source: clap::error::Error },
11+
12+
#[snafu(display("{}", source))]
13+
Runtime { source: anyhow::Error },
14+
15+
#[snafu(display("{}", source))]
16+
Version { source: anyhow::Error },
17+
}
18+
19+
// Deeper error types are reported using the Debug trait, but we handle them via Snafu and the Display trait.
20+
// So we delegate. c.f. https://github.com/shepmaster/snafu/issues/110
21+
impl std::fmt::Debug for Error {
22+
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
23+
std::fmt::Display::fmt(self, fmt)
24+
}
25+
}
26+
27+
type Result<T, E = Error> = std::result::Result<T, E>;
528

629
fn main() -> Result<()> {
730
let version = option_env!("VERGEN_GIT_DESCRIBE").unwrap_or_else(|| env!("CARGO_PKG_VERSION"));
831
let version = version.replacen('-', ".r", 1);
9-
let long_version = sile::version()?
32+
let long_version = sile::version()
33+
.context(VersionSnafu)?
1034
.strip_prefix("SILE ")
1135
.unwrap_or("")
1236
.to_string();
1337
let app = Cli::command().version(version).long_version(long_version);
1438
let matches = app.get_matches();
15-
let args = Cli::from_arg_matches(&matches).expect("Unable to parse arguments");
39+
let args = Cli::from_arg_matches(&matches).context(ArgsSnafu)?;
1640
sile::run(
1741
args.input,
1842
args.backend,
@@ -29,6 +53,7 @@ fn main() -> Result<()> {
2953
args.r#use,
3054
args.quiet,
3155
args.traceback,
32-
)?;
56+
)
57+
.context(RuntimeSnafu)?;
3358
Ok(())
3459
}

0 commit comments

Comments
 (0)