1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! # `traceback-derive`
//!
//! `traceback-derive` is a procedural macro crate designed to enhance the functionality of the
//! `traceback-error` crate by providing custom macros for streamlined error handling and tracebacks in Rust.
//!
//! ## Usage
//!
//! To use `traceback-derive` in your Rust project, follow these steps:
//!
//! 1. Add `traceback-derive` and `traceback-error` as dependencies in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! traceback-derive = "0.1.1"
//! traceback-error = "0.1.5"
//! ```
//!
//! The `#[traceback]` attribute enhances the function with traceback capabilities, making it easier to handle errors
//! and capture detailed trace information.
//!
//! 2. Apply the `traceback` macro to your function to create and handle errors with tracebacks:
//!
//! ```rust
//! #[traceback_derive::traceback]
//! fn my_function() -> Result<(), traceback_error::TracebackError> {
//! // Your code here
//! risky_function()?;
//! // ...
//! }
//! ```
//!
//! The `traceback!` macro simplifies error creation and captures relevant context information.
//!
//! ## Examples
//!
//! Here's an example of how `traceback-derive` simplifies error handling compared to using `traceback-error` directly:
//!
//! **Without `traceback-derive` (using `traceback-error` directly):**
//!
//! ```rust
//! use traceback_error::{traceback, TracebackError};
//!
//! fn main() {
//! match caller_of_tasks() {
//! Ok(_) => {}
//! Err(e) => {
//! traceback!(e, "One of the tasks failed");
//! }
//! }
//! }
//!
//! fn task_that_may_fail() -> Result<(), TracebackError> {
//! return Err(traceback!("task_that_may_fail failed"));
//! }
//!
//! fn other_task_that_may_fail() -> Result<(), TracebackError> {
//! return Err(traceback!("other_task_that_may_fail failed"));
//! }
//!
//! fn caller_of_tasks() -> Result<(), TracebackError> {
//! match task_that_may_fail() {
//! Ok(_) => {}
//! Err(e) => {
//! return Err(traceback!(err e));
//! }
//! };
//! match other_task_that_may_fail() {
//! Ok(_) => {}
//! Err(e) => {
//! return Err(traceback!(err e));
//! }
//! };
//! Ok(())
//! }
//! ```
//!
//! **With `traceback-derive`:**
//!
//! ```rust
//! use traceback_error::{traceback, TracebackError};
//!
//! fn main() {
//! match caller_of_tasks() {
//! Ok(_) => {}
//! Err(e) => {
//! traceback!(e, "One of the tasks failed");
//! }
//! }
//! }
//!
//! fn task_that_may_fail() -> Result<(), TracebackError> {
//! return Err(traceback!("task_that_may_fail failed"));
//! }
//!
//! fn other_task_that_may_fail() -> Result<(), TracebackError> {
//! return Err(traceback!("other_task_that_may_fail failed"));
//! }
//!
//! #[traceback_derive::traceback]
//! fn caller_of_tasks() -> Result<(), TracebackError> {
//! task_that_may_fail()?;
//! other_task_that_may_fail()?;
//! Ok(())
//! }
//! ```
//!
//! The two code snippets are equivalent when expanded, but `traceback-derive` simplifies error handling and capture.
//!
//! ## Contribution
//!
//! Contributions are welcome! Feel free to open issues or pull requests on the GitHub repository.
//! This project is still in very early development, and proper contribution guidelines have not yet been established.
//!
//! ## License
//!
//! This crate is dual-licensed under the [MIT License](LICENSE-MIT) and the [Apache License, Version 2.0](LICENSE-APACHE-2.0).
//! You may choose either of these licenses when using this crate.
//! See the [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE-2.0](LICENSE-APACHE-2.0) files for the full text of the licenses.
//!
//! ## GitHub Repository
//!
//! For more information and to contribute to the development of `traceback-derive`, visit the
//! [GitHub repository](https://github.com/Tommy-ASD/traceback-derive).
//!
use TokenStream;
use ;
use Spanned;
use ExprIndex;
use ;
;