#[derive(CtxError)]
{
// Attributes available to this derive:
#[context]
#[location]
#[source]
#[from]
}
Expand description
Derives an implementation of dterror::CtxError for an error type.
For a struct, the macro generates:
- a
{Error}Ctx<'ctx>struct that holds every field not marked with#[source],#[from], or#[location]. By default, the generated type derives nothing. The container-level#[context(derive(...))]attribute selects which traits the generated type derives. - a
pub fn new(...)constructor on{Error}Ctx. It takes one argument per context field, in the declaration order. - an
impl ::dterror::CtxError for {Error}. The generatedfrom_contextmoves the context fields into the error and assignslocationandsourceto the marked fields when the struct has those fields. A#[source]field of typeOption<T>receives the source wrapped inSome(source).
For an enum, the macro generates a {Error}Ctx<'ctx> enum instead. It creates one same-named
variant per constructible variant of the error (a variant with a #[source] or #[from]
field). It also creates one public associated constructor on {Error}Ctx per Ctx variant. The
macro gives each constructor the snake_case form of the variant name. A variant without a #[source]
or #[from] field gets no Ctx variant and no constructor.
§Attribute helpers
#[context(derive(Trait, ...))]: container-level (on the struct or enum that you apply the derive to). It selects which traits the generated{Error}Ctxderives. You can add trait paths that#[derive(...)]itself accepts. The list replaces the default, so if you omit the attribute, the derive adds no traits. This default means context field types do not need to implement traits that call sites never use.#[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’sstd::panic::Location. At most one per struct or variant.#[context(option)]: source-field-only. It wraps the captured source in::std::option::Option::Some(source)when it builds the error. This is valid only on the#[source]/#[from]field, which must have the typeOption<T>. The macro applies the wrapper automatically when the source field’s type is alreadyOption<T>. This helper only records that theOption<T>type is intentional.#[context(borrow = TargetType)]: stores a borrowed reference (&'ctx TargetType) in Ctx instead of an owned value. The macro allocates only insidefrom_context. When the caller already holds a reference, the macro builds the context at no cost. Details are inCtxError::Ctxin thedterrorcrate.#[context(from = TargetType)]: stores an owned value of typeTargetTypethat is cheap to construct, instead of the field’s own type. The constructor takesimpl Into<TargetType>, andfrom_contextconverts the stored value to the field’s owned type with.into(). IfTargetTypeincludes lifetime parameters (for exampleKindCtx<'a>), the macro binds those lifetimes to the'ctxlifetime parameter that{Error}Ctxcarries.#[context(constructor = "name")]: variant-level. It renames the generated constructor for that variant toname, a quoted string that parses as an identifier. At most one per variant.
A field may carry at most one #[context(...)] attribute, with at most one helper inside it. The
helpers borrow and from are mutually exclusive. The borrow and from helpers are also mutually
exclusive with a location marker. On a #[source]/#[from] field, the only accepted helper is
option. Every other field becomes part of the generated {Error}Ctx.