Skip to content

Commit

Permalink
Add basic #[expunge(default)] shorthand (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
VanceLongwill authored Mar 13, 2024
1 parent 458b112 commit 27b2864
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
112 changes: 112 additions & 0 deletions expunge/tests/expunge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,95 @@ fn it_works_enum() {
assert_eq!(SensitiveItem::ZeroizableString("99".to_string()), expunged);
}

#[test]
fn it_works_enum_all() {
#[derive(PartialEq, Debug, Clone, Expunge)]
enum SensitiveNested {
Name(#[expunge] String, i32),
}

#[derive(Clone, Debug, PartialEq)]
struct UnitStruct;

impl Expunge for UnitStruct {
fn expunge(self) -> Self
where
Self: Sized,
{
self
}
}

#[derive(PartialEq, Debug, Clone, Expunge)]
#[expunge(all)]
enum SensitiveItem {
Name(String, i32),
DateOfBirth(String),
BankDetails {
account_number: i32,
},
Location(Location),
Nested(SensitiveNested, i32),
LocationHistory(Vec<Location>),
WithUnit(i32, UnitStruct),
#[expunge(as = i32::MAX, zeroize)]
Zeroizable(i32),
#[expunge(as = "99".to_string(), zeroize)]
ZeroizableString(String),
}

#[derive(PartialEq, Debug, Clone, Expunge, Default)]
struct Location {
#[expunge]
city: String,
}

let item = SensitiveItem::Name("Bob".to_string(), 1);

let expunged = item.expunge();

assert_eq!(SensitiveItem::Name("".to_string(), 0), expunged);

let item = SensitiveItem::BankDetails {
account_number: 123,
};
let expunged = item.expunge();
assert_eq!(SensitiveItem::BankDetails { account_number: 0 }, expunged);

let new_york = Location {
city: "New York".to_string(),
};
let item = SensitiveItem::Location(new_york.clone());

let expunged = item.expunge();
assert_eq!(SensitiveItem::Location(Location::default()), expunged);

let item = SensitiveItem::Nested(SensitiveNested::Name("Alice".to_string(), 1), 99);
let expunged = item.expunge();
assert_eq!(
SensitiveItem::Nested(SensitiveNested::Name("".to_string(), 1), 0),
expunged
);

let boston = Location {
city: "Boston".to_string(),
};
let item = SensitiveItem::LocationHistory(vec![new_york, boston]);
let expunged = item.expunge();
assert_eq!(
SensitiveItem::LocationHistory(vec![Location::default(), Location::default()],),
expunged
);

let item = SensitiveItem::Zeroizable(12309812);
let expunged = item.expunge();
assert_eq!(SensitiveItem::Zeroizable(2147483647), expunged);

let item = SensitiveItem::ZeroizableString("my_password".to_string());
let expunged = item.expunge();
assert_eq!(SensitiveItem::ZeroizableString("99".to_string()), expunged);
}

#[test]
fn it_returns_boxed() {
#[derive(Expunge)]
Expand All @@ -431,3 +520,26 @@ fn it_returns_boxed() {

let _: Box<Location> = location.expunge();
}

#[test]
fn it_expunges_default() {
#[derive(Default)]
struct SomeData {
pub name: String,
}

#[derive(Expunge)]
struct Person {
#[expunge(default)]
data: SomeData,
}

let p = Person {
data: SomeData {
name: "John Smith".to_string(),
},
};


assert_eq!(String::default(), p.expunge().data.name);
}
4 changes: 4 additions & 0 deletions expunge_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const ALL: &str = "all";
const IGNORE: &str = "ignore";
const ZEROIZE: &str = "zeroize";
const SLOG: &str = "slog";
const DEFAULT: &str = "default";

fn parse_attributes(
span: Span,
Expand Down Expand Up @@ -273,6 +274,9 @@ fn parse_attributes(
format!("the `{SLOG}` feature must be enabled"),
))
}
} else if meta.path.is_ident(DEFAULT) {
builder.expunge_as = Some(quote!{ Default::default() });
Ok(())
} else {
Err(syn::Error::new(
meta.path.span(),
Expand Down

0 comments on commit 27b2864

Please sign in to comment.