-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate optional anyhow support via feature flag
- Introduce `anyhow` as an optional dependency in `wherr/Cargo.toml`. - Implement conditional compilation in `wherr/src/lib.rs` to determine error type based on the presence of `anyhow` feature flag. - Add `wherr/examples/anyhow.rs` to demonstrate usage with anyhow. This allows users to leverage the benefits of the anyhow crate for error handling in projects that already utilize it while maintaining backward compatibility for projects that don't.
- Loading branch information
Showing
9 changed files
with
170 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "wherr-macro" | ||
version = "0.1.6" | ||
version = "0.1.7" | ||
edition = "2021" | ||
authors = ["Joel Jakobsson <[email protected]>"] | ||
categories = ["rust-patterns"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "wherr" | ||
version = "0.1.6" | ||
version = "0.1.7" | ||
edition = "2021" | ||
authors = ["Joel Jakobsson <[email protected]>"] | ||
categories = ["rust-patterns"] | ||
|
@@ -9,5 +9,18 @@ keywords = ["error", "error-handling"] | |
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/joelonsql/wherr" | ||
|
||
[features] | ||
anyhow = ["dep:anyhow"] | ||
|
||
[dependencies] | ||
wherr-macro = "0.1" | ||
anyhow = { version = "1.0", optional = true } | ||
|
||
[[example]] | ||
name = "anyhow" | ||
required-features = ["anyhow"] | ||
|
||
[[test]] | ||
name = "anyhow_error_tests" | ||
path = "tests/anyhow_error_tests.rs" | ||
required-features = ["anyhow"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use anyhow::{Context, Result}; | ||
use wherr::wherr; | ||
|
||
#[wherr] | ||
fn concat_files(path1: &str, path2: &str) -> Result<String> { | ||
let mut content1 = std::fs::read_to_string(path1).with_context(|| format!("Failed to read {}", path1))?; | ||
let content2 = std::fs::read_to_string(path2).with_context(|| format!("Failed to read {}", path2))?; | ||
|
||
content1.push_str(&content2); | ||
Ok(content1) | ||
} | ||
|
||
fn main() { | ||
let content = concat_files("file1.txt", "file2.txt").expect("Failed to concatenate the files"); | ||
println!("Concatenated content:\n{content}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use wherr::{wherr, wherrapper, Wherr}; | ||
use anyhow::{Error, Result}; | ||
|
||
#[test] | ||
fn test_wherr_new() { | ||
let error_message = "Test error"; | ||
let err = anyhow::Error::new(std::io::Error::new(std::io::ErrorKind::Other, error_message)); | ||
let wherr = Wherr::new(err, "test.rs", 42); | ||
|
||
assert_eq!(wherr.file, "test.rs"); | ||
assert_eq!(wherr.line, 42); | ||
assert_eq!(wherr.inner.to_string(), error_message); | ||
} | ||
|
||
#[test] | ||
fn test_wherr_display() { | ||
let error_message = "Test error"; | ||
let err = anyhow::Error::new(std::io::Error::new(std::io::ErrorKind::Other, error_message)); | ||
let wherr = Wherr::new(err, "test.rs", 42); | ||
|
||
assert_eq!( | ||
format!("{}", wherr), | ||
format!("{}\nerror at test.rs:42", error_message) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_wherrapper() { | ||
let error_message = "Test error"; | ||
let err = std::io::Error::new(std::io::ErrorKind::Other, error_message); | ||
let result: Result<(), _> = Err(err.into()); // Convert the error to anyhow::Error | ||
|
||
match wherrapper::<(), anyhow::Error>(result, "test.rs", 42) { | ||
Ok(_) => panic!("Expected an error"), | ||
Err(err) => { | ||
let wherr = err.downcast::<Wherr>().expect("Expected a Wherr error"); | ||
assert_eq!(wherr.file, "test.rs"); | ||
assert_eq!(wherr.line, 42); | ||
assert_eq!(wherr.inner.to_string(), error_message); | ||
} | ||
} | ||
} | ||
|
||
#[wherr] | ||
fn f3() -> Result<()> { | ||
i64::from_str_radix("not a decimal number", 10).map_err(Error::new)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[wherr] | ||
fn f2() -> Result<()> { | ||
f3()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[wherr] | ||
fn f1() -> Result<()> { | ||
f2()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_wherr_macro() { | ||
match f1() { | ||
Ok(_) => panic!("Expected an error"), | ||
Err(err) => { | ||
let wherr = err.downcast::<Wherr>().expect("Expected a Wherr error"); | ||
assert_eq!(wherr.file, "wherr/tests/anyhow_error_tests.rs"); | ||
assert_eq!(wherr.line, 46); | ||
assert_eq!(wherr.inner.to_string(), "invalid digit found in string"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters