Skip to content

Commit

Permalink
Add fields(Default=!) for struct attribute #[builder] (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganmzz committed Sep 15, 2024
1 parent cc26b55 commit 8011d85
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Source: https://keepachangelog.com/
### Added

- Add nested `fields(...)` value for struct attribute `#[builder]` (#32)
- Add `fields(Default=!)` for struct attribute `#[builder]` (#32)

## [1.1.0] - 2024-08-26

Expand Down
3 changes: 3 additions & 0 deletions CRATES_IO.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Enforce [`Default`][Default] support for **struct**. See ["`Default` struct"](#d
* **`fields(Option=!)`** <br/>
Disable automatic [`Option`][Option] detection for **fields**. See ["`Option` fields"](#option-fields).

* **`fields(Default=!)`** <br/>
Disable automatic [`Default`][Default] detection for **fields**. See ["`Default` fields"](#default-fields).

* **`fields(Into=!)`** <br/>
Disable [`Into`][Into] for **fields**. See ["`Into` argument"](#into-argument).

Expand Down
141 changes: 141 additions & 0 deletions macon_derive/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct StructBuilder {
#[derive(Debug, Default, PartialEq)]
pub struct StructBuilderFields {
option: Setting<()>,
default: Setting<()>,
into: Setting<()>,
}

Expand Down Expand Up @@ -119,6 +120,13 @@ impl StructBuilderFields {
&mut self.option
}

pub fn default(&self) -> &Setting<()> {
&self.default
}
pub fn default_mut(&mut self) -> &mut Setting<()> {
&mut self.default
}

pub fn into_(&self) -> &Setting<()> {
&self.into
}
Expand All @@ -131,6 +139,9 @@ impl StructBuilderFields {
if nested.path.is_ident("Option") {
self.option = Setting::<()>::from_parse_nested_meta(nested)
.map_err_context("Unable to parse Option for fields struct builder attribute")?;
} else if nested.path.is_ident("Default") {
self.default = Setting::<()>::from_parse_nested_meta(nested)
.map_err_context("Unable to parse Default for fields struct builder attribute")?;
} else if nested.path.is_ident("Into") {
self.into = Setting::<()>::from_parse_nested_meta(nested)
.map_err_context("Unable to parse Into for fields struct builder attribute")?;
Expand Down Expand Up @@ -293,6 +304,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -323,6 +339,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -353,6 +374,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -384,6 +410,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -414,6 +445,11 @@ pub mod tests {
Setting::enable((), span()),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -444,6 +480,11 @@ pub mod tests {
Setting::disable(span()),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -474,6 +515,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::enable((), span()),
Expand Down Expand Up @@ -504,6 +550,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::disable(span()),
Expand Down Expand Up @@ -534,6 +585,11 @@ pub mod tests {
Setting::enable((), span()),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -564,6 +620,81 @@ pub mod tests {
Setting::disable(span()),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
"fields.option",
);
}

#[test]
fn struct_builder_attribute_fields_default_enabled() {
let derive_input: DeriveInput = parse_quote! {
#[builder(fields(Default))]
struct Foobar;
};
let builder = StructBuilder::from_input(&derive_input)
.expect("StructBuilder::from_input");
assert_eq!(
builder.mode,
Setting::undefined(),
"mode",
);
assert_eq!(
builder.default,
Setting::undefined(),
"default",
);
assert_eq!(
builder.fields.into,
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::enable((), span()),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
"fields.option",
);
}

#[test]
fn struct_builder_attribute_fields_default_disabled() {
let derive_input: DeriveInput = parse_quote! {
#[builder(fields(Default=!))]
struct Foobar;
};
let builder = StructBuilder::from_input(&derive_input)
.expect("StructBuilder::from_input");
assert_eq!(
builder.mode,
Setting::undefined(),
"mode",
);
assert_eq!(
builder.default,
Setting::undefined(),
"default",
);
assert_eq!(
builder.fields.into,
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::disable(span()),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::undefined(),
Expand Down Expand Up @@ -594,6 +725,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::enable((), span()),
Expand Down Expand Up @@ -624,6 +760,11 @@ pub mod tests {
Setting::undefined(),
"fields.into",
);
assert_eq!(
builder.fields.default,
Setting::undefined(),
"fields.default",
);
assert_eq!(
builder.fields.option,
Setting::disable(span()),
Expand Down
Loading

0 comments on commit 8011d85

Please sign in to comment.