Skip to main content

Sensitive

Derive Macro Sensitive 

Source
#[derive(Sensitive)]
{
    // Attributes available to this derive:
    #[sensitive]
    #[not_sensitive]
    #[redactable]
}
Expand description

Derives redactable::RedactableWithMapper (and related impls) for structs and enums.

Sensitive and SensitiveDisplay are standalone derives. Use SensitiveDual when a type needs both structural and display redaction.

§Recursive Fields

Use #[redactable(recursive)] on a field whose crate-qualified, aliased, or mutually recursive type would otherwise create a self-referential inferred bound. Unannotated fields retain their exact complete-type bounds.

§Field Attributes

  • No annotation: The field requires declared Redactable behavior and is traversed with the supplied mapper. Raw leaves need a policy or an explicit public declaration.

  • #[sensitive(Secret)]: For scalar types (i32, bool, char, etc.), redacts to default values (0, false, ‘*’). For string-like types, applies full redaction to "[REDACTED]".

  • #[sensitive(Policy)]: Applies the policy’s redaction rules to string-like values. Works for String, Option<String>, Vec<String>, Box<String>. Scalars can only use #[sensitive(Secret)].

  • #[not_sensitive]: Explicit passthrough - the field is not transformed at all. Use this for foreign types that don’t implement RedactableWithMapper. This is the right declaration for a foreign field in a struct you own; BypassRedaction<T> is only for satisfying a Redactable bound on a value you cannot annotate.

Field operations are checked against the original declaration bounds, even when no generated method is called. For an unannotated generic field T, declare T: Redactable; complete container bounds may be declared instead. Policy fields require the corresponding complete-type PolicyField<P> bound. The recursion override omits inferred recursive predicates, but still checks the actual field operations.

Unions are rejected at compile time.

§Generated Impls

  • ToRedacted: always generated. It clones, redacts and serializes, producing a RedactedValue that carries redacted JSON. This is why Sensitive requires Clone and serde::Serialize on the type; a missing bound is reported on the generated impl.
  • RedactableWithMapper: always generated.
  • Redactable: always generated. Provides .redact() and allows the type inside Sensitive containers.
  • Debug: uses the production redacted representation in every build mode.
  • slog::Value + SlogRedacted (requires slog feature): borrowed generated output is a fixed fail-closed placeholder and never clones or serializes the raw reference. Owned values can use SlogRedactedExt::slog_redacted_json for redact-then-serialize structured output.
  • TracingRedacted (requires tracing feature): marker trait.