Skip to content

Commit

Permalink
Get rid of anyhow and use structured errors with context
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamVenner committed Sep 6, 2023
1 parent 8e092cd commit c790ff2
Show file tree
Hide file tree
Showing 9 changed files with 638 additions and 241 deletions.
56 changes: 1 addition & 55 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = ["fastgmad-lib", "fastgmad-publish", "fastgmad-bin"]
[profile.release]
lto = "thin"
codegen-units = 1
debug = "line-tables-only"
panic = "abort"
strip = true
62 changes: 31 additions & 31 deletions fastgmad-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fastgmad::{
bin_prelude::*,
create::{CreateGmaConfig, CreateGmadOut},
error::{FastGmadError, FastGmadErrorKind},
extract::{ExtractGmaConfig, ExtractGmadIn},
workshop::{WorkshopPublishConfig, WorkshopUpdateConfig},
};
Expand Down Expand Up @@ -54,21 +55,10 @@ fn main() {
match bin() {
Ok(()) => {}

Err(FastGmadBinError::Error(err)) => {
eprintln!();
log::error!("{err:#?}\n");
Err::<(), _>(err).unwrap();
}

Err(FastGmadBinError::PrintHelp(msg)) => {
if let Some(msg) = msg {
log::error!("{msg}\n");
}

eprintln!("{}", include_str!("usage.txt"));
}

Err(FastGmadBinError::Libloading(err)) => {
Err(FastGmadBinError::FastGmadError(FastGmadError {
kind: FastGmadErrorKind::Libloading(err),
..
})) => {
eprintln!();
log::error!("Error loading shared libraries for Workshop publishing: {err}");
if cfg!(target_os = "windows") {
Expand All @@ -90,6 +80,20 @@ fn main() {
eprintln!();
Err(err).unwrap()
}

Err(FastGmadBinError::FastGmadError(err)) => {
eprintln!();
log::error!("{err}\n");
Err::<(), _>(err).unwrap();
}

Err(FastGmadBinError::PrintHelp(msg)) => {
if let Some(msg) = msg {
log::error!("{msg}\n");
}

eprintln!("{}", include_str!("usage.txt"));
}
}
}

Expand Down Expand Up @@ -152,7 +156,10 @@ fn create(conf: CreateGmaConfig, out: CreateGmadOut, exit: &mut impl FnMut()) ->
match out {
CreateGmadOut::File(path) => {
log::info!("Opening output file...");
let mut w = BufWriter::new(File::create(path)?);
let mut w = BufWriter::new(File::create(&path).map_err(|error| FastGmadError {
kind: FastGmadErrorKind::PathIoError { path, error },
context: Some("opening output file".to_string()),
})?);
fastgmad::create::seekable_create_gma_with_done_callback(&conf, &mut w, exit)?;
}

Expand All @@ -172,7 +179,10 @@ fn extract(conf: ExtractGmaConfig, r#in: ExtractGmadIn, exit: &mut impl FnMut())
match r#in {
ExtractGmadIn::File(path) => {
log::info!("Opening input file...");
let mut r = BufReader::new(File::open(path)?);
let mut r = BufReader::new(File::open(&path).map_err(|error| FastGmadError {
kind: FastGmadErrorKind::PathIoError { path, error },
context: Some("opening input file".to_string()),
})?);
fastgmad::extract::extract_gma_with_done_callback(&conf, &mut r, exit)?;
}

Expand Down Expand Up @@ -203,22 +213,12 @@ fn update(conf: WorkshopUpdateConfig) -> Result<(), FastGmadBinError> {
}

enum FastGmadBinError {
FastGmadError(FastGmadError),
PrintHelp(Option<&'static str>),
Error(anyhow::Error),
Libloading(libloading::Error),
}
impl From<anyhow::Error> for FastGmadBinError {
fn from(e: anyhow::Error) -> Self {
match e.downcast::<libloading::Error>() {
Ok(e) => Self::Libloading(e),
Err(e) => Self::Error(e),
}
}
}
impl From<std::io::Error> for FastGmadBinError {
#[track_caller]
fn from(e: std::io::Error) -> Self {
Self::Error(e.into())
impl From<FastGmadError> for FastGmadBinError {
fn from(e: FastGmadError) -> Self {
Self::FastGmadError(e)
}
}
impl From<PrintHelp> for FastGmadBinError {
Expand Down
2 changes: 1 addition & 1 deletion fastgmad-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ workshop = ["dep:steamworks"]
walkdir = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = { version = "1", features = ["backtrace"] }
memchr = "2"
byteorder = "1"
uuid = { version = "1", features = ["v4"] }
log = "0.4"
ctrlc = { version = "3", optional = true }
steamworks = { version = "0.9", optional = true }
libloading = { version = "0.8", optional = true }
thiserror = "1"

[dev-dependencies]
lazy_static = "1"
Expand Down
Loading

0 comments on commit c790ff2

Please sign in to comment.