Skip to content

Commit

Permalink
feat(AutoDebug): add print format debug_debug_not_pretty for {:?}
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Feb 9, 2025
1 parent a2114ff commit 17b7800
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Works on structs and enums, similar to [`std::fmt::Debug`] but support some cust

* Specify field name or value in print message.
* Ignore specified field.
* Use [`std::fmt::Display`] instead of [`std::fmt:;Debug`] on specified field.
* Use [`std::fmt::Display`] instead of [`std::fmt::Debug`] on specified field.
* Set print style similar to printing tuple or struct.

#### Basic Usage
Expand All @@ -27,8 +27,9 @@ Works on structs and enums, similar to [`std::fmt::Debug`] but support some cust
* `#[debug_name = "foo"]` override field name with "foo", if in struct `debug_style`.
* `#[debug_value = "foo"]` override field value with "foo".
* `#[debug_ignore]` will ignore this field in the output.
* `#[debug_debug]` will use [Debug] trait implementation for this field in output.
* `#[debug_display]` will use [Display] trait implementation for this field in output.
* `#[debug_debug]` will use `Debug` `{:#?}` for this field in output.
* `#[debug_display]` will use `Display` `{}` for this field in output.
* `#[debug_debug_not_pretty]` will use `Debug` `{:?}` for this field in output.

#### Example

Expand Down Expand Up @@ -172,7 +173,8 @@ The string format can be set to
* `PascalCase`
* `snake_case`
* `SCREAMING_CASE`
by adding a `#[autorule = "xxxx"]` attribute to the enum:

by adding a `#[autorule = "xxxx"]` attribute to the enum:

