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