Skip to content

feat: add 'required' attribute for Option fields#58

Open
Deepak8858 wants to merge 1 commit intofunnyboy-roks:mainfrom
Deepak8858:fix/issue-55-required-option
Open

feat: add 'required' attribute for Option fields#58
Deepak8858 wants to merge 1 commit intofunnyboy-roks:mainfrom
Deepak8858:fix/issue-55-required-option

Conversation

@Deepak8858
Copy link
Copy Markdown

Added a required attribute for Option fields that are normally unwrapped to be optional. This allows forcing a field to be specified even if its type is Option<T>. This works for both default (owned/borrowed) and type-state builders. Fixes #55.

Copy link
Copy Markdown
Owner

@funnyboy-roks funnyboy-roks left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! I am glad to see it really was just changing how wrapped_option is set!

There are a few notes, but nothing too major. Your branch is somewhat out dated, but most of the changes I've made since then are just on restructuring so updating should be pretty straightforward.

Especially since I'm pretty heavily developing this crate right now, if you want to pick up other issues, leave a note on the issue that you're working on so I don't also start working on it 😅

Comment on lines 123 to 151
impl Attribute {
fn as_str(self) -> &'static str {
self.into()
const ALL: [Self; 10] = [
Self::Default,
Self::Into,
Self::Repeat,
Self::RepeatN,
Self::Rename,
Self::SkipPrefix,
Self::SkipSuffix,
Self::Tuple,
Self::Adapter,
Self::Required,
];

const fn as_str(self) -> &'static str {
match self {
Attribute::Default => "default",
Attribute::Into => "into",
Attribute::Repeat => "repeat",
Attribute::RepeatN => "repeat_n",
Attribute::Rename => "rename",
Attribute::SkipPrefix => "skip_prefix",
Attribute::SkipSuffix => "skip_suffix",
Attribute::Tuple => "tuple",
Attribute::Adapter => "adapter",
Attribute::Required => "required",
}
}
}
Copy link
Copy Markdown
Owner

@funnyboy-roks funnyboy-roks Apr 5, 2026

Choose a reason for hiding this comment

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

In #49, this was modified to be less manual. Those changes will appear when you update your branch.

Comment on lines +624 to +627
if out.required {
bail!(ident.span() => "`required` may only be used once.");
}
out.required = true;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Add a check here to ensure that the the type is Option. (I'd probably just re-use get_single_generic)

Comment on lines +283 to +284
/// Force a field to be specified, even if it is an `Option`. Normally, `Option` fields are
/// optional.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think it would be better to have this be a bit more concise, something like

Force an optional field to be specified using Option

I think the examples do most of the work for this attribute :)

use bauer::Builder;

#[test]
fn required_option_owned() {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you add an instance of this test for the borrowed kind


let err = Foo::builder().build().unwrap_err();
// The exact error name depends on the field name
assert!(format!("{:?}", err).contains("MissingField"));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You should be able to do a proper equality comparison here:

Suggested change
assert!(format!("{:?}", err).contains("MissingField"));
assert_eq!(err, FooBuildError::MissingField);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add required attribute for Option arguments

3 participants