Skip to content

Commit

Permalink
Use anyhow to provide better error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Feb 18, 2024
1 parent 71013af commit 3fd294a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ clap_complete = "4.5.0"
waj = { path = "libwaj", package = "libwaj", version = "0.2.0" }
indicatif = "0.17.3"
env_logger = "0.10.0"
anyhow = "1.0.75"
log = "0.4.20"
jbk.workspace = true
5 changes: 3 additions & 2 deletions src/create.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use std::cell::Cell;
use std::fs::File;
use std::io::{BufRead, BufReader};
Expand Down Expand Up @@ -108,7 +109,7 @@ impl CachedSize {
}
}

pub fn create(options: Options) -> jbk::Result<()> {
pub fn create(options: Options) -> Result<()> {
if options.verbose > 0 {
println!("Creating archive {:?}", options.outfile);
println!("With files {:?}", options.infiles);
Expand Down Expand Up @@ -152,5 +153,5 @@ pub fn create(options: Options) -> jbk::Result<()> {
}

let ret = creator.finalize(&out_file);
ret
Ok(ret?)
}
8 changes: 5 additions & 3 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{Context, Result};
use clap::Parser;
use std::path::PathBuf;

Expand Down Expand Up @@ -32,8 +33,9 @@ impl waj::walk::Operator<(), waj::FullBuilder> for Lister {
}
}

pub fn list(options: Options) -> jbk::Result<()> {
let waj = waj::Waj::new(options.infile)?;
pub fn list(options: Options) -> Result<()> {
let waj =
waj::Waj::new(&options.infile).with_context(|| format!("Opening {:?}", options.infile))?;
let mut walker = waj::walk::Walker::new(&waj, ());
walker.run(&Lister)
Ok(walker.run(&Lister)?)
}
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod create;
mod list;

use anyhow::{Context, Result};
use clap::{CommandFactory, Parser, Subcommand};
use log::error;
use std::path::PathBuf;
use std::process::ExitCode;

#[derive(Parser)]
#[clap(name = "waj", author, version, about, long_about=None)]
Expand Down Expand Up @@ -75,7 +78,7 @@ fn configure_log(verbose: u8) {
.init();
}

fn main() -> jbk::Result<()> {
fn run() -> Result<()> {
let args = Cli::parse();
configure_log(args.verbose);

Expand Down Expand Up @@ -110,10 +113,21 @@ fn main() -> jbk::Result<()> {
options.infile, options.address,
);
}
let server = waj::Server::new(&options.infile)?;
server.serve(&options.address)
let server = waj::Server::new(&options.infile)
.with_context(|| format!("Opening {:?}", options.infile))?;
Ok(server.serve(&options.address)?)
}
Commands::List(options) => list::list(options),
},
}
}

fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
error!("Error : {e:#}");
ExitCode::FAILURE
}
}
}

0 comments on commit 3fd294a

Please sign in to comment.