-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add compatible crate exn-anyhow #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a7d125
feat: add compatible crate exn-anyhow
andylokandy f1cba7a
fix
andylokandy 770b347
fix
andylokandy e2fe6b2
fix
andylokandy 4c9a3e7
fix
andylokandy 19e551b
fix
andylokandy 3af8994
Merge remote-tracking branch 'upstream/main' into dev-11
andylokandy 737418d
fix
andylokandy f39036b
fix
andylokandy 1864e59
fix
andylokandy 2752cb7
fix
andylokandy 061ae5a
fix
andylokandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| // Copyright 2025 FastLabs Developers | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # Anyhow Interoperate Example - Converting `anyhow::Result<_>` into `exn::Result<_>` | ||
| //! | ||
| //! This example shows a common pattern: | ||
| //! - Legacy code returns `anyhow::Result<T>`. | ||
| //! - At the boundary, convert into `exn::Result<T, exn_anyhow::AnyhowError>`. | ||
|
|
||
| use derive_more::Display; | ||
| use exn::Result; | ||
| use exn::ResultExt; | ||
| use exn_anyhow::from_anyhow; | ||
|
|
||
| fn main() -> Result<(), MainError> { | ||
| app::run().or_raise(|| MainError)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| #[display("fatal error occurred in application")] | ||
| struct MainError; | ||
| impl std::error::Error for MainError {} | ||
|
|
||
| mod app { | ||
| use super::*; | ||
|
|
||
| pub fn run() -> Result<(), AppError> { | ||
| legacy::load_port() | ||
| .map_err(from_anyhow) | ||
| .or_raise(|| AppError)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| #[display("failed to run app")] | ||
| pub struct AppError; | ||
| impl std::error::Error for AppError {} | ||
| } | ||
|
|
||
| mod legacy { | ||
| use anyhow::Context; | ||
|
|
||
| pub fn load_port() -> anyhow::Result<u16> { | ||
| let raw = "not-a-number"; | ||
|
|
||
| let port = raw | ||
| .parse::<u16>() | ||
| .with_context(|| format!("PORT must be a number; got {raw:?}"))?; | ||
|
|
||
| Ok(port) | ||
| } | ||
| } | ||
|
|
||
| // Output when running `cargo run -p examples --example from-anyhow`: | ||
| // | ||
| // Error: fatal error occurred in application, at examples/src/from-anyhow.rs:27:16 | ||
| // | | ||
| // |-> failed to run app, at examples/src/from-anyhow.rs:42:14 | ||
| // | | ||
| // |-> PORT must be a number; got "not-a-number", at exn-anyhow/src/lib.rs:51:19 | ||
| // | | ||
| // |-> invalid digit found in string, at exn-anyhow/src/lib.rs:48:19 |
This file contains hidden or 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 hidden or 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,33 @@ | ||
| # Copyright 2025 FastLabs Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| [package] | ||
| name = "exn-anyhow" | ||
| version = "0.1.0" | ||
|
|
||
| description = "Interop helpers between exn and anyhow." | ||
|
|
||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| license.workspace = true | ||
| readme = "README.md" | ||
| repository.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| exn = { workspace = true } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or 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,58 @@ | ||
| # Interop helpers between `exn` and `anyhow` | ||
|
|
||
| [![Crates.io][crates-badge]][crates-url] | ||
| [![Documentation][docs-badge]][docs-url] | ||
| [![MSRV 1.85][msrv-badge]](https://www.whatrustisit.com) | ||
| [![Apache 2.0 licensed][license-badge]][license-url] | ||
| [![Build Status][actions-badge]][actions-url] | ||
|
|
||
| [crates-badge]: https://img.shields.io/crates/v/exn-anyhow.svg | ||
| [crates-url]: https://crates.io/crates/exn-anyhow | ||
| [docs-badge]: https://docs.rs/exn-anyhow/badge.svg | ||
| [docs-url]: https://docs.rs/exn-anyhow | ||
| [msrv-badge]: https://img.shields.io/badge/MSRV-1.85-green?logo=rust | ||
| [license-badge]: https://img.shields.io/crates/l/exn-anyhow | ||
| [license-url]: https://github.com/fast/exn/blob/main/LICENSE | ||
| [actions-badge]: https://github.com/fast/exn/workflows/CI/badge.svg | ||
| [actions-url]:https://github.com/fast/exn/actions?query=workflow%3ACI | ||
|
|
||
| ## Overview | ||
|
|
||
| `exn-anyhow` provides explicit boundary conversion helpers: | ||
|
|
||
| - `exn_anyhow::into_anyhow`: `exn::Exn<E>` -> `anyhow::Error` | ||
| - `exn_anyhow::from_anyhow`: `anyhow::Error` -> `exn::Exn<exn_anyhow::AnyhowError>` | ||
|
|
||
| Recommended usage is explicit error conversion at API boundaries via `map_err`: | ||
|
|
||
| ```rust | ||
| use exn_anyhow::from_anyhow; | ||
| use exn_anyhow::into_anyhow; | ||
|
|
||
| let exn_result = anyhow_result.map_err(from_anyhow); | ||
| let anyhow_result = exn_result.map_err(into_anyhow); | ||
| ``` | ||
|
|
||
| ## Documentation | ||
|
|
||
| Read the online documents at https://docs.rs/exn-anyhow. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```bash | ||
| cargo run -p examples --example from-anyhow | ||
| cargo run -p examples --example into-anyhow | ||
| ``` | ||
|
|
||
| ## Minimum Rust version policy | ||
|
|
||
| This crate is built against the latest stable release, and its minimum supported rustc version is 1.85.0. | ||
|
|
||
| The policy is that the minimum Rust version required to use this crate can be increased in minor | ||
| version updates. For example, if version 1.0 requires Rust 1.60.0, then version 1.0.z for all | ||
| values of z will also require Rust 1.60.0 or newer. However, version 1.y for y > 0 may require a | ||
| newer minimum version of Rust. | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0). |
This file contains hidden or 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,55 @@ | ||
| // Copyright 2025 FastLabs Developers | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Interop helpers between [`exn`] and [`anyhow`]. | ||
|
|
||
| use std::error::Error; | ||
| use std::fmt; | ||
|
|
||
| use exn::Exn; | ||
|
|
||
| /// Convert an [`Exn`] into [`anyhow::Error`]. | ||
| pub fn into_anyhow<E>(err: Exn<E>) -> anyhow::Error | ||
| where | ||
| E: Error + Send + Sync + 'static, | ||
| { | ||
| anyhow::Error::from_boxed(err.into()) | ||
| } | ||
|
|
||
| /// A plain message error frame used while converting from [`anyhow::Error`]. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct AnyhowError(String); | ||
|
|
||
| impl fmt::Display for AnyhowError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl Error for AnyhowError {} | ||
|
|
||
| /// Convert an [`anyhow::Error`] into [`Exn`], preserving `anyhow`'s cause chain. | ||
| pub fn from_anyhow(err: anyhow::Error) -> Exn<AnyhowError> { | ||
| let mut chain = err.chain().map(ToString::to_string).collect::<Vec<_>>(); | ||
| let leaf = chain | ||
| .pop() | ||
| .expect("anyhow::Error must have at least one error in the chain"); | ||
| let mut exn = Exn::new(AnyhowError(leaf)); | ||
|
|
||
| while let Some(message) = chain.pop() { | ||
| exn = exn.raise(AnyhowError(message)); | ||
| } | ||
|
|
||
| exn | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.