``` rust
#[derive(AutoStr)]
Expand Down
11 changes: 11 additions & 0 deletions examples/auto_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ struct Foo2 {
}

#[derive(AutoDebug)]
#[debug_format = "debug"]
enum Foo3 {
Foo1,
Foo2((i32, u32)),
Foo3(Foo2),
Foo4 { a: i32, b: u32 },
#[debug_debug_not_pretty]
Foo5(Option<String>),
}

#[derive(AutoDebug)]
Expand Down Expand Up @@ -131,6 +134,14 @@ fn main() {
}"#
);

let foo35 = Foo3::Foo5(Some(String::from("hello world")));
assert_eq!(
format(format_args!("{foo35:#?}")),
r#"Foo5(
Some("hello world"),
)"#,
);

let my_type = MyType {};
let foo4 = Foo4::Foo3 { a: my_type, b: 4 };
assert_eq!(
Expand Down
33 changes: 19 additions & 14 deletions src/auto_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ enum DebugStyle {
enum DebugFormat {
Debug,
Display,
DebugNotPretty,
}

const PLACEHOLDER_DEBUG: &str = "{:#?}";
const PLACEHOLDER_DISPLAY: &str = "{}";
const PLACEHOLDER_DEBUG_NOT_PRETTY: &str = "{:?}";

pub fn auto_debug_internal(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
// println!("{:#?}", ast);
Expand All @@ -37,9 +42,6 @@ pub fn auto_debug_internal(input: TokenStream) -> TokenStream {

#[allow(clippy::too_many_lines)]
fn auto_debug_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
let debug_placeholder = "{:#?}";
let display_placeholder = "{}";

let mut debug_style = DebugStyle::Struct;
let mut debug_format = DebugFormat::Debug;

Expand Down Expand Up @@ -154,6 +156,7 @@ fn auto_debug_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream
"debug_ignore" => field_ignore = true,
"debug_display" => field_format = Some(DebugFormat::Display),
"debug_debug" => field_format = Some(DebugFormat::Debug),
"debug_debug_not_pretty" => field_format = Some(DebugFormat::DebugNotPretty),
_ => continue,
}
}
Expand All @@ -167,11 +170,13 @@ fn auto_debug_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream
field_override_name.map_or_else(|| field_ident.to_string(), |v| v);

let mut raw_field_placeholder = match field_format {
Some(DebugFormat::Debug) => debug_placeholder,
Some(DebugFormat::Display) => display_placeholder,
Some(DebugFormat::Debug) => PLACEHOLDER_DEBUG,
Some(DebugFormat::Display) => PLACEHOLDER_DISPLAY,
Some(DebugFormat::DebugNotPretty) => PLACEHOLDER_DEBUG_NOT_PRETTY,
None => match debug_format {
DebugFormat::Debug => debug_placeholder,
DebugFormat::Display => display_placeholder,
DebugFormat::Debug => PLACEHOLDER_DEBUG,
DebugFormat::Display => PLACEHOLDER_DISPLAY,
DebugFormat::DebugNotPretty=> PLACEHOLDER_DEBUG_NOT_PRETTY,
},
}
.to_string();
Expand Down Expand Up @@ -216,9 +221,6 @@ fn auto_debug_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream

#[allow(clippy::too_many_lines)]
fn auto_debug_enum(ast: &DeriveInput, data_enum: &DataEnum) -> TokenStream {
let debug_placeholder = "{:#?}";
let display_placeholder = "{}";

let mut debug_format = DebugFormat::Debug;

let target_ident = &ast.ident;
Expand Down Expand Up @@ -279,6 +281,7 @@ fn auto_debug_enum(ast: &DeriveInput, data_enum: &DataEnum) -> TokenStream {
"debug_ignore" => variant_ignore = true,
"debug_display" => variant_format = Some(DebugFormat::Display),
"debug_debug" => variant_format = Some(DebugFormat::Debug),
"debug_debug_not_pretty" => variant_format = Some(DebugFormat::DebugNotPretty),
_ => continue,
}
}
Expand All @@ -289,11 +292,13 @@ fn auto_debug_enum(ast: &DeriveInput, data_enum: &DataEnum) -> TokenStream {
}

let variant_placeholder = match variant_format {
Some(DebugFormat::Debug) => debug_placeholder,
Some(DebugFormat::Display) => display_placeholder,
Some(DebugFormat::Debug) => PLACEHOLDER_DEBUG,
Some(DebugFormat::Display) => PLACEHOLDER_DISPLAY,
Some(DebugFormat::DebugNotPretty) => PLACEHOLDER_DEBUG_NOT_PRETTY,
None => match debug_format {
DebugFormat::Debug => debug_placeholder,
DebugFormat::Display => display_placeholder,
DebugFormat::Debug => PLACEHOLDER_DEBUG,
DebugFormat::Display => PLACEHOLDER_DISPLAY,
DebugFormat::DebugNotPretty => PLACEHOLDER_DEBUG_NOT_PRETTY,
},
};

Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//!
//! * Specify field name or value in print message.
//! * Ignore specified field.
//! * Use [`std::fmt::Display`] instead of [`std::fmt:;Debug`] on specified field.
//! * Use [`std::fmt::Display`] instead of [`std::fmt::Debug`] on specified field.
//! * Set print style similar to printing tuple or struct.
//!
//! ## [`AutoStr`]
Expand Down Expand Up @@ -258,13 +258,15 @@ pub fn copy_with(input: TokenStream) -> TokenStream {
/// * `#[debug_name = "foo"]` override field name with "foo", if in struct `debug_style`.
/// * `#[debug_value = "foo"]` override field value with "foo".
/// * `#[debug_ignore]` will ignore this field in the output.
/// * `#[debug_debug]` will use [Debug] trait implementation for this field in output.
/// * `#[debug_display]` will use `Display` trait implementation for this field in output.
/// * `#[debug_debug]` will use `Debug` `{:#?}` for this field in output.
/// * `#[debug_display]` will use `Display` `{}` for this field in output.
/// * `#[debug_debug_not_pretty]` will use `Debug` `{:?}` for this field in output.
///
/// ## Enum Variant Attributes
/// * `#[debug_ignore]`
/// * `#[debug_debug]`
/// * `#[debug_display]`
/// * `#[debug_debug_not_pretty]`
///
/// # Example
///
Expand Down Expand Up @@ -346,6 +348,8 @@ pub fn copy_with(input: TokenStream) -> TokenStream {
/// Foo2((i32, u32)),
/// Foo3(Foo2),
/// Foo4 { a: i32, b: u32 },
/// #[debug_debug_not_pretty]
/// Foo5(Option<String>),
/// }
///
/// let foo33 = Foo3::Foo3(Foo2 {
Expand All @@ -371,6 +375,14 @@ pub fn copy_with(input: TokenStream) -> TokenStream {
/// }"#
/// );
///
/// let foo35 = Foo3::Foo5(Some(String::from("hello world")));
/// assert_eq!(
/// format(format_args!("{foo35:#?}")),
/// r#"Foo5(
/// Some("hello world"),
/// )"#,
/// );
///
/// ```
///
#[proc_macro_derive(
Expand All @@ -383,6 +395,7 @@ pub fn copy_with(input: TokenStream) -> TokenStream {
debug_ignore,
debug_debug,
debug_display,
debug_debug_not_pretty,
)
)]
pub fn auto_debug(input: TokenStream) -> TokenStream {
Expand Down

0 comments on commit 17b7800

Please sign in to comment.