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
//! Derive macro for the `dterror::FromContext` trait.
//!
//! See the `dterror` crate for usage documentation.
use TokenStream;
use TokenStream as TokenStream2;
use format_ident;
use ;
/// Derive an implementation of `dterror::FromContext` for an error type.
///
/// For a struct, the macro generates:
///
/// - a `{Error}Ctx<'ctx>` struct holding every field not marked with `#[source]`, `#[from]`, or
/// `#[location]`, deriving `Clone`, `Debug`, and `PartialEq`;
/// - a `pub fn new(...)` constructor on `{Error}Ctx` taking one argument per context field, in
/// declaration order;
/// - an `impl ::dterror::FromContext for {Error}` whose `from_context` moves the context fields
/// into the error and assigns `location` and `source` to the marked fields, when present.
///
/// For an enum, the macro generates a `{Error}Ctx<'ctx>` **enum** instead: one same-named
/// variant per *constructible* variant of the error (a variant with at least one context field
/// AND a `#[source]`/`#[from]` field), plus one public associated constructor on `{Error}Ctx` per
/// Ctx variant, named after the variant in `snake_case`. Unit variants, marker-only
/// variants, and source-less variants get no Ctx variant and no constructor.
///
/// # Attribute helpers
///
/// - `#[source]` or `#[from]`: the field that stores the source of the error. At most one per
/// struct or variant.
/// - `#[location]`: the field that stores the caller's [`std::panic::Location`]. At most one
/// per struct or variant.
/// - `#[context(borrow = TargetType)]`: store a borrowed reference (`&'ctx TargetType`) in Ctx
/// instead of an owned value, deferring allocation to `from_context`. This enables zero-cost
/// context construction when the caller already holds a reference — no eager cloning occurs on
/// the Ok path. See `FromContext::Ctx` in the `dterror` crate for details. At most one
/// `#[context(...)]` attribute per field, and it cannot be combined with `#[source]`/`#[from]`
/// or `#[location]`.
/// - `#[context(constructor = "name")]`: variant-level; renames the generated constructor for
/// that variant to `name`, a quoted string that parses as an identifier. At most one per
/// variant.
///
/// Every other field becomes a field of the generated `{Error}Ctx`. A struct with no context
/// fields, or an enum with no constructible variant, fails to compile, as do duplicate or
/// conflicting markers and duplicate constructor names.