|
1 | | -//! Community-made procedural macros for |
2 | | -//! [`error-stack`](https://crates.io/crates/error-stack). |
| 1 | +//! [](https://crates.io/crates/error-stack-macros2) |
| 2 | +//! [](https://crates.io/crates/error-stack-macros2) |
| 3 | +//! [](https://github.com/LuisFerLCC/error-stack-macros2/actions/workflows/test.yml) |
| 4 | +//! [](https://github.com/LuisFerLCC/error-stack-macros2/blob/master/.github/CODE_OF_CONDUCT.md) |
3 | 5 | //! |
4 | | -//! **NOTE 1:** This crate is not affiliated with the official |
5 | | -//! [`error-stack`](https://crates.io/crates/error-stack) crate or its |
| 6 | +//! Community-made procedural macros for [`error-stack`]. |
| 7 | +//! |
| 8 | +//! # Example |
| 9 | +//! |
| 10 | +//! Here is the same example shown in the [`error-stack`] `README`, modified to |
| 11 | +//! use the macros provided by this crate: |
| 12 | +//! |
| 13 | +//! ```rust |
| 14 | +//! use error_stack::{Report, ResultExt}; |
| 15 | +//! use error_stack_macros2::Error; |
| 16 | +//! |
| 17 | +//! #[derive(Debug, Error)] |
| 18 | +//! #[display("invalid experiment description")] |
| 19 | +//! struct ParseExperimentError; |
| 20 | +//! |
| 21 | +//! fn parse_experiment( |
| 22 | +//! description: &str |
| 23 | +//! ) -> Result<(u64, u64), Report<ParseExperimentError>> { |
| 24 | +//! let value = description |
| 25 | +//! .parse::<u64>() |
| 26 | +//! .attach_with(|| { |
| 27 | +//! format!("{description:?} could not be parsed as experiment") |
| 28 | +//! }) |
| 29 | +//! .change_context(ParseExperimentError)?; |
| 30 | +//! |
| 31 | +//! Ok((value, 2 * value)) |
| 32 | +//! } |
| 33 | +//! |
| 34 | +//! #[derive(Debug, Error)] |
| 35 | +//! #[display("experiment error: could not run experiment")] |
| 36 | +//! struct ExperimentError; |
| 37 | +//! |
| 38 | +//! fn start_experiments( |
| 39 | +//! experiment_ids: &[usize], |
| 40 | +//! experiment_descriptions: &[&str], |
| 41 | +//! ) -> Result<Vec<u64>, Report<ExperimentError>> { |
| 42 | +//! let experiments = experiment_ids |
| 43 | +//! .iter() |
| 44 | +//! .map(|exp_id| { |
| 45 | +//! let description = experiment_descriptions |
| 46 | +//! .get(*exp_id) |
| 47 | +//! .ok_or_else(|| { |
| 48 | +//! Report::new(ExperimentError) |
| 49 | +//! .attach(format!( |
| 50 | +//! "experiment {exp_id} has no valid description") |
| 51 | +//! ) |
| 52 | +//! })?; |
| 53 | +//! |
| 54 | +//! let experiment = parse_experiment(description) |
| 55 | +//! .attach(format!("experiment {exp_id} could not be parsed")) |
| 56 | +//! .change_context(ExperimentError)?; |
| 57 | +//! |
| 58 | +//! Ok(move || experiment.0 * experiment.1) |
| 59 | +//! }) |
| 60 | +//! .collect::<Result<Vec<_>, Report<ExperimentError>>>() |
| 61 | +//! .attach("unable to set up experiments")?; |
| 62 | +//! |
| 63 | +//! Ok(experiments.iter().map(|experiment| experiment()).collect()) |
| 64 | +//! } |
| 65 | +//! |
| 66 | +//! let experiment_ids = &[0, 2]; |
| 67 | +//! let experiment_descriptions = &["10", "20", "3o"]; |
| 68 | +//! let err = start_experiments(experiment_ids, experiment_descriptions) |
| 69 | +//! .unwrap_err(); |
| 70 | +//! |
| 71 | +//! assert_eq!(err.to_string(), "experiment error: could not run experiment"); |
| 72 | +//! ``` |
| 73 | +//! |
| 74 | +//! # Support |
| 75 | +//! |
| 76 | +//! Need help using `error-stack-macros2`? Don't hesitate to reach out on |
| 77 | +//! [GitHub Discussions](https://github.com/LuisFerLCC/error-stack-macros2/discussions/categories/q-a)! |
| 78 | +//! |
| 79 | +//! # Links |
| 80 | +//! |
| 81 | +//! - [Documentation] |
| 82 | +//! - [GitHub](https://github.com/LuisFerLCC/error-stack-macros2) |
| 83 | +//! - [crates.io](https://crates.io/crates/error-stack-macros2) |
| 84 | +//! |
| 85 | +//! # Contributing |
| 86 | +//! |
| 87 | +//! Before creating an issue, please consider the following: |
| 88 | +//! |
| 89 | +//! - Refer to the [documentation] to |
| 90 | +//! make sure the error is actually a bug and not a mistake of your own. |
| 91 | +//! - Make sure the issue hasn't already been reported or suggested. |
| 92 | +//! - Please report any security vulnerabilities privately through |
| 93 | +//! [Security Advisories](https://github.com/LuisFerLCC/error-stack-macros2/security/advisories/new). |
| 94 | +//! - After following these steps, you can file an issue using one of our |
| 95 | +//! [templates](https://github.com/LuisFerLCC/error-stack-macros2/issues/new/choose). |
| 96 | +//! Please make sure to follow our |
| 97 | +//! [Code of Conduct](https://github.com/LuisFerLCC/error-stack-macros2/blob/master/.github/CODE_OF_CONDUCT.md). |
| 98 | +//! - If you wish to [submit a pull request](https://github.com/LuisFerLCC/error-stack-macros2/compare) |
| 99 | +//! alongside your issue, please follow our |
| 100 | +//! [contribution guidelines](https://github.com/LuisFerLCC/error-stack-macros2/blob/master/.github/CONTRIBUTING.md). |
| 101 | +//! |
| 102 | +//! # Disclaimer |
| 103 | +//! |
| 104 | +//! This crate is not affiliated with the official [`error-stack`] crate or its |
6 | 105 | //! maintainers. |
7 | 106 | //! |
8 | | -//! **NOTE 2:** This crate is currently empty and under development. This |
9 | | -//! version (`0.0.0-reserved`) only reserves the crate name on crates.io for |
10 | | -//! future use. |
| 107 | +//! [`error-stack`]: https://crates.io/crates/error-stack |
| 108 | +//! [documentation]: https://docs.rs/error-stack-macros2 |
11 | 109 |
|
12 | 110 | use proc_macro::TokenStream; |
13 | 111 | use quote::quote; |
|
0 commit comments