Skip to main content

always_context

Attribute Macro always_context 

Source
#[always_context]
Available on crate feature always-context only.
Expand description

Automatically adds .with_context(context!()) to all ? operators that don’t already have context.

Transforms operation()? into operation().with_context(context!("operation()"))? with function call details, arguments, and file location.

§Requirements

  • Function must return anyhow::Result<T> or Result<T, UserFriendlyError> (please add an issue if you need support for other types)

§Control Attributes

§Macro-level

  • Skip
    • easy_sql or es - Skips requiring context for query! macros
    • skip_macros or ! - Skips requiring context for macros entirely

Examples: #[always_context(skip(!))], #[always_context(skip(macros, easy_sql))], #[always_context(skip(!, es))]

§Function-level

  • #[no_context] - Disable context generation entirely
  • #[no_context_inputs] - Add context but exclude function arguments
  • #[enable_context] - Re-enable context (useful in macros where auto-disabled)

§Argument-level

  • #[context(display)] - Use Display instead of Debug for formatting
  • #[context(.method())] - Call method on argument before displaying
  • #[context(tokens)] - Format as token stream (equivalent to display + .to_token_stream())
  • #[context(tokens_vec)] - Format as token stream collection
  • #[context(ignore)] or #[context(ignored)] or #[context(no)] - Exclude this argument from context

§Limitations

These expressions before ? require manual .with_context() or .context(): blocks, control flow (if/match/while/for/loop), field access, macros.