| Crates.io | safe-debug |
| lib.rs | safe-debug |
| version | 0.1.1 |
| created_at | 2025-11-21 06:43:42.955085+00 |
| updated_at | 2025-11-28 21:16:59.863457+00 |
| description | Derives std::fmt::Debug with automatic redaction for sensitive fields marked with #[facet(sensitive)] |
| homepage | https://github.com/ceejbot/safe-debug |
| repository | https://github.com/ceejbot/safe-debug |
| max_upload_size | |
| id | 1943189 |
| size | 73,500 |
A Rust derive macro for std::fmt::Debug that automatically redacts sensitive fields marked with #[facet(sensitive)]. Derive Facet and SafeDebug, then decorate fields with sensitive data with #[facet(sensitive)]. You'll get:
std::fmt::Debug implementation-- respects {:?} and {:#?}The benefit of opting into facet here is, for example, the ability to do other things based on the presence of the shape metadata and the sensitivity flag, which you might want when implementing things in a context where this matters
use facet::Facet;
use safe_debug::SafeDebug;
#[derive(Facet, SafeDebug)]
struct PatientRecord {
id: String,
name: String,
#[facet(sensitive)]
ssn: String,
#[facet(sensitive)]
medical_history: String,
}
fn main() {
let record = PatientRecord {
id: "12345".to_string(),
name: "John Doe".to_string(),
ssn: "123-45-6789".to_string(),
medical_history: "Confidential medical data".to_string(),
};
// While logging, somebody drops in data that includes fields with PHI in them...
println!("useful but inattentive log line; record='{:?}'", record);
// but we are saved because the output is:
// useful but inattentive log line; record=PatientRecord { id: "12345", name: "John Doe", ssn: "[REDACTED]", medical_history: "[REDACTED]" }
// Pretty-print also works
println!("{:#?}", record);
}
See the ./examples directory for more usage examples.
Add safe-debug and facet to your crate's dependencies:
cargo add safe-debug
cargo add facet
Or add manually to your Cargo.toml:
[dependencies]
safe-debug = "0.1"
facet = "0.31"
For any struct or enum where you want automatic sensitive field redaction:
Facet and SafeDebug#[facet(sensitive)]{:?} or {:#?} formatting as normalThe Debug implementation will automatically redact fields marked as sensitive.
YourType: Facet is not satisfied"Remember to derive Facet as well as SafeDebug:
#[derive(Facet, SafeDebug)]
struct YourType { /* ... */ }
T: std::fmt::Debug is not satisfied"Make sure all your type parameters implement Debug:
#[derive(Facet, SafeDebug)]
struct Container<T: Debug> { // add a bound here to enforce it
data: T,
}
Make sure you're actually using the Debug trait (via {:?} or {:#?}), not Display or other formatting traits, via {}. This crate only derives Debug, leaving Display for you to control if you need it to print sensitive data for legitmate reasons.
This crate uses the same licensing approach as facet itself.
Licensed under either of:
at your option.