@@ -91,25 +91,66 @@ pub fn derive_model(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
91
91
/// Only when you derive `SafeDebug` will types help prevent leaking PII because, by default, only the type name is printed.
92
92
/// Only when you enable the `debug` feature will it derive `Debug` normally.
93
93
///
94
+ /// You can attribute types, fields, and variants with `#[safe(true)]` or `#[safe(false)]` to optionally show or hide members.
95
+ /// The default is that no members are shown. The inner most `#[safe(..)]` attribute determines whether to show or hide a member.
96
+ ///
94
97
/// # Examples
95
98
///
96
99
/// ```
97
100
/// # use typespec_macros::SafeDebug;
98
101
/// #[derive(SafeDebug)]
99
- /// struct MyModel {
100
- /// name: Option<String>,
102
+ /// struct Person {
103
+ /// name: String,
104
+ /// }
105
+ ///
106
+ /// let person = Person {
107
+ /// name: "Kelly Smith".to_string(),
108
+ /// };
109
+ /// if cfg!(feature = "debug") {
110
+ /// assert_eq!(format!("{person:?}"), r#"Person { name: "Kelly Smith" }"#);
111
+ /// } else {
112
+ /// assert_eq!(format!("{person:?}"), "Person { .. }");
113
+ /// }
114
+ /// ```
115
+ ///
116
+ /// Using the `#[safe(..)]` attribute, you can selectively show or hide members.
117
+ /// The default, when not present or inherited, is to always hide members unless the `debug` feature is enabled.
118
+ ///
119
+ /// ```
120
+ /// # use typespec_macros::SafeDebug;
121
+ /// use std::ops::Range;
122
+ ///
123
+ /// #[derive(SafeDebug)]
124
+ /// struct Employee {
125
+ /// name: String,
126
+ /// #[safe(true)]
127
+ /// position: Position,
128
+ /// }
129
+ ///
130
+ /// #[derive(SafeDebug)]
131
+ /// #[safe(true)]
132
+ /// struct Position {
133
+ /// id: i32,
134
+ /// title: String,
135
+ /// #[safe(false)]
136
+ /// salary: Range<i32>,
101
137
/// }
102
138
///
103
- /// let model = MyModel {
104
- /// name: Some("Kelly Smith".to_string()),
139
+ /// let employee = Employee {
140
+ /// name: "Kelly Smith".to_string(),
141
+ /// position: Position {
142
+ /// id: 12,
143
+ /// title: "Staff Engineer".to_string(),
144
+ /// salary: 200_000..250_000,
145
+ /// },
105
146
/// };
106
147
/// if cfg!(feature = "debug") {
107
- /// assert_eq!(format!("{model :?}"), r#"MyModel { name: Some( "Kelly Smith") }"#);
148
+ /// assert_eq!(format!("{employee :?}"), r#"Employee { name: "Kelly Smith", position: Position { id: 12, title: "Staff Engineer", salary: 200000..250000 } }"#);
108
149
/// } else {
109
- /// assert_eq!(format!("{model :?}"), "MyModel { .. }" );
150
+ /// assert_eq!(format!("{employee :?}"), r#"Employee { position: Position { id: 12, title: "Staff Engineer", .. }, .. }"# );
110
151
/// }
111
152
/// ```
112
- #[ proc_macro_derive( SafeDebug ) ]
153
+ #[ proc_macro_derive( SafeDebug , attributes ( safe ) ) ]
113
154
pub fn derive_safe_debug ( input : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
114
155
run_derive_macro ( input, safe_debug:: derive_safe_debug_impl)
115
156
}
0 commit comments