redactkit 0.2.1

Prevent accidental leakage of secrets in Debug, tracing, and serialized output
Documentation
//! # redactkit
//!
//! Small toolkit for redacting sensitive data in debug output and tracing logs.
//!
//! > **Warning:** `redactkit` redacts formatted output only.
//! > It does not modify or erase original field values in memory.
//!
//! ## Features
//!
//! | Feature | Default | Description |
//! | --- | --- | --- |
//! | `std` | yes | Standard library support. |
//! | `derive` | yes | Enables the `RedactDebug` derive macro. |
//! | `regex` | no | Enables regex-based field and value rules. |
//! | `tracing` | no | Enables `tracing-subscriber` field redaction. |
//!
//! ## Derive example
//!
//! ```
//! # #[cfg(feature = "derive")]
//! # {
//! use redactkit::RedactDebug;
//!
//! #[derive(RedactDebug)]
//! struct User {
//!     username: String,
//!     #[redact]
//!     password: String,
//! }
//!
//! let user = User {
//!     username: "anna".to_string(),
//!     password: "s3cr3t".to_string(),
//! };
//!
//! assert_eq!(
//!     format!("{user:?}"),
//!     "User { username: \"anna\", password: \"******\" }"
//! );
//! # }
//! ```
//!
//! ## Builder example
//!
//! ```
//! use redactkit::Redactor;
//!
//! let redactor = Redactor::builder()
//!     .field("password")
//!     .field("token")
//!     .mask("[hidden]")
//!     .build();
//!
//! assert_eq!(redactor.redact_field("password", "secret"), "[hidden]");
//! assert_eq!(redactor.redact_field("username", "anna"), "anna");
//! ```
//!
//! ## Default redactor
//!
//! ```
//! let redactor = redactkit::default_redactor();
//!
//! assert_eq!(redactor.redact_field("password", "secret"), "******");
//! assert_eq!(redactor.redact_field("api_key", "abc123"), "******");
//! ```
//!
//! ## Regex rules
//!
//! ```
//! # #[cfg(feature = "regex")]
//! # {
//! use redactkit::Redactor;
//!
//! let redactor = Redactor::builder()
//!     .field_pattern("(?i)token|secret")
//!     .unwrap()
//!     .value_pattern(r"\d{4}", "****")
//!     .unwrap()
//!     .build();
//!
//! assert_eq!(redactor.redact_field("api_token", "abcd"), "******");
//! assert_eq!(redactor.redact_field("note", "card 1234"), "card ****");
//! # }
//! ```
//!
//! ## Tracing integration
//!
//! ```
//! # #[cfg(feature = "tracing")]
//! # {
//! use tracing_subscriber::fmt;
//!
//! fmt()
//!     .fmt_fields(redactkit::tracing::redact_fields())
//!     .init();
//! # }
//! ```

#![forbid(unsafe_code)]

pub mod builder;
pub mod error;
pub mod patterns;
pub mod redactor;

#[cfg(feature = "tracing")]
pub mod tracing;

#[cfg(feature = "serde")]
/// Derives `Serialize` for structs, redacting fields marked with `#[redact]`.
///
/// This derive generates a `serde::Serialize` implementation that replaces
/// redacted fields with a mask string during serialization.
///
/// The original values in memory are not modified.
///
/// # Examples
///
/// ```
/// use redactkit::RedactSerialize;
///
/// #[derive(RedactSerialize)]
/// struct Config {
///     username: String,
///     #[redact]
///     password: String,
/// }
///
/// let config = Config {
///     username: "anna".to_string(),
///     password: "s3cr3t".to_string(),
/// };
///
/// let json = serde_json::to_string(&config).expect("serialization should succeed");
///
/// assert_eq!(json, r#"{"username":"anna","password":"******"}"#);
/// ```
pub use redactkit_derive::RedactSerialize;

#[cfg(feature = "serde")]
#[doc(hidden)]
pub mod __private {
    //! Private re-exports used by `redactkit` derive macros.
    //!
    //! This module is not part of the public API.
    pub use serde;
}

pub use builder::RedactorBuilder;
pub use error::Error;
pub use redactor::{Redactor, default_redactor};

#[cfg(feature = "derive")]
pub use redactkit_derive::RedactDebug;