|
10 | 10 | /// ## Usage |
11 | 11 | /// |
12 | 12 | /// Simple getters without type transformation: |
| 13 | +/// |
13 | 14 | /// ```rust |
| 15 | +/// # use contextual_errors::provided_attachments; |
14 | 16 | /// #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] |
15 | 17 | /// enum Retryable { Yes, No } |
16 | 18 | /// |
17 | 19 | /// provided_attachments!( |
18 | 20 | /// retryable(single: Retryable) -> Option<&Retryable> { |v| v }; |
19 | 21 | /// ); |
20 | 22 | /// ``` |
| 23 | +/// |
21 | 24 | /// This will create a method `fn retryable(&self) -> Option<&Retryable>` on `CtxError`. |
22 | 25 | /// |
23 | 26 | /// You can also make use of the transformation expression that will be applied to the attachment |
24 | | -/// before returning it: ```rust |
| 27 | +/// before returning it: |
| 28 | +/// |
| 29 | +/// ```rust |
| 30 | +/// # use contextual_errors::provided_attachments; |
25 | 31 | /// #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] |
26 | 32 | /// enum Retryable { Yes, No } |
27 | 33 | /// |
28 | 34 | /// provided_attachments!( |
29 | 35 | /// retryable(single: Retryable) -> Retryable { |retry| retry.copied().unwrap_or(Retryable::No) }; |
30 | 36 | /// ); |
31 | 37 | /// ``` |
| 38 | +/// |
32 | 39 | /// This will create a method `fn retryable(&self) -> Retryable` on `CtxError`. The closure receives |
33 | 40 | /// the `Option<&Retryable>` and returns a `Retryable`. |
34 | 41 | /// |
35 | 42 | /// Finally, you can also retrieve multiple attachments of the same type and transform the iterator |
36 | | -/// into your return type: ```rust |
| 43 | +/// into your return type: |
| 44 | +/// |
| 45 | +/// ```rust |
| 46 | +/// # use contextual_errors::provided_attachments; |
37 | 47 | /// #[derive(Debug, PartialEq, Clone)] |
38 | 48 | /// struct UserInfo(String); |
39 | 49 | /// |
40 | 50 | /// provided_attachments!( |
41 | | -/// user_info(multiple: UserInfo) -> String { |iter| iter.map(|UserInfo(s)| s).collect() }; |
| 51 | +/// user_info(multiple: UserInfo) -> String { |iter| iter.map(|UserInfo(s)| s.as_str()).collect() }; |
42 | 52 | /// ); |
43 | | -/// This will create a method `fn user_info(&self) -> String` on `CtxError`, which collects all `UserInfo` attachments, unpacks them and collects them into a single `String`. |
44 | 53 | /// ``` |
| 54 | +/// |
| 55 | +/// This will create a method `fn user_info(&self) -> String` on `CtxError`, which collects all |
| 56 | +/// `UserInfo` attachments, unpacks them and collects them into a single `String`. |
45 | 57 | #[macro_export] |
46 | 58 | macro_rules! provided_attachments { |
47 | 59 | // Declare rule for single attachment. |
|
0 commit comments