Skip to content
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

[PLATFORM-861]: Fix borked enum variant name formatting #61

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
2 changes: 1 addition & 1 deletion veil-macros/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(super) fn derive_redact(
for (variant, flags) in e.variants.iter().zip(variant_flags.into_iter()) {
// Variant name redacting
let variant_name = variant.ident.to_string();
let variant_name = if let Some(flags) = &flags.variant_flags {
let variant_name = if let Some(flags @ FieldFlags { skip: false, .. }) = &flags.variant_flags {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for me: wth is @?

Copy link
Member

@MaeIsBad MaeIsBad Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it binds the flags variable and matches the pattern
https://doc.rust-lang.org/stable/rust-by-example/flow_control/match/binding.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such an awesome feature :)

// The variant name must always be formatted with the Display impl.
let flags = FieldFlags {
display: true,
Expand Down
78 changes: 78 additions & 0 deletions veil-tests/src/redaction_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,81 @@ fn test_enum_display_redaction() {
"Foo { foo: ***** \"*******\"!\n*** ****'* *** *******..., bar: \"***** \\\"*******\\\"!\\**** ****'* *** *******...\" }"
);
}

#[test]
fn test_enum_variant_names() {
#[derive(Debug)]
enum Control {
Foo(String),
Bar,
}

#[derive(Redact)]
enum Redacted {
#[redact(variant)]
Foo(#[redact] String),
Bar,
}

#[derive(Redact)]
#[redact(all, variant)]
enum RedactedAll {
#[redact(skip, variant)]
Foo(#[redact] String),
#[redact(skip, variant)]
Bar,
}

assert_eq!(format!("{:?}", RedactedAll::Bar), format!("{:?}", Redacted::Bar));
assert_ne!(
format!("{:?}", Redacted::Foo("Hello".to_string())),
format!("{:?}", RedactedAll::Foo("Hello".to_string()))
);
assert_eq!(
format!("{:?} {:?}", Control::Foo("Hello".to_string()), Control::Bar),
"Foo(\"Hello\") Bar"
);
assert_eq!(
format!("{:?} {:?}", Redacted::Foo("Hello".to_string()), Redacted::Bar),
"***(\"*****\") Bar"
);
assert_eq!(format!("{:?}", Control::Bar), format!("{:?}", Redacted::Bar));
assert_ne!(
format!("{:?}", Control::Foo("Hello".to_string())),
format!("{:?}", Redacted::Foo("Hello".to_string()))
);
}

#[test]
fn test_struct_name() {
#[derive(Debug)]
struct Control {
#[allow(dead_code)]
foo: String,
}

#[derive(Redact)]
struct Redacted {
#[redact]
foo: String,
}

assert_eq!(
format!(
"{:?}",
Control {
foo: "Hello".to_string()
}
),
"Control { foo: \"Hello\" }"
);
assert_eq!(
format!(
"{:?}",
Redacted {
foo: "Hello".to_string()
}
),
"Redacted { foo: \"*****\" }"
);
}