Skip to content

Add safe attribute helper to SafeDebug #2613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions sdk/typespec/typespec_client_core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,6 @@

use std::borrow::Cow;

/// Derive to help prevent leaking personally identifiable information (PII) that deriving [`Debug`](std::fmt::Debug) might otherwise.
///
/// `SafeDebug` is not a trait and cannot be implemented, nor should you derive `Debug` explicitly.
/// Only when you derive `SafeDebug` will types help prevent leaking PII because, by default, only the type name is printed.
/// Only when you import `typespec_client_core` with feature `debug` will it derive `Debug` normally.
///
/// # Examples
///
/// ```
/// use typespec_client_core::fmt::SafeDebug;
///
/// #[derive(SafeDebug)]
/// struct MyModel {
/// name: Option<String>,
/// }
///
/// let model = MyModel {
/// name: Some("Kelly Smith".to_string()),
/// };
/// if cfg!(feature = "debug") {
/// assert_eq!(format!("{model:?}"), r#"MyModel { name: Some("Kelly Smith") }"#);
/// } else {
/// assert_eq!(format!("{model:?}"), "MyModel { .. }");
/// }
/// ```
#[cfg(feature = "derive")]
pub use typespec_macros::SafeDebug;

Expand Down
55 changes: 48 additions & 7 deletions sdk/typespec/typespec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,66 @@ pub fn derive_model(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
/// Only when you derive `SafeDebug` will types help prevent leaking PII because, by default, only the type name is printed.
/// Only when you enable the `debug` feature will it derive `Debug` normally.
///
/// You can attribute types, fields, and variants with `#[safe(true)]` or `#[safe(false)]` to optionally show or hide members.
/// The default is that no members are shown. The inner most `#[safe(..)]` attribute determines whether to show or hide a member.
///
/// # Examples
///
/// ```
/// # use typespec_macros::SafeDebug;
/// #[derive(SafeDebug)]
/// struct MyModel {
/// name: Option<String>,
/// struct Person {
/// name: String,
/// }
///
/// let person = Person {
/// name: "Kelly Smith".to_string(),
/// };
/// if cfg!(feature = "debug") {
/// assert_eq!(format!("{person:?}"), r#"Person { name: "Kelly Smith" }"#);
/// } else {
/// assert_eq!(format!("{person:?}"), "Person { .. }");
/// }
/// ```
///
/// Using the `#[safe(..)]` attribute, you can selectively show or hide members.
/// The default, when not present or inherited, is to always hide members unless the `debug` feature is enabled.
///
/// ```
/// # use typespec_macros::SafeDebug;
/// use std::ops::Range;
///
/// #[derive(SafeDebug)]
/// struct Employee {
/// name: String,
/// #[safe(true)]
/// position: Position,
/// }
///
/// #[derive(SafeDebug)]
/// #[safe(true)]
/// struct Position {
/// id: i32,
/// title: String,
/// #[safe(false)]
/// salary: Range<i32>,
/// }
///
/// let model = MyModel {
/// name: Some("Kelly Smith".to_string()),
/// let employee = Employee {
/// name: "Kelly Smith".to_string(),
/// position: Position {
/// id: 12,
/// title: "Staff Engineer".to_string(),
/// salary: 200_000..250_000,
/// },
/// };
/// if cfg!(feature = "debug") {
/// assert_eq!(format!("{model:?}"), r#"MyModel { name: Some("Kelly Smith") }"#);
/// assert_eq!(format!("{employee:?}"), r#"Employee { name: "Kelly Smith", position: Position { id: 12, title: "Staff Engineer", salary: 200000..250000 } }"#);
/// } else {
/// assert_eq!(format!("{model:?}"), "MyModel { .. }");
/// assert_eq!(format!("{employee:?}"), r#"Employee { position: Position { id: 12, title: "Staff Engineer", .. }, .. }"#);
/// }
/// ```
#[proc_macro_derive(SafeDebug)]
#[proc_macro_derive(SafeDebug, attributes(safe))]
pub fn derive_safe_debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
run_derive_macro(input, safe_debug::derive_safe_debug_impl)
}
Loading