Skip to content

Commit b0716c2

Browse files
authored
Merge pull request #9 from LuisFerLCC/impl_error_stack
`impl_error_stack` derive macro
2 parents 794a1fb + d811d28 commit b0716c2

18 files changed

Lines changed: 1510 additions & 63 deletions

File tree

.github/CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ If you wish to contribute to the `error-stack-macros2` codebase, feel free to fo
1010
1. Make sure the issue hasn't already been reported or suggested.
1111
1. Fork and clone the repository.
1212
1. Make your changes (add or modify tests and documentation comments as necessary to cover your changes).
13-
1. Run `cargo test` (or VSCode task _Cargo: Test_) to run the tests. You can also run `cargo build` to test the macro in a local Cargo project, or run `cargo doc` to build the documentation.
14-
1. Run `cargo clippy` (or VSCode task _Cargo Clippy: Lint_) and make sure there are no warnings or errors.
13+
1. Run `cargo test` (or VSCode task _Cargo: Test_) to run the tests. You can also run `cargo build` (_Cargo: Create development build_) to test the macro in a local Cargo project, or run `cargo doc` (_Cargo: Generate documentation_) to build the documentation.
14+
1. Run `cargo fmt` and `cargo clippy` (or VSCode tasks _RustFMT: Format_ and _Cargo Clippy: Lint_) and make sure there are no warnings or errors.
15+
1. Commit and push your changes.
1516
1. [Submit a pull request](https://github.com/LuisFerLCC/error-stack-macros2/compare).

.vscode/tasks.json

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111

1212
"type": "shell",
13-
"command": "cargo watch -c -x run",
13+
"command": "cargo watch -c -x build",
1414

1515
"isBackground": true,
1616
"presentation": {
@@ -84,6 +84,26 @@
8484
"problemMatcher": []
8585
},
8686

87+
{
88+
"label": "Cargo: Generate documentation",
89+
"icon": {
90+
"id": "book",
91+
"color": "terminal.ansiYellow"
92+
},
93+
94+
"type": "shell",
95+
"command": "cargo doc -p error-stack-macros2",
96+
97+
"isBackground": false,
98+
"presentation": {
99+
"echo": false,
100+
"panel": "dedicated",
101+
"showReuseMessage": false
102+
},
103+
104+
"problemMatcher": []
105+
},
106+
87107
{
88108
"label": "Cargo: Test",
89109
"icon": {
@@ -131,14 +151,34 @@
131151
},
132152

133153
{
134-
"label": "Cargo: Install crates",
154+
"label": "Cargo: Install crates (impl crate)",
155+
"icon": {
156+
"id": "package",
157+
"color": "terminal.ansiYellow"
158+
},
159+
160+
"type": "shell",
161+
"command": "cargo add -p error-stack-macros2 ${input:crates}",
162+
163+
"isBackground": false,
164+
"presentation": {
165+
"echo": false,
166+
"panel": "dedicated",
167+
"showReuseMessage": false
168+
},
169+
170+
"problemMatcher": []
171+
},
172+
173+
{
174+
"label": "Cargo: Install crates (tests crate)",
135175
"icon": {
136176
"id": "package",
137177
"color": "terminal.ansiYellow"
138178
},
139179

140180
"type": "shell",
141-
"command": "cargo add ${input:crates}",
181+
"command": "cargo add -p error-stack-macros2-tests ${input:crates}",
142182

143183
"isBackground": false,
144184
"presentation": {

Cargo.lock

Lines changed: 57 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
1-
[package]
2-
name = "error-stack-macros2"
3-
version = "0.0.0-reserved"
4-
authors = ["LuisFerLCC"]
5-
edition = "2024"
6-
rust-version = "1.89.0"
7-
description = "Community-made procedural macros for error-stack."
8-
readme = "README.md"
9-
repository = "https://github.com/LuisFerLCC/error-stack-macros2"
10-
license = "Apache-2.0"
11-
keywords = ["error", "errorstack", "macros", "proc-macro", "no_std"]
12-
categories = ["development-tools", "no-std", "rust-patterns"]
13-
exclude = ["/.github", "/.idea", "/.vscode", "/target", ".editorconfig"]
14-
15-
[lib]
16-
proc-macro = true
17-
18-
[lints.rust]
19-
missing_docs = "warn"
20-
single_use_lifetimes = "warn"
21-
unconditional_recursion = "deny"
22-
unit_bindings = "warn"
23-
unnameable_types = "warn"
24-
unreachable_pub = "warn"
25-
unsafe_code = "deny"
26-
unused_lifetimes = "warn"
27-
28-
[lints.clippy]
29-
unwrap_used = "warn"
30-
expect_used = "warn"
31-
panic = "warn"
32-
33-
[dependencies]
34-
proc-macro2 = "1.0.101"
35-
quote = "1.0.40"
36-
syn = "2.0.106"
37-
38-
[dev-dependencies]
39-
error-stack = "0.6.0"
1+
[workspace]
2+
members = ["impl", "tests"]
3+
resolver = "3"

README.md

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,110 @@
11
# `error-stack-macros2`
22

3-
Community-made procedural macros for [`error-stack`](https://crates.io/crates/error-stack).
3+
[![crates.io latest version](https://img.shields.io/crates/v/error-stack-macros2?label=version&logo=rust)](https://crates.io/crates/error-stack-macros2)
4+
[![crates.io downloads](https://img.shields.io/crates/d/error-stack-macros2)](https://crates.io/crates/error-stack-macros2)
5+
[![Tests status](https://img.shields.io/github/actions/workflow/status/LuisFerLCC/error-stack-macros2/test.yml?branch=master&label=tests)](https://github.com/LuisFerLCC/error-stack-macros2/actions/workflows/test.yml)
6+
[![Contributor Covenant Code of Conduct](https://img.shields.io/badge/Contributor%20Covenant-3.0-5e0d73?logo=contributorcovenant)](https://github.com/LuisFerLCC/error-stack-macros2/blob/master/.github/CODE_OF_CONDUCT.md)
47

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`].
79

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

impl/Cargo.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "error-stack-macros2"
3+
version = "0.0.0-reserved"
4+
authors = ["LuisFerLCC"]
5+
edition = "2024"
6+
rust-version = "1.90.0"
7+
description = "Community-made procedural macros for error-stack."
8+
readme = "../README.md"
9+
repository = "https://github.com/LuisFerLCC/error-stack-macros2"
10+
license = "Apache-2.0"
11+
keywords = ["error", "errorstack", "error-handling", "macros", "proc-macro"]
12+
categories = ["development-tools", "rust-patterns"]
13+
14+
[lib]
15+
proc-macro = true
16+
17+
[lints.rust]
18+
missing_docs = "warn"
19+
single_use_lifetimes = "warn"
20+
unconditional_recursion = "deny"
21+
unit_bindings = "warn"
22+
unnameable_types = "warn"
23+
unreachable_pub = "warn"
24+
unsafe_code = "deny"
25+
unused_lifetimes = "warn"
26+
27+
[lints.clippy]
28+
unwrap_used = "warn"
29+
expect_used = "warn"
30+
panic = "warn"
31+
32+
[dependencies]
33+
proc-macro2 = "1.0.101"
34+
quote = "1.0.40"
35+
regex = "1.11.2"
36+
syn = { version = "2.0.106", features = [] }
37+
38+
[dev-dependencies]
39+
error-stack = "0.6.0"

0 commit comments

Comments
 (0